Type conversion error

I have multiple projects that depend on SSPI.h (taken from MSDN code samples). However, during the conversion to the .NET 2.0 Framework, this file is generating a type conversion error and I am not sure how to fix it. This is the error where __const_Char_ptr is typedefined asSystem::Char*
error C2440: '=' : cannot convert from '__const_Char_ptr' to 'wchar_t __pin *'

This is the code snippit:
wchar_t __pin* pwszServerPrincipalName = NULL;
if ((credential->SecurityPackage == Credential::Package::Kerberos) || (credential->SecurityPackage == Credential::Package::Negotiate))
pwszServerPrincipalName =PtrToStringChars(serverPrincipalName);

(Obviously, PtrToStringChars is of type __const_Char_ptr)
Any suggestions?
[1195 byte] By [JenniferGamble] at [2008-2-18]
# 1
Have you tried simply typecasting it by going
pwszServerPrincipalName = (wchar_t __pin *) PtrToStringChars(serverPrincipalName);
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 2
Thanks, but I tried that and it generates a whole new list of errors and warnings:Sad
warning C4197: 'wchar_t __pin *' : top-level volatile in cast is ignored
error C3834: illegal explicit cast to a pinning pointer; use a pinned local variable instead
warning C4303: C-style cast from '__const_Char_ptr' to 'wchar_t __pin *' is deprecated, use static_cast, __try_cast or dynamic_cast


I will check out these deprecated options. Meanwhile, any further suggestions would be appreciated.
JenniferGamble at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 3
I don't understand!!! This file compiles no problem in Visual Studio 2003. Why does it get this conversion error when I compile it in Visual Studio 2005? What has changed? I cannot find any info on Google. I need to be able to convert my projects to 64-bit.
JenniferGamble at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 4
It's a little weird. If you can make a small repro that I can take a look at, that would be great.

In the meantime, go to Properties->C/C++->Language->Treat wchar_t as Built-in Type and change that to "No" (same as /Zc:wchar_t-).

Thanks,

Kang Su Gatlin
Visual C++ Program Manager

KangSuGatlin at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 5
Thanks for the temp fix.
As it turns out, System::Char* is a constant, so merely adding the const keyword solved the problem. Smile
const wchar_t __pin* constpwszServerPrincipalName = PtrToStringChars(serverPrincipalName);
Apparently VS 2005 is more sensitive to this than VS 2003.
JenniferGamble at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 6
Hi Jennifer,

I am facing the same C2440 error.
I tried out the solution which you used but it resulted in another set of issues.
Any suggestion for how should I proceed.

HV

Hiral at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 7
This is actually what our development team worked out to solve this problem.
Reason:

The API method PtrToStringChars returns a constant pointer to characters but the existing code tries to cast it to a non-constant value.


Solution:

Use Marshal::StringToHGlobalUni instead.Need to use the namespace System::Runtime::InteropServices and free the memory that is allocated by Marshal::StringToHGlobalUni using Marshal::FreeHGlobal


Code Snippit:
// get a pinned reference to types appropriate for calling unmanaged methods
ULONG __pin *pulContextAttributes = &this->contextAttributes;
wchar_t __pin* pwszServerPrincipalName = NULL;
if ((credential->SecurityPackage == Credential::Package::Kerberos) || (credential->SecurityPackage == Credential::Package::Negotiate))
pwszServerPrincipalName = (wchar_t*)(void*)Marshal::StringToHGlobalUni(serverPrincipalName);
JenniferGamble at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 8
Thank You Jennifer. I will try out the solution.
HV
Hiral at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....

.NET Development

Site Classified