473,925 Members | 16,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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\t estsubs\cmosub3 2.dll")]
public static extern string icmo_version_ch k(
[MarshalAs(Unman agedType.LPStr)]
string version );
and using it as follows:

string sTestVersion = "3.0f_p2";
string sVersion = icmo_version_ch k( sTestVersion );

I get an exception: System.NullRefe renceException ... Object reference
could not set to an instance of an object.

What am I doing wrong? Is it the string icmo_version_ch k? If so,
what should I use.

Thanks,

Ted
Nov 15 '05 #1
1 4491
Adam Nathan wrote a book called ".NET and COM, The Complete Interoperabilit y
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\t estsubs\cmosub3 2.dll")]
public static extern String icmo_version_ch k(
String version );

"Ted Sung" <te**@intex.com > wrote in message
news:86******** *************** **@posting.goog le.com... 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\t estsubs\cmosub3 2.dll")]
public static extern string icmo_version_ch k(
[MarshalAs(Unman agedType.LPStr)]
string version );
and using it as follows:

string sTestVersion = "3.0f_p2";
string sVersion = icmo_version_ch k( sTestVersion );

I get an exception: System.NullRefe renceException ... Object reference
could not set to an instance of an object.

What am I doing wrong? Is it the string icmo_version_ch k? If so,
what should I use.

Thanks,

Ted

Nov 15 '05 #2

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

Similar topics

3
1607
by: Zork | last post by:
Hi, I am a little confused with const functions, for instance (here i am just overloading the unary+ operator) if i define: 1) Length Length :: operator+ ( void ) const {return * this;} ... I get no error... 2) Length & Length :: operator+ ( void ) const {return * this;} ... I get the error -> 'return' : cannot convert from 'const class Length' to
7
1793
by: johny smith | last post by:
Well, I thought that I was creating a constant pointer, and would not be able to increment it without a compiler error. But this example compiles fine. The const is to the left of the * so I thought this would force that to be constanst.
4
4637
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
1
4389
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
2
1991
by: Plissken.s | last post by:
Hi, Is it possible to over load a function of a class with const input parameter ? class A { public: void aFunc(const B& b); void aFunc(B& b); }
6
2221
by: Alex Buell | last post by:
Consider: const bool func() {} and bool func() const {} Are they different? --
20
2496
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
3
6430
by: Elikhom | last post by:
Hi, I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is what I have... C++ side void populateRequiredIndexNames(char** names); C# side
4
2993
by: Olaf | last post by:
Hi, is there a way to declare an array with const variables like: static const ushort RX_BUF_SIZE = 0x100; typedef struct { volatile ushort buf; volatile unsigned head; volatile unsigned tail;
0
9932
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
11452
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...
1
11200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10600
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
9801
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...
1
8158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7316
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
4846
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
3
3445
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.