473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshal::String ToHGlobalAnsi 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::String ToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?

Thanks in advance.

Mar 27 '07 #1
4 4401
"DaTurk" <mm******@hotma il.comwrote in message
news:11******** **************@ l77g2000hsb.goo glegroups.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::String ToHGlobalAnsi, but I'm a
little worried about the overhead. Any ideas?
If you don't plan to modify the strings you can use PtrToStringChar s():

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******@hotma il.comwrote in message
news:11******** **************@ l77g2000hsb.goo glegroups.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::String ToHGlobalAnsi, 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<Sys tem::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.no spamwrote:
"DaTurk" <mmagd...@hotma il.comwrote in message

news:11******** **************@ l77g2000hsb.goo glegroups.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::String ToHGlobalAnsi, 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<Sys tem::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
2694
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 mixed mode DLL unmanaged code is written in unmanaged C++ & resides in an unmanaged DL Product used is VS.NET 200 Please tell me what is the best practice for getting and setting data fro unmanaged to managed part when I have a class with...
9
6214
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 these handles, it does not work, and GetLastError returns ERROR_INVALID_HANDLE. But when I CloseHandle() them, it succeeds! I tested this in VC++ and VBA, and GetStdHandle(-11) always returns 7, which is valid. There is the same problem with...
24
17485
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 managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
5
1447
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 string 3", "abcdef string 4",
1
1313
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 of my String. I want to pass it as constant (LPCWSTR). I don't need allocation and copying, just the pointer. Can I have it, pretty please? In normal C++, we usually have conversion operator or a function to get it from a string class...
1
5254
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 button1_Click(object sender, System.EventArgs e) { IntPtr p1 = new IntPtr(-1); string inputStr;
14
12645
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 dont have this in normal). So i decided to use the EnumWindows with SendMessage. What i try here is to pass a WM_USER+1111 with LParam and WParam, pointing to strings. But when i try to get the strings at the other end with Marshal.PtrToStringAnsi,
0
1797
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 the dotnet.framework.interop NG but feel free to chage it as appopriate. I have a question regarding converting a Managed System.String^ to unmanaged const char*. I inherited a code that does it like this: String^ s = "00000";
2
2308
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: // based on http://msdn.microsoft.com/en-us/magazine/bb985936.aspx std::string MarshalString (const System::String^ s) {
0
8840
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
9367
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
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
8007
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6669
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
5981
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();...
1
3189
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
2130
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.