Data Errors When Calling Unmanaged VC6 DLL

G

Guest

Hi All, I Have A Few Problems with a unmanged DLL (VC6) sending back data to
a C# application. The DLL is passed 10 "strings" (char* in the dll), which
should then be updated - in other words i want to pass some strings to the
dll by reference - it is called like this

s1 = enc.GetBytes(lat);
s2 = enc.GetBytes(lon);
s3 = enc.GetBytes(Sats);
s4 = enc.GetBytes(height);
s5 = enc.GetBytes(pDop);
s6 = enc.GetBytes(fp6);
s7 = enc.GetBytes(fp7);
s8 = enc.GetBytes(fp8);
s9 = enc.GetBytes(fp9);
s10 = enc.GetBytes(fp10);

MMGPS.PNT1_TO_AV(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10);

lat = enc.GetString(s1);
lon = enc.GetString(s2);
Sats = enc.GetString(s5);
height = enc.GetString(s3);
pDop= enc.GetString(s4);
fp6 = enc.GetString(s6);
fp7 = enc.GetString(s7);
fp8 = enc.GetString(s8);
fp9 = enc.GetString(s9);
fp10 = enc.GetString(s10);

The Dll Code get the striings okay, proceeds with the processing and updats
them in the dll with the returned version. But when the data gets brought
back to the calling program, it appears that the C# code is only picked up
the first char of the string: so instead of getting 190311.11, it gets 1, and
instead of 44.898 it will get 4, does anybody have any suggestions about what
is cxausing this?

Andrew.

PS here are the C++ and C# function declatations.

int _stdcall PNT1_TO_AV(char* fp1, char* fp2, char* fp3, char* fp4, char*
fp5, char* fp6,
char* fp7, char* fp8, char* fp9, char* fp10)
{

}

[DllImport("MMGPS", EntryPoint = "PNT1_TO_AV", ExactSpelling = true, CharSet
= CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe void PNT1_TO_AV(
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp1,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp2,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp3,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp4,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp5,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp6,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp7,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp8,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp9,
[MarshalAs(UnmanagedType.LPArray)]
byte[] fp10);
 
M

Mattias Sjögren

The Dll Code get the striings okay, proceeds with the processing and updats
them in the dll with the returned version. But when the data gets brought
back to the calling program, it appears that the C# code is only picked up
the first char of the string: so instead of getting 190311.11, it gets 1, and
instead of 44.898 it will get 4, does anybody have any suggestions about what
is cxausing this?

What kind of encoding is the enc variable representing?

BTW you could get rid of all the GetBytes/GetString calls if you used
StringBuilder as the parameter type instead of byte[].


[DllImport("MMGPS", EntryPoint = "PNT1_TO_AV", ExactSpelling = true, CharSet
= CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe void PNT1_TO_AV(

The calling convention is wrong, it should be Stdcall. The return type
should be int, not void. And you can remove the unsafe keyword, it's
not needed.


Mattias
 
G

Guest

Hi Mattias,

The enc is a System.Text.ASCIIEncoder.

Andrew


Mattias Sjögren said:
The Dll Code get the striings okay, proceeds with the processing and updats
them in the dll with the returned version. But when the data gets brought
back to the calling program, it appears that the C# code is only picked up
the first char of the string: so instead of getting 190311.11, it gets 1, and
instead of 44.898 it will get 4, does anybody have any suggestions about what
is cxausing this?

What kind of encoding is the enc variable representing?

BTW you could get rid of all the GetBytes/GetString calls if you used
StringBuilder as the parameter type instead of byte[].


[DllImport("MMGPS", EntryPoint = "PNT1_TO_AV", ExactSpelling = true, CharSet
= CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe void PNT1_TO_AV(

The calling convention is wrong, it should be Stdcall. The return type
should be int, not void. And you can remove the unsafe keyword, it's
not needed.


Mattias
 

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