By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <te**@intex.com> wrote in message
news:86*************************@posting.google.co m... 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

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.