String^

  • Thread starter Thread starter dragonslayer008
  • Start date Start date
D

dragonslayer008

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?
 
.... 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
 
Hello David!
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 );
}

This solution is "overkill"...

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

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
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
 
Back
Top