C declarations in C#, continued

T

Ted Sung

Hi,

I finally got this C function to be called correctly from C#.

char * version_chk( const char *)

I needed to do the following:

[DllImport( "test.dll",
CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr version_chk(
[MarshalAs(UnmanagedType.LPStr)]
string version );

string sTestVersion = "3.0f_p2";
IntPtr test = version_chk( sTestVersion );
// how do we figure out the length?
string sReturnVersion = Marshal.PtrToStringAnsi(test,7);
Console.WriteLine( "version is " + sReturnVersion ) ;

The key was the CallingConvention!

Now that I have the IntPtr, test, available, I'm trying to understand
how I can know the length instead of having to hardcode it.

Any ideas?

Thanks,

Ted
 
M

Mattias Sjögren

Now that I have the IntPtr, test, available, I'm trying to understand
how I can know the length instead of having to hardcode it.

Any ideas?

If the string is zero terminated, you can simply use the
PtrToStringAnsi overload that doesn't take a length parameter. It will
read the string up to the null terminator.



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