By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,806 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

passing an struct from vb to c dll

P: n/a
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

Jul 11 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
abhijeet wrote:
Hi,
I am having some problem for getting values in struct from the c dll.
Way off topic here, try a windows specific group.

--
Ian Collins.
Jul 11 '06 #2

P: n/a

"abhijeet" <ab***********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...

[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
Jul 11 '06 #3

P: n/a
adding more...

abhijeet wrote:
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

Jul 11 '06 #4

P: n/a
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.

Jul 11 '06 #5

P: n/a
Eric Jensen <er**@no.spam.comwrote:

"abhijeet" <ab***********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>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
No, his Declare is absolutely correct, as it is identical to yours.
If you omit ByRef/ByVal, you will get an implied ByRef.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 11 '06 #6

P: n/a

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.
Jul 11 '06 #7

P: n/a
"abhijeet" <ab***********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
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.
Jul 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.