P/Invoke help with C# declaration of C function char * func(const char *)

T

Ted Sung

Hi,

I'm trying to call a C function from a DLL. The C function is
declared as

char * function( const char * )

I'm declaring it in C# as follows:

[DllImport(@"d:\mywork\csharp\testsubs\cmosub32.dll")]
public static extern string icmo_version_chk(
[MarshalAs(UnmanagedType.LPStr)]
string version );


and using it as follows:

string sTestVersion = "3.0f_p2";
string sVersion = icmo_version_chk( sTestVersion );

I get an exception: System.NullReferenceException ... Object reference
could not set to an instance of an object.

What am I doing wrong? Is it the string icmo_version_chk? If so,
what should I use.

Thanks,

Ted
 
K

Ken Onweller \(.NET MCSD\)

Adam Nathan wrote a book called ".NET and COM, The Complete Interoperability
Guide" which
can help with EVERYTHING you ever need to know regarding using COM and
Native libraries.

Nonetheless, you might want to try doing this instead...
[DllImport(@"d:\mywork\csharp\testsubs\cmosub32.dll")]
public static extern String icmo_version_chk(
String version );


Ted Sung said:
Hi,

I'm trying to call a C function from a DLL. The C function is
declared as

char * function( const char * )

I'm declaring it in C# as follows:

[DllImport(@"d:\mywork\csharp\testsubs\cmosub32.dll")]
public static extern string icmo_version_chk(
[MarshalAs(UnmanagedType.LPStr)]
string version );


and using it as follows:

string sTestVersion = "3.0f_p2";
string sVersion = icmo_version_chk( sTestVersion );

I get an exception: System.NullReferenceException ... Object reference
could not set to an instance of an object.

What am I doing wrong? Is it the string icmo_version_chk? If so,
what should I use.

Thanks,

Ted
 

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