473,395 Members | 1,504 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,395 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 2953
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Steven Blair | last post by:
Hi, I am having problem passing a string by reference to some C++ code. Wondered if anyone could help me out. C# code: myFunc(str);
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
3
by: James Robertson | last post by:
I am new to the ASP and VB thing so be kind. Question I have is that I have created an ASPX web site to use as an E-Mail page. But I want to use this for a lot of users. Can I create the link on...
11
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.