String format and unmanaged DLL

T

Thomas

Hi group!

I've a problem calling a function in a unmanaged DLL.

Here is the definition of the function I want to use, as it is written
in the documentation :
char Identify(char * ID)
ID : points to a buffer that receives the string. The buffer must have
sufficient space for the string and a terminating null character.

Here is my code in VB.NET to declare the function :
<DllImport("DllToUse.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function IdentifyReader(ByVal Identity As
StringBuilder)As Integer
End Function

Here is my code in VB.NET to call the function :
Dim IDReader As New StringBuilder(255)
Dim res As Integer

res = Identify(IDReader)
IdentifierLecteur = IDReader.ToString()

My problem is that IDReader only contains "white squares" (ASCII
Code=63 ?). I have no error message but the encoding of the string
seems to be incorrect.

Thank you for your help !

Thomas
 
C

Chris Tacke, eMVP

One part of the problem is your use of char. Since CE is Unicode, all
strings are going to be wchars, so you're bound to get major
inconsistencies.
 
T

Thomas VUILLET

Thanks for your quick answer !

Does it mean that I can't use this DLL with .NET ?
Is there no way to convert the chars into correct strings, or a
different way to declare the function ?

Thomas
 
P

Paul G. Tobey [eMVP]

Are you sure that the DLL is expecting ASCII strings and not Unicode
strings?

Paul T.
 
A

Alex Feinman [MVP]

You can declare your function like this:
<DllImport("DllToUse.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function IdentifyReader(ByVal Identity As Byte())As Integer
End Function

Make sure you have preallocated array of a suffcient size.

After you have called the function, extract you string from it by calling
Encoding.GetString():
Dim buffer as Byte(1024)
IdentifyReader(buffer)
Dim Identity as String = System.Text.Encoding.ASCII.GetString(buffer)
 

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

Top