473,695 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting const char * -> System::String^ without gcnew.

Hi,

I have several interfaces in CLI that I access via c#. My problem is,
is that down in the unmanaged c++, which the CLI lies on top of, I
have a lot of c_str() happening. But all of my methods in CLI return
System::String^ . I originally just gcnew'd System::String^ passing in
the c_str(). But I can't really have as many gcnew's as I'm using for
overhead and for fear of leaks.

So my question is this, how can I get the char* coming from c_str()
call to return as a System::String^ without actually calling gcnew?
The reason this is an issue is that these libraries are pushed quite
hard, maybe having string coming through in excess of 100 time a
second and will most likely never be turned off. Any ideas?

THanks in advance.

Feb 22 '07 #1
6 7462
DaTurk wrote:
I originally just gcnew'd System::String^ passing in
the c_str(). But I can't really have as many gcnew's as I'm using for
overhead and for fear of leaks.
There is no leak, System::String^ is garbage collected. Even if you
wanted to, you couldn't control the memory deallocation of managed
strings. It's always managed by the .NET framework.

The overhead is given, you can't avoid that. char* is an unmanaged 8-bit
per character string, while System::String is a managed 16-bit per
character string. At the minimum, the string's value must be copied
between those two classes, you can't avoid that. Even when you assign
one std::string to another, a byte-for-byte copy has to be made. Even
std::string calls malloc and free internally, which are typically slower
than gcnew. When you have enough memory available, gcnew can be as fast
as incrementing a pointer.
So my question is this, how can I get the char* coming from c_str()
call to return as a System::String^ without actually calling gcnew?
You can not.
The reason this is an issue is that these libraries are pushed quite
hard
If the native->unmanaged transition is proven to cause a major
performance problem, you have to write your code 100% managed, or 100%
native. In fact, std::string itself is not nearly as fast as working
with char* directly (see strncpy, itoa, snprintf, etc.).

Tom
Feb 22 '07 #2
>So my question is this, how can I get the char* coming from c_str()
>call to return as a System::String^ without actually calling gcnew?

You can not.
So what if you can't make a String without gcnew, you can do even better.

Require your caller to pass you either a preallocated System::Char[]
(cli::array<wch ar_t>^) or a StringBuilder. Then you needn't reallocate
memory on each call.

But memory allocation in .NET is optimized and should be a very cheap
operation, unless you are allocating large objects in a loop (using
String::Concat iteratively to slowly build a large string is very bad, use a
StringBuilder instead for that sort of thing and preserve a sufficient
Capacity).
If the native->unmanaged transition is proven to cause a major performance
problem, you have to write your code 100% managed, or 100% native. In
fact, std::string itself is not nearly as fast as working with char*
directly (see strncpy, itoa, snprintf, etc.).
But of course you may work directly with a managed array and interior
pointers with no extra overhead, and only a little extra if you need to
pin_ptr it to pass to a native function.
Feb 23 '07 #3
Ben Voigt wrote:
Require your caller to pass you either a preallocated System::Char[]
(cli::array<wch ar_t>^) or a StringBuilder.
That sounds reasonable. It still requires a copy operation, which could
be slower than what you save by eliminating gcnew from the loop.
But of course you may work directly with a managed array and interior
pointers with no extra overhead
I like this idea. So you recommend returning an IntPtr to the char*:

// C++/CLI library:
struct Unmanaged
{
Unmanaged() : some_string("te st") { }
std::string some_string;
};

public ref class Lib
{
public:
Lib() : unmanaged(new Unmanaged()) { }
~Lib() { this->!Lib(); }
!Lib() { delete unmanaged; }
IntPtr GetString() { return &unmanaged->some_string[0]; }
private:
Unmanaged* unmanaged;
};

// C# application:
unsafe void ProcessString()
{
using(Lib lib = new Lib())
{
IntPtr ip = lib.GetString() ;
byte* c = (byte*)ip.ToPoi nter();
// access Unmanaged::some _string's characters directly from C#
}
}

This can only be used with the unsafe keyword and the /unsafe compiler
switch.

Tom
Feb 23 '07 #4

"Tamas Demjen" <td*****@yahoo. comwrote in message
news:ew******** ******@TK2MSFTN GP05.phx.gbl...
Ben Voigt wrote:
>Require your caller to pass you either a preallocated System::Char[]
(cli::array<wc har_t>^) or a StringBuilder.

That sounds reasonable. It still requires a copy operation, which could be
slower than what you save by eliminating gcnew from the loop.
How? gcnew will require the same copy operation, as well as creating an
additional garbage collected object. But the C++ code can most likely work
with unicode directly, and avoid the copy operation.
>
>But of course you may work directly with a managed array and interior
pointers with no extra overhead

I like this idea. So you recommend returning an IntPtr to the char*:
No, I recommended totally avoiding any allocation inside the function, and
having the caller provide an existing buffer, so that one buffer allocation
can serve multiple calls into the C++ code.

Let the caller pass in a (C#) byte[] if working with ASCII data, or a char[]
if working with Unicode. Getting a System::String of the data eventually
involves a new instance, because String objects are immutable -- each
distinct content requires a distinct instance. Of course, you can also try
to share string instances across multiple calls that return the same content
(this also helps future comparison).
Feb 23 '07 #5
On Feb 23, 8:52 am, "Ben Voigt" <r...@nospam.no spamwrote:
"Tamas Demjen" <tdem...@yahoo. comwrote in message

news:ew******** ******@TK2MSFTN GP05.phx.gbl...
Ben Voigt wrote:
Require your caller to pass you either a preallocated System::Char[]
(cli::array<wch ar_t>^) or a StringBuilder.
That sounds reasonable. It still requires a copy operation, which could be
slower than what you save by eliminating gcnew from the loop.

How? gcnew will require the same copy operation, as well as creating an
additional garbage collected object. But the C++ code can most likely work
with unicode directly, and avoid the copy operation.
But of course you may work directly with a managed array and interior
pointers with no extra overhead
I like this idea. So you recommend returning an IntPtr to the char*:

No, I recommended totally avoiding any allocation inside the function, and
having the caller provide an existing buffer, so that one buffer allocation
can serve multiple calls into the C++ code.

Let the caller pass in a (C#) byte[] if working with ASCII data, or a char[]
if working with Unicode. Getting a System::String of the data eventually
involves a new instance, because String objects are immutable -- each
distinct content requires a distinct instance. Of course, you can also try
to share string instances across multiple calls that return the same content
(this also helps future comparison).
What about MArshaling? What if you Marshal the c_str to a
std::string. Then can't you just do something of the nature String^
test = std::string test?

Feb 23 '07 #6

"DaTurk" <mm******@hotma il.comwrote in message
news:11******** *************@8 g2000cwh.google groups.com...
On Feb 23, 8:52 am, "Ben Voigt" <r...@nospam.no spamwrote:
>"Tamas Demjen" <tdem...@yahoo. comwrote in message

news:ew******* *******@TK2MSFT NGP05.phx.gbl.. .
Ben Voigt wrote:
>Require your caller to pass you either a preallocated System::Char[]
(cli::array<wc har_t>^) or a StringBuilder.
That sounds reasonable. It still requires a copy operation, which could
be
slower than what you save by eliminating gcnew from the loop.

How? gcnew will require the same copy operation, as well as creating an
additional garbage collected object. But the C++ code can most likely
work
with unicode directly, and avoid the copy operation.
>But of course you may work directly with a managed array and interior
pointers with no extra overhead
I like this idea. So you recommend returning an IntPtr to the char*:

No, I recommended totally avoiding any allocation inside the function,
and
having the caller provide an existing buffer, so that one buffer
allocation
can serve multiple calls into the C++ code.

Let the caller pass in a (C#) byte[] if working with ASCII data, or a
char[]
if working with Unicode. Getting a System::String of the data eventually
involves a new instance, because String objects are immutable -- each
distinct content requires a distinct instance. Of course, you can also
try
to share string instances across multiple calls that return the same
content
(this also helps future comparison).

What about MArshaling? What if you Marshal the c_str to a
std::string. Then can't you just do something of the nature String^
test = std::string test?
One, a std::string is not compatible in any way with a System::String^ , the
conversion is first to char*. Secondly, Marshaling is an expensive
operation that requires multiple copies, and would only be appropriate for
interprocess communication. With C++/CLI, the managed and unmanaged code
share the same process, the same memory space, even the same thread, so
marshalling is definitely not needed.
Feb 23 '07 #7

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

Similar topics

5
9401
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts. If i make another constructor with parameter const char*, how can i call the constructor with the const string& ? I tried
1
1743
by: yccheok | last post by:
Hi, consider the following bug at #include <stdio.h> class Drink { public: Drink( char *name_ ) : name( name_ ) {} char *name; char & get(int i) { return name; } };
4
2797
by: sam | last post by:
Hi, Is there any way I can prevent people use some binary disambler (eg. strings in unix) to view the const string value in a compiled C++ program? Sam.
2
2543
by: Geoffrey | last post by:
Hello, Is it possible to have a constant String initialised with special character like that = (Char)0x02 + "CMD" + (Char)0x03 ? Thx
5
8827
by: Jae | last post by:
Real(const string &fileName) { FILE * myInputFile = fopen(fileName, "rt"); ..... fclose(myInputFile);
41
2365
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
7
2581
by: Zytan | last post by:
I am trying to set a const string made up of Chr(), but if the value passed into Chr() is too large, the compiler complains that it is not constant!! Example: Public Const m_Data As String = Chr(&HD) & Chr(&HE) 'ok Public Const m_Data As String = Chr(&HD0) & Chr(&HE0) 'not ok But, Char stores 0..255, so it should be ok in both cases. What's going on?
2
10114
by: wizofaus | last post by:
Given the following code: public class Test { static unsafe void StringManip(string data) { fixed (char* ps = data) ps = '$'; }
6
2865
by: Toe001 | last post by:
Can you please help me understand why I do not see the answer expected (Parity: Odd). Can I have an array of strings as constants? How do I index each string in the array? const Parity = {'None', 'Odd', 'Even'}; char string; sprintf(string, "Parity: %s", Parity); printf(string); Thanks
13
3734
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would like to remove the weekday information that is "Wed" here, and I also would like to replace the spaces char by "_" and also remove the "\n" char. I didn't know how to truncate the string from beginning or replace some chars in a string with another...
0
8640
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8860
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3018
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.