473,624 Members | 2,252 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_cVersionNumbe r, int p_iVersBufSize,
char* p_cDescription, int p_iDescBufSize,
BOOL& p_bCompulsory );

So, i do this:

[DllImport("myDl l.dll", CharSet=CharSet .Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
ref string p_cVersionNumbe r, 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.Le ngth,
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.NullRef erenceException ' 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 2861
Hi JoeB,

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

[DllImport("myDl l.dll", CharSet=CharSet .Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
StringBuilder p_cVersionNumbe r, 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.StringBu ilder 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******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi

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

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

So, i do this:

[DllImport("myDl l.dll", CharSet=CharSet .Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
ref string p_cVersionNumbe r, 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.Le ngth,
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.NullRef erenceException ' 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******** ******@TK2MSFTN GP14.phx.gbl...
Hi JoeB,

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

[DllImport("myDl l.dll", CharSet=CharSet .Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
StringBuilder p_cVersionNumbe r, 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.StringBu ilder 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******** ********@TK2MSF TNGP12.phx.gbl. ..
Hi

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

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

So, i do this:

[DllImport("myDl l.dll", CharSet=CharSet .Ansi)]
public static extern void
getVersionInfo( long p_lIndex,
ref string p_cVersionNumbe r, 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.Le ngth,
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.NullRef erenceException ' 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
4743
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 child trying to call another cgi script and pass arguments to it. It works fine from the command line, and calls the required script and passes the arguments correctly. However, when it is run on the browser, it calls the script but does not pass...
6
6894
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 conflict with one another." I've run the same stored procedure with the same exact list of arguments in Query Analyser in SQL Server, and it works. The page has the adovbs constants. Note that uspGetProperties is a stored procedure in SQL...
2
6905
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 a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This inteface has a lot more methods which works fine, it is just the
4
1387
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
9516
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=".." jreversion="1.5"
1
1284
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 thought I'd cast my net (sic) a little further afield. So, anyone using VBCommenter with VB 2003, please read on. My biggest problem at the moment is with Events. VBCommenter generates /two/ entries in the XML file (read by NDOC) one with
18
1663
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 functions will be needed. I know I can do If Userinput = "SINE" Then zz = Sin(whatever)
10
6960
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 MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
8177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.