passing an struct from vb to c dll

A

abhijeet

Hi,
I am having some problem for getting values in struct from the c dll.

I have following definition

Type MBRINFO
szMember As String * 80 ' member name array
usLevel As Integer
End Type

I am passing this struct to c dll for getting back the values filled
from the dll

Declare Function GetMemberInfo Lib "xyz" (pInfo As MBRINFO) As Long

On c side I have code as following

struct MBRINFO
{
char szMember[80];
int usLevel;
}
long GetMemberInfo(MBRINFO * info)
{
long sts = 0;
memset(info,0,sizeof(MBRINFO ));
szMember(info->szMember,"Something");
info->usLevel = 10;
return sts;
}

The definition is exported from the xyz.dll for acess.
But, when I get value back on vb side, I noticed the value for szMember
is filled with assigned value which is "Something" folloed by lot of
space characters. Can somebody help me figure out how to avoid this
issue.

Thanks,
Abhijeet
 
E

Eric Jensen

[snip]
The definition is exported from the xyz.dll for acess.
But, when I get value back on vb side, I noticed the value for szMember
is filled with assigned value which is "Something" folloed by lot of
space characters. Can somebody help me figure out how to avoid this
issue.

First of all you're offtopic. Second you dont show the VB code where you
call the dll function.
Declare Function GetMemberInfo Lib "xyz" (pInfo As MBRINFO) As Long

^^^ That is bad and wrong, and it should be:

Declare Function GetMemberInfo Lib "xyz" (ByRef pInfo As MBRINFO) As Long

When passing user defined VB types to a C dll it must be passed by reference
and not by value as you do.

//eric
 
M

Murali Krishna

adding more...
Hi,
I am having some problem for getting values in struct from the c dll.

Surely an off-topic but how did your dll compile with following errors?
struct MBRINFO
{
char szMember[80];
int usLevel;
}

didn't your compiler ask for ';' at the end of the structure?
long GetMemberInfo(MBRINFO * info)
{
long sts = 0;
memset(info,0,sizeof(MBRINFO ));
szMember(info->szMember,"Something");
info->usLevel = 10;
return sts;
}
szMember(info->szMember,"Something");

what is this anyway? a function?

Don't type in the thread editor. It is better to copy the code from the
compiler IDE.

-- Murali Krishna
 
A

abhijeet

Sorry my mistake code should read

long GetMemberInfo(MBRINFO * info)
{
long sts = 0;
memset(info,0,sizeof(MBRINFO ));
strcpy(info->szMember,"Something");
info->usLevel = 10;
return sts;
}

Anyways thank you guys for your replies.
 
J

Jakob Bieling

^^^ That is bad and wrong, and it should be:

Declare Function GetMemberInfo Lib "xyz" (ByRef pInfo As MBRINFO) As
Long

No, his Declare is absolutely correct, as it is identical to yours.
If you omit ByRef/ByVal, you will get an implied ByRef.

regards
 
G

Gernot Frisch

google for "passing string VB dll" will get some result.
I don't remember anymore, but strings were not just strcpy, that I
know for sure.
 
J

Jim Langston

abhijeet said:
Hi,
I am having some problem for getting values in struct from the c dll.

I have following definition

Type MBRINFO
szMember As String * 80 ' member name array
usLevel As Integer
End Type

I am passing this struct to c dll for getting back the values filled
from the dll

Declare Function GetMemberInfo Lib "xyz" (pInfo As MBRINFO) As Long

On c side I have code as following

struct MBRINFO
{
char szMember[80];
int usLevel;
}
long GetMemberInfo(MBRINFO * info)
{
long sts = 0;
memset(info,0,sizeof(MBRINFO ));
szMember(info->szMember,"Something");
info->usLevel = 10;
return sts;
}

The definition is exported from the xyz.dll for acess.
But, when I get value back on vb side, I noticed the value for szMember
is filled with assigned value which is "Something" folloed by lot of
space characters. Can somebody help me figure out how to avoid this
issue.

Thanks,
Abhijeet

I believe your problem may do with the C style .vs. Pascal style strings. I
may be wrong, however, but perhaps vb is using the pascal style string where
the first byte is the length of the string, then the contents of the string,
where a C style string is simply null terminated.

To fix this, you should perhaps simply also add the length of the string to
the structure so you can clean it up on the Basic side. Otherwise you'll
need to research what VB is expecting it's string array to be filled with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top