Calling a c++ dll, param error?

J

JoeB

Hi

Trying to call the following (c++) dll:

void getVersionInfo( long p_lIndex,
char* p_cVersionNumber, int p_iVersBufSize,
char* p_cDescription, int p_iDescBufSize,
BOOL& p_bCompulsory );

So, i do this:

[DllImport("myDll.dll", CharSet=CharSet.Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
ref string p_cVersionNumber, int p_iVersBufSize,
ref string p_cDescription, int p_iDescBufSize,
ref uint p_bCompulsory );

uint iCompulsory = 0;
long index = 0;

getVersionInfo( index,
ref sVersionNumber, sVersionNumber.Length,
ref sDescription, sDescription.Length,
ref iCompulsory );

( The dll will try to write to the strings, and iCompulsory )


But is throws with this error:

An unhandled exception of type 'System.NullReferenceException' occurred in
TestApp32.exe
Additional information: Object reference not set to an instance of an
object.


What am i doing wrong?!



j
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi JoeB,

Instead of using 'ref string' parameters try using StringBuilder.

[DllImport("myDll.dll", CharSet=CharSet.Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
StringBuilder p_cVersionNumber, int p_iVersBufSize,
StringBuilder p_cDescription, int p_iDescBufSize,
ref uint p_bCompulsory );


Here is a one article about marshaling data types
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/

And one excerpt from it :
"...
Once you have established whether the text parameter is input only or
input/output, you can determine which CLR type to use for the parameter
type. Here are the rules. If the string parameter is input only, use the
System.String type. Strings are immutable in managed code, and are well
suited to be used as buffers that will not be changed by the native API
function.

If the string parameter can be input and/or output, then use the
System.StringBuilder type. The StringBuilder type is a useful class library
type that helps you build strings efficiently, and it happens to be great
for passing buffers to native functions that the functions fill with string
data on your behalf. Once the function call has returned, you need only call
ToString on the StringBuilder object to get a String object.

...."


HTH
Stoitcho Goutsev (100) [C# MVP]
 
J

JoeB

Thanks !!!



J




Stoitcho Goutsev (100) said:
Hi JoeB,

Instead of using 'ref string' parameters try using StringBuilder.

[DllImport("myDll.dll", CharSet=CharSet.Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
StringBuilder p_cVersionNumber, int p_iVersBufSize,
StringBuilder p_cDescription, int p_iDescBufSize,
ref uint p_bCompulsory );


Here is a one article about marshaling data types
http://msdn.microsoft.com/msdnmag/issues/03/07/NET/

And one excerpt from it :
"...
Once you have established whether the text parameter is input only or
input/output, you can determine which CLR type to use for the parameter
type. Here are the rules. If the string parameter is input only, use the
System.String type. Strings are immutable in managed code, and are well
suited to be used as buffers that will not be changed by the native API
function.

If the string parameter can be input and/or output, then use the
System.StringBuilder type. The StringBuilder type is a useful class
library type that helps you build strings efficiently, and it happens to
be great for passing buffers to native functions that the functions fill
with string data on your behalf. Once the function call has returned, you
need only call ToString on the StringBuilder object to get a String
object.

..."


HTH
Stoitcho Goutsev (100) [C# MVP]

JoeB said:
Hi

Trying to call the following (c++) dll:

void getVersionInfo( long p_lIndex,
char* p_cVersionNumber, int p_iVersBufSize,
char* p_cDescription, int p_iDescBufSize,
BOOL& p_bCompulsory );

So, i do this:

[DllImport("myDll.dll", CharSet=CharSet.Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
ref string p_cVersionNumber, int p_iVersBufSize,
ref string p_cDescription, int p_iDescBufSize,
ref uint p_bCompulsory );

uint iCompulsory = 0;
long index = 0;

getVersionInfo( index,
ref sVersionNumber, sVersionNumber.Length,
ref sDescription, sDescription.Length,
ref iCompulsory );

( The dll will try to write to the strings, and iCompulsory )


But is throws with this error:

An unhandled exception of type 'System.NullReferenceException' occurred
in TestApp32.exe
Additional information: Object reference not set to an instance of an
object.


What am i doing wrong?!



j
 

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