Convert from __GC String to const WCHAR
-
Hello All, I trying to call an unmanged code function from managed code and the unmanaged code function requires a type of WCHAR. How can I convert a __GC String to type of WCHAR so it be passed to the function. If I type out the value, such as const WCHAR *pdn = L"Some value"; It works; however I need the "Some value" to be passed in as a variable. By the way this unmanged code is in unicode, if that helps. Thanks, DC
-
Hello All, I trying to call an unmanged code function from managed code and the unmanaged code function requires a type of WCHAR. How can I convert a __GC String to type of WCHAR so it be passed to the function. If I type out the value, such as const WCHAR *pdn = L"Some value"; It works; however I need the "Some value" to be passed in as a variable. By the way this unmanged code is in unicode, if that helps. Thanks, DC
// From Extending MFC Applications with the .NET Framework
#include //...
String* s = S"Hello";
const __wchar_t __pin * str = PtrToStringChars(s);Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework
-
// From Extending MFC Applications with the .NET Framework
#include //...
String* s = S"Hello";
const __wchar_t __pin * str = PtrToStringChars(s);Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework
Thanks, When I try this I get the following: cannot convert parameter 1 from 'const wchar_t __pin *volatile ' to 'WCHAR *' Conversion loses qualifiers: Here is the code Unmanaged Code UserStatus* WaitForChanges(WCHAR* dn) { UserStatus* status = new UserStatus(); MonitorLDAPChanges(pdn); return status; } Managed Code const __wchar_t __pin * str = PtrToStringChars(dn); WaitForChanges(str);
-
Thanks, When I try this I get the following: cannot convert parameter 1 from 'const wchar_t __pin *volatile ' to 'WCHAR *' Conversion loses qualifiers: Here is the code Unmanaged Code UserStatus* WaitForChanges(WCHAR* dn) { UserStatus* status = new UserStatus(); MonitorLDAPChanges(pdn); return status; } Managed Code const __wchar_t __pin * str = PtrToStringChars(dn); WaitForChanges(str);
I got it I forgot to put the 'const' in the method.
-
I got it I forgot to put the 'const' in the method.
DR Clevenger wrote: I got it I forgot to put the 'const' in the method Yep :) Here's the thing. The
__pin
keyword is used to indicate that the value should not be moved in memory. This is done because a .NETString
is subject to being moved about by the GC and when you convert that data into aWCHAR*
for purposes of using that pointer over any length of time, you need to ensure that the data is not moved. TheWCHAR*
is also defined asconst
becauseString
objects are immutable. Anyway, you probably know this, but I wanted to give you a complete answer on what is going on here just in case. Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET Framework -
DR Clevenger wrote: I got it I forgot to put the 'const' in the method Yep :) Here's the thing. The
__pin
keyword is used to indicate that the value should not be moved in memory. This is done because a .NETString
is subject to being moved about by the GC and when you convert that data into aWCHAR*
for purposes of using that pointer over any length of time, you need to ensure that the data is not moved. TheWCHAR*
is also defined asconst
becauseString
objects are immutable. Anyway, you probably know this, but I wanted to give you a complete answer on what is going on here just in case. Cheers, Tom Archer * Inside C# -Second Edition * Visual C++.NET Bible * Extending MFC Applications with the .NET FrameworkThanks for your help and the information.