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++ / September 2007

Tip: Looking for answers? Try searching our database.

String^

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dragonslayer008@hotmail.com - 19 Sep 2007 05:47 GMT
I need to expose a String property, so I have:

property String^ Filename;

in my ref class.  Now, in a member function, I need to call a native
function that takes a std::wstring parameter.  Of course, the compiler
is complaining of me trying to convert String^ to std::wstring.

What is the solution?
David Lowndes - 19 Sep 2007 09:25 GMT
>....  Now, in a member function, I need to call a native
>function that takes a std::wstring parameter.  Of course, the compiler
>is complaining of me trying to convert String^ to std::wstring.
>
>What is the solution?

I use a function like this:

static void SystemStringToStdString(String ^ SystemString, wstring &
StdString)
{
    IntPtr ip = Marshal::StringToHGlobalUni( SystemString );
    const wchar_t * ps = static_cast<const wchar_t
*>(ip.ToPointer());
    StdString = ps;
    Marshal::FreeHGlobal( ip );
}

There are some built-in conversions facilities in VS2008 but when I
tried them they were less efficient that the above method.

Dave
Jochen Kalmbach [MVP] - 19 Sep 2007 17:30 GMT
Hello David!
>> ....  Now, in a member function, I need to call a native
>> function that takes a std::wstring parameter.  Of course, the compiler
>> is complaining of me trying to convert String^ to std::wstring.

> static void SystemStringToStdString(String ^ SystemString, wstring &
> StdString)
[quoted text clipped - 5 lines]
>     Marshal::FreeHGlobal( ip );
> }

This solution is "overkill"...

Please use simply "PtrToStringChars"... this is the fastest method...

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

David Lowndes - 19 Sep 2007 18:28 GMT
>This solution is "overkill"...
>
>Please use simply "PtrToStringChars"... this is the fastest method...

True - and I knew it too having posed the same question elsewhere. I'm
getting a short memory these days :(

Dave

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.