Getting a CString

  • Thread starter Thread starter Stephen Engle
  • Start date Start date
S

Stephen Engle

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.
 
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);
 
Oh, crap. I just saw that you said your library _returns_ CStrings, not
accepts them. Sorry for the brain-dead post. :-(
 
Thanks. I went with what you posted and that worked well.


James Curran said:
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 said:
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.
 
Back
Top