473,498 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a c++ dll, param error?

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
Nov 21 '05 #1
2 2850
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" <jo*@sdfdklshj.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2
Thanks !!!

J


"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
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" <jo*@sdfdklshj.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4731
by: Shailan | last post by:
Hi Im having trouble with the following code that seems to be behave differently when called from the browser as opposed to the command line. The calling script is a cgi that forks, with the...
6
6883
by: dw | last post by:
Hello all, I'm having a dickens of a time calling a stored procedure on a connection. Every time I do, it generates an error "Arguments are of the wrong type, are out of acceptable range, or are in...
2
6880
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
4
1385
by: OzgurGul | last post by:
int main(){ /* I have a function :) and I want to use it but I dunno how to call it... (I have trouble that pointers) please help me... mmm... thanks for all advance!! */
2
9512
by: Blondie21 | last post by:
Hi, I have a .jsp file in the following format: .... html headers, jsp code, html code... <jsp:plugin type="applet" code="path.in.packet.appletclassname.class" codebase=".."...
1
1278
by: Phill W. | last post by:
I'm still having trouble trying to generate MSDN-style documentation for the classes and controls I've written and, because traffic in the SourceForge forums seems to have completely dried up, I...
18
1644
by: Franky | last post by:
I don't know how to ask this so that someone will not misread it but here goes. At run time I want to be able to evaluate functions from the Math library, but at design time I do not know what...
10
6923
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.