471,605 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Passing string from C++ code to C#

Hi all,

I am passing string from C++ to C# but not sure which way is more
correct. Here it is:
First approach:
CDAnetCSharpHooks::PassString( )
{
char str[] = "String to pass";
DotNetObject *obj = new DotNetObject();
obj->UseStringInDotNet( str );
}

Second approach:
CDAnetCSharpHooks::PassString( )
{
char str[] = "String to pass";
CString temp(str);
System::String * dotNetStr = new System::String( temp);
DotNetObject *obj = new DotNetObject();
obj->UseStringInDotNet( dotNetStr );
}

C# method
public void UseStringInDotNet( String str )
{
// create new thread and use string in it
}

UseStringInDotNet() is creating new thread and that is why PassString()
will finish its work before UseStringInDotNet() finishes its own.
Basically first approach is working, but sometimes on the .NET side I
have some unpredictable behaviour in memory. I am wondering if that
memory for str buffer is cleared before C# get its work done. In this
respect will be the second approach more safe ?

Thank you very much in advance for any suggestions

Roman

Feb 8 '06 #1
1 2684
Hi,

This actually belongs in microsoft.public.dotnet.framework.interop, but I'll
answer here.

I am not sure system::string can be properly marshalled to the managed
world. As far as I remember it is able to cast itself either to char* or to
wchar_t* (I believe the marshaler can understand both), but I'd better stick
with explicit char*. Even better, use the OLEView tool to examine the C#
server's type library and to find out which flavor of string it expects
(might be BSTR, for example).

Memory management can be nasty. I'd recommend that you check with MSDN re
memory management conventions applicable to the flavor of string you use. It
is quite common for the caller to allocate the memory and for the callee to
free it when no longer needed. Again, please read documentation on COM
interop marshalling carefully.

See here for example:

http://msdn.microsoft.com/library/de...forstrings.asp

and

http://www.develop.com/conferences/c...terials/C8.pdf (pages
7 and 8)

<mu******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi all,

I am passing string from C++ to C# but not sure which way is more
correct. Here it is:
First approach:
CDAnetCSharpHooks::PassString( )
{
char str[] = "String to pass";
DotNetObject *obj = new DotNetObject();
obj->UseStringInDotNet( str );
}

Second approach:
CDAnetCSharpHooks::PassString( )
{
char str[] = "String to pass";
CString temp(str);
System::String * dotNetStr = new System::String( temp);
DotNetObject *obj = new DotNetObject();
obj->UseStringInDotNet( dotNetStr );
}

C# method
public void UseStringInDotNet( String str )
{
// create new thread and use string in it
}

UseStringInDotNet() is creating new thread and that is why PassString()
will finish its work before UseStringInDotNet() finishes its own.
Basically first approach is working, but sometimes on the .NET side I
have some unpredictable behaviour in memory. I am wondering if that
memory for str buffer is cleared before C# get its work done. In this
respect will be the second approach more safe ?

Thank you very much in advance for any suggestions

Roman

Feb 9 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
1 post views Thread by Steven Blair | last post: by
7 posts views Thread by Ken Allen | last post: by
22 posts views Thread by Arne | last post: by
11 posts views Thread by =?Utf-8?B?U3VqZWV0?= | last post: by
6 posts views Thread by Andy Baker | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.