473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting a CString

I am attempting to write a C# program that accesses a C++ library. The
library has functions that return CStrings. So far I have not been able to
find a way to declare the function prototype in C# such that it will work.

For instance, I have tried declaring the function as a string type but when
I call the function, it errors out with the following:

An unhandled exception of type 'System.NullReferenceException' occurred in
TestDLM.exe

Additional information: Object reference not set to an instance of an
object.

I have tried a couple other declarations with no better results.

Does anyone have any advice?

Thanks.
Nov 17 '05 #1
4 3163
I don't think there's anyway to get C# to recognize a CString. I'd ssay
your best bet is to write a wrapper for the C++ library in Managed C++,
which will convert the CStrings to System.Strings

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Stephen Engle" <en***********@aoins.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
I am attempting to write a C# program that accesses a C++ library. The
library has functions that return CStrings. So far I have not been able to find a way to declare the function prototype in C# such that it will work.

For instance, I have tried declaring the function as a string type but when I call the function, it errors out with the following:

An unhandled exception of type 'System.NullReferenceException' occurred in
TestDLM.exe

Additional information: Object reference not set to an instance of an
object.

I have tried a couple other declarations with no better results.

Does anyone have any advice?

Thanks.

Nov 17 '05 #2
Here is an example from my GDI32.cs:
[DllImport("winspool.drv", CharSet=CharSet.Auto)]
static extern bool OpenPrinter(
[MarshalAs(UnmanagedType.LPTStr)] string printerName,
out IntPtr printerHandle, IntPtr printerDefaults);

Nov 17 '05 #3
Oh, crap. I just saw that you said your library _returns_ CStrings, not
accepts them. Sorry for the brain-dead post. :-(

Nov 17 '05 #4
Thanks. I went with what you posted and that worked well.
"James Curran" <ja*********@mvps.org> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I don't think there's anyway to get C# to recognize a CString. I'd ssay your best bet is to write a wrapper for the C++ library in Managed C++,
which will convert the CStrings to System.Strings

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Stephen Engle" <en***********@aoins.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
I am attempting to write a C# program that accesses a C++ library. The
library has functions that return CStrings. So far I have not been able

to
find a way to declare the function prototype in C# such that it will work.
For instance, I have tried declaring the function as a string type but

when
I call the function, it errors out with the following:

An unhandled exception of type 'System.NullReferenceException' occurred in TestDLM.exe

Additional information: Object reference not set to an instance of an
object.

I have tried a couple other declarations with no better results.

Does anyone have any advice?

Thanks.


Nov 17 '05 #5

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

Similar topics

11
by: Rajesh Garg | last post by:
Why and in what situations is CString not preferred? RVG
6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
0
by: vsaraog | last post by:
Hi y'all, I am using a Created Global Temporary Table in DB2 OS/390 version 7.1.2. I am inserting some data in it and then updating the table using ODBC but while I try to update it, I am...
0
by: clara | last post by:
while parsing file i am getting square box at starting but next line it parse correctly this is my code don know hown to solve that. i cant use CStdioFile it wont support for windows ce ...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
4
by: Susan Rice | last post by:
I'm new to using CString. Why won't the following compile? I'm using Microsoft Visual C++ 6.0 Line 37 which it complains about is the function: 37 CString ConvertFile(char *szFileName) I...
2
by: flyingxu | last post by:
Hi, I run into a cstring related link problem in VC7. My solution has 3 projects, one MFC exe, two MFC extersion DLL. the two MFC extersion DLL export functions which use CString as parameters....
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.