all groups > dotnet interop > november 2006 >
You're in the

dotnet interop

group:

converting char* to string gives strange characters


Re: converting char* to string gives strange characters Christian Fröschlin
11/8/2006 12:00:00 AM
dotnet interop:
[quoted text, click to view]

In this case a string representation may not be appropriate. You can use
the Marshal.Copy method to copy the data into a byte array or similar.
Of course, if it is not zero-terminated there must be some other way
converting char* to string gives strange characters Rob Tillie
11/8/2006 12:16:01 AM
Hi,

I'm calling a method from a C dll which I have defined as follows:
[DllImport("avpzip.dll", EntryPoint="VERIFY_ZIP",
CharSet=CharSet.Ansi,
ExactSpelling=false,
CallingConvention=CallingConvention.StdCall)]
public static extern int Verify_Zip(
[MarshalAs(UnmanagedType.LPStr)]
string inbuffer,
IntPtr outbuffer);

In the c header it is specified as:
int
#if defined _WINDLL || defined _WINDOWS
WINAPI _export
#if defined WIN32
_stdcall
#endif
#endif
Verify_Zip(char * zipinbuffer, char * zipoutbuffer);

I am using Marshal.PtrToStringAnsi(outbuffer); to convert it to a string,
which works fine for another method in this library, but not for this one. It
contains weird characters, thereby ending the string far too early.

Any suggestions?

Regards,
-- Rob.
AddThis Social Bookmark Button