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

  • Thread starter Thread starter Ted Sung
  • Start date Start date
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
 
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
 
Back
Top