all groups > dotnet interop > december 2006 >
You're in the

dotnet interop

group:

How to pass C# string to Embeded C++ String?


How to pass C# string to Embeded C++ String? vyip
12/19/2006 7:17:38 AM
dotnet interop:
I'm having a little trouble passing a C# string to a Embedded C++ 4.0
CE dll. The regular 'string' from C# would not work. I have also
tried Marshaling, but no luck. Sometimes the Embedded C++ code throw
an exception like
"ExceptionCode: 0xc000005, ExceptionAddress: 0x01e415bc,
Reading:0x0009dc"


Here is Embeded C++ 4.0 dll code in MyEmbedded.dll
---------------------------------------------------------------------------------
#include <string.h>
#define STRING string

extern "C" MY_API STRING ReturnMyStr(STRING MyStr)
{

return MyStr;
}
-------------------------------------------------------------

In C# code, I call this by
-------------------------
[DllImport("MyEmbedded.dll")]
public static extern string ReturnMyStr(string myStr); //This did
not work
--------------------------
///I also try Marshaling etc..
[DllImport("MyEmbedded.dll")]
public static extern string
ReturnMyStr([MarshalAs(UnmanagedType.LPStr)] string myStr); //This did
not work
-----------------------------------------------------------------------------------

I couldn't get it to work. So, how can I pass a C# string to the
embedded C++? I guess Marshalling should work, maybe I did something
wrong. Any help will be very appriciated.

Thanks.
- vyip
Re: How to pass C# string to Embeded C String? Divya Jain
1/5/2007 5:10:04 AM
try using
[DllImport("MyEmbedded.dll")]
public static extern string
ReturnMyStr([MarshalAs(UnmanagedType.LPStr)] StringBuilder myStr);
It will work.
---
AddThis Social Bookmark Button