Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi,

I've the following function written in C++ in Win32 Library Project

C++
__declspec(dllexport) void __stdcall SetupChat(char *chatName, bool Advertiser, int &maxchars)

I'm trying to call this function from VB.Net application (Framework 3.5) using the following code:

VB
[dllimport("chatlib32.dll"] _
Private Shared Sub SetupChat(<marshalas(unmanagedtype.lparray)> chatName As Char(), ByVal asAdvertiser As Boolean, ByRef size As Integer)
End Sub

But in the function SetupChat in the C++ it only prints the first character. So I tried the following code instead:

VB
Declare Auto Sub SetupChat Lib "ChatLib32.dll" (name As String, ByVal asAdvertiser As Boolean, ByRef size As Integer)

And I also got the same result of the previous function.

So can anyone help me?

Thanks a lot.
Posted
Updated 24-May-13 3:39am
v2
Comments
Richard MacCutchan 24-May-13 9:42am    
The default character type in VB.NET is Unicode, but char in C++ is ASCII. You need to convert your characters to ASCII in the VB.NET code, or modify the dll code to expect a Unicode string.
Sergey Alexandrovich Kryukov 24-May-13 11:04am    
This is the default marshaling. Other cases are resolved by applying [MarshalAs] attribute to a parameter...
Please see OP's comment below.
—SA
Richard MacCutchan 24-May-13 11:19am    
From the comments it sounds as though the C++ code gets a Unicode string passed in.
Sergey Alexandrovich Kryukov 24-May-13 14:30pm    
And then the parameter should come with appropriate [MarshalAs]...
—SA
Mr.HA-AS 24-May-13 10:01am    
In my c++ project I add UNICODE word in Preprocessor Definitions. Also in my function in VB.NET I added thisenumeration to the SetupChat function CharSet.Unicode as following:

<DllImport("ChatLib32.dll", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Unicode)> _
Private Shared Sub SetupChat(<marshalas(unmanagedtype.lparray)> chatName As Char(), ByVal asAdvertiser As Boolean, ByRef size As Integer)
End Sub

I assume what I did let both VB.Net and VC++ use UNICODE Character Set .But I still get the same result.

1 solution

I got an answer from StackOverFlow as following and it worked with, thanks for everyone supports:

ASM
<DllImport("ChatLib32.dll", CharSet:=CharSet.Ansi)> _
Private Shared Sub SetupChat(ByVal chatName As String, ByVal asAdvertiser As Boolean, ByRef size As Integer)
End Sub
 
Share this answer
 
Comments
Richard MacCutchan 25-May-13 3:42am    
Which is what I told you in my first comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900