Problem with encoding between vb.net and a unmanaged DLL in C

Hi!

I have a DLL(made in C) which I want to communicate with, and I get the communication to work but the string which get sent becomes wrongly encoded.

I have tried to declare the dll in the vb.net code like this:

Public Declare Function Write Lib "test.dll" (<MarshalAs(UnmanagedType.LPArray)> ByVal Data() As Byte, ByVal DataLen As Integer, ByVal Id As Integer) As Integer

and also like this:

<DllImport("FG197038d.dll", bestfitmapping:=True, CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi, throwonunmappablechar:=True)> _

Public Shared Function Write(ByVal Data() As Byte, ByVal DataLen As Integer, ByVal Id As Integer) As Integer

End Function

The function in the DLL is declared like this:

int Write(unsigned char *Data, int DataLen, int Id);

The problem is that say I want to send "FFFF" and when the string gets to the DLL the string is converted to "46464646" which is the ASCII value of the F:s, how do I get the original "FFFF" to the DLL without conversion

Many thanks in advance!



Answer this question

Problem with encoding between vb.net and a unmanaged DLL in C

  • Daveko

    Hi again!

    I'm converting the original string to an byte array like this:

    Dim sendArr(10000) As Byte

    sendArr = System.Text.Encoding.ASCII.GetBytes(sendStr)

    I've tried to send a char array instead but the result is the same =/


  • Igor Antunes

    The problem is that I dont have the sourcecode for the DLL, i only have a small debuprogram which can get info from the DLL.

    And that debugprogram reports that it gets hexadecimal value "46" instead of the hexdecimal value "F" which it should get and which I am sending from vb.net

    So the DLL doesnt now anything about strings it just want a reference to a char array containing hexadecimal values


  • Jarrardi

    How are you converting the string to bytes, for one thing.

    Likewise, the function is expecting characters, not bytes, which may nt necessarily be the same thing - i'm a bit rusty on the C side of things, and am not exactly sure what an unsigned char actually is.

    You can try converting your declaration to be an array of chars, instead.



  • SONAL N. SATPUTE

    If you send it a string "F" that is hexadecimal value 46, not a string "46". It's reading exactly what you are sending.

    What do you get if you send a byte array:

    Dim b() As Byte = New Byte() {&HF}



  • AussieNoobie

    First, the DLL doesn't recieve a string, it receives an array of characters, so how do you know what 'string' it recieves How is the DLL converting from an array of characters to a string (things can get very messy when manipulating strings in C/C++).

    Presumably, when you send/convert a 4 character string, you recieve 4 bytes/characters...yet you show 8 characters...it's all in the DLL it seems...



  • Problem with encoding between vb.net and a unmanaged DLL in C