473,387 Members | 1,650 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,387 software developers and data experts.

Marshal::StringToHGlobalAnsi and the corresponding overhead

Hi,

I have a c# GUI that needs to untimately send its data down to
unmanaged c++. I've decided to do this by having a ref struct in the
CLI layer and have the c# populate this, and then pass it back to the
CLI layer. The CLI layer will then populate the unmanged struct where
the data needs to end up.

Now, my question, I just realized that I'm going to have alot of
strings. I need to have these things end up as char*, but they start
as String^. I'm just thinking about overhead, I was thinking about
Marshalling the strings via Marshal::StringToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?

Thanks in advance.

Mar 27 '07 #1
4 4379
"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Now, my question, I just realized that I'm going to have alot of
strings. I need to have these things end up as char*, but they start
as String^. I'm just thinking about overhead, I was thinking about
Marshalling the strings via Marshal::StringToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?
If you don't plan to modify the strings you can use PtrToStringChars():

http://blogs.msdn.com/slippman/archi...02/147090.aspx

IMO, it's only by profiling that you'll be able to determine if your worries
are warranted or if one method is materially better than another.

Regards,
Will
Mar 27 '07 #2

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Hi,

I have a c# GUI that needs to untimately send its data down to
unmanaged c++. I've decided to do this by having a ref struct in the
CLI layer and have the c# populate this, and then pass it back to the
CLI layer. The CLI layer will then populate the unmanged struct where
the data needs to end up.

Now, my question, I just realized that I'm going to have alot of
strings. I need to have these things end up as char*, but they start
as String^. I'm just thinking about overhead, I was thinking about
Marshalling the strings via Marshal::StringToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?
Most of the overhead there is in Unicode->ANSI conversion. If the client
can use byte[] (cli::array<System::Byte>^) instead, it will be faster.
Depends on whether it acts like character strings or raw byte data.
>
Thanks in advance.

Mar 28 '07 #3
On Mar 28, 3:47 pm, "Ben Voigt" <r...@nospam.nospamwrote:
"DaTurk" <mmagd...@hotmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Hi,
I have a c# GUI that needs to untimately send its data down to
unmanaged c++. I've decided to do this by having a ref struct in the
CLI layer and have the c# populate this, and then pass it back to the
CLI layer. The CLI layer will then populate the unmanged struct where
the data needs to end up.
Now, my question, I just realized that I'm going to have alot of
strings. I need to have these things end up as char*, but they start
as String^. I'm just thinking about overhead, I was thinking about
Marshalling the strings via Marshal::StringToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?

Most of the overhead there is in Unicode->ANSI conversion. If the client
can use byte[] (cli::array<System::Byte>^) instead, it will be faster.
Depends on whether it acts like character strings or raw byte data.


Thanks in advance.- Hide quoted text -

- Show quoted text -
That's interesting, I didn't think about that. There will still have
to be someconversion at some point as the information I'm going to be
using is going to be coming from a WinForm, so it will be of the type
String^ at the presentation layer. SO I huess the question then
becomes, would it be more costly to convert a String^ -a byte[] at
the presentation layer, or to convert a String^ -a char* down lower
in the program?

Mar 30 '07 #4
That's interesting, I didn't think about that. There will still have
to be someconversion at some point as the information I'm going to be
using is going to be coming from a WinForm, so it will be of the type
String^ at the presentation layer. SO I huess the question then
becomes, would it be more costly to convert a String^ -a byte[] at
the presentation layer, or to convert a String^ -a char* down lower
in the program?
I think the Unicode->ANSI conversion may be implemented differently in .NET
vs native, so that could be a consideration too. Otherwise, I don't see why
either path would be any faster. I would probably hand a String^ to C++ to
keep the presentation layer free from implementation details.

My basic suggestion was that not all char* start life as strings, some are
more "magic numbers". E.g. in SMTP, the "MAIL TO:" command isn't a string
subject to localization, it has to be those exact bytes. Similarly for HTML
tags and the filetype codes used by a lot of programs (GIF does this).
Yours is user-entered though, so you do need the conversion.
Mar 30 '07 #5

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

Similar topics

0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
9
by: Dragon | last post by:
Hello all, I ran into trouble with GetStdHandle API. When I'm trying to get standard output with GetStdHandle(-11I), it returns strange values such as 1548, 1876. If I try to WriteConsole() into...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
5
by: gs | last post by:
I want to pass a string array to C which uses ansi in a test, I tried: unsafe public int ctstAny1dp(String* strOut) { private string strMyC = { "abcdef string 1", "abcdef string 2", "abcdef...
1
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
1
by: sabys | last post by:
I've been noticing a memory leak with the following sample code snippet. Can someone please advise. Have a C# Winforms app with the following code on a button-click event. private void...
14
by: Kerem Gümrükcü | last post by:
Hi, i want to emulate a (synchronous) BroadCastSystemMessage with EnumWindows and SendMessage. I dont want to use the BroadcastSystemMessage because it needs the SE_TCB_NAME Privilege (you...
0
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to...
2
by: Kenneth Porter | last post by:
I try to be const correct everywhere and found that I can't pass a const String to this API. Is it going to change the value? Or is the signature not the best it could be? I want to write this:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.