Re: Marshal::StringToHGlobalAnsi and System.AccessViolationException
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Sat, 1 Mar 2008 14:50:39 -0600
"Mihai N." <nmihai_year_2000@xxxxxxxxx> wrote in message news:Xns9A51BD2DAA573MihaiN@xxxxxxxxxxxxxxxx
Leaving aside the fact that there are better ways to accomplish what IIf you know that is better, why not use it?
want (like calling .ToPointer() for example)
========================
String^ managedString = "Hello unmanaged world (from the managed world).";
char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString
).ToPointer();
Marshal::FreeHGlobal(IntPtr(stringPointer));
Don't cast and cast back. Instead:
IntPtr hglobalPtr = Marshal::StringToHGlobalAnsi(managedString);
char* stringPointer = static_cast<char*>(hglobalPtr.ToPointer());
....
Marshal::FreeHGlobal(hglobalPtr);
Official example at http://msdn2.microsoft.com/en-
us/library/system.runtime.interopservices.marshal.stringtohglobalansi.aspx
==================
LPCSTR str = static_cast<LPCTSTR>(const_cast<void*>(static_cast<constPotential problem here: why static_cast to LPCTSTR if what you want is
LPCSTR? The 'T' in LPCTSTR means that when compiled as Unicode the type
is really wchar_t const *, while LPCSTR is char const *
So you basically do char const * str = static_cast<wchar_t const *>(...)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
.
- Prev by Date: Re: Redirecting sdtin, stdout, stderr from an already running process
- Next by Date: Re: Redirecting sdtin, stdout, stderr from an already running process
- Previous by thread: Re: Redirecting sdtin, stdout, stderr from an already running process
- Next by thread: Re: Marshal::StringToHGlobalAnsi and System.AccessViolationException
- Index(es):
Relevant Pages
|