Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

Marshal::StringToHGlobalAnsi and the corresponding overhead

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaTurk - 27 Mar 2007 16:22 GMT
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.
William DePalo [MVP VC++] - 27 Mar 2007 17:57 GMT
> 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/archive/2004/06/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
Ben Voigt - 28 Mar 2007 21:47 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> 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.
DaTurk - 30 Mar 2007 20:46 GMT
> > Hi,
>
[quoted text clipped - 17 lines]
>
> - 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?
Ben Voigt - 30 Mar 2007 20:56 GMT
> 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
[quoted text clipped - 3 lines]
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.