Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie question regarding conversion from wchar_t* to unsigned sho

127 views
Skip to first unread message

Anthony Yott

unread,
Oct 31, 2005, 1:14:40 PM10/31/05
to
Folks,

I've inherited some C++ code that I'm trying to convert from VC++ 6.0 to
VC++ 2005 and was wondering if anyone could help as I haven't done any C++
programming in several years.

The code is basically trying to populate a struct that is defined in the
sspi.h file. The compile error that I'm getting is "error C2440:
'initializing' cannot convert from wchar_t* to unsigned short*".

Here is the code to populate the struct:

const wchar_t* pszAuthority
const wchar_t* pszPrincipal
const wchar_t* pszPassword

SEC_WINNT_AUTH_IDENTITY_EX authIdent = {
SEC_WINNT_AUTH_IDENTITY_VERSION,
sizeof authIdent,
const_cast<wchar_t*>(pszPrincipal), //*****Compilie error here*******
lstrlenW(pszPrincipal),
const_cast<wchar_t*>(pszAuthority), //*****Compilie error here*******
lstrlenW(pszAuthority),
const_cast<wchar_t*>(pszPassword), //*****Compilie error here******
lstrlenW(pszPassword),
SEC_WINNT_AUTH_IDENTITY_UNICODE,
0,
0};

The struct is defined as follows in the sspi.h header file

#define SEC_WINNT_AUTH_IDENTITY_EX SEC_WINNT_AUTH_IDENTITY_EXW

typedef struct _SEC_WINNT_AUTH_IDENTITY_EXW {
unsigned long Version;
unsigned long Length;
unsigned short SEC_FAR *User;
unsigned long UserLength;
unsigned short SEC_FAR *Domain;
unsigned long DomainLength;
unsigned short SEC_FAR *Password;
unsigned long PasswordLength;
unsigned long Flags;
unsigned short SEC_FAR * PackageList;
unsigned long PackageListLength;
} SEC_WINNT_AUTH_IDENTITY_EXW, *PSEC_WINNT_AUTH_IDENTITY_EXW;

I guess the question is what is the best way to "cast" a wchar_t* to a
unsigned short* so that it will compile and run correctly? Any help or
feedback would be greatly appreciated.

--
Anthony Yott

Igor Tandetnik

unread,
Oct 31, 2005, 1:28:31 PM10/31/05
to
Anthony Yott <antho...@hotmail.com> wrote:
> I've inherited some C++ code that I'm trying to convert from VC++ 6.0
> to VC++ 2005 and was wondering if anyone could help as I haven't done
> any C++ programming in several years.
>
> The code is basically trying to populate a struct that is defined in
> the sspi.h file. The compile error that I'm getting is "error C2440:
> 'initializing' cannot convert from wchar_t* to unsigned short*".
>
> Here is the code to populate the struct:
>
> const wchar_t* pszPrincipal

>
> SEC_WINNT_AUTH_IDENTITY_EX authIdent = {
> SEC_WINNT_AUTH_IDENTITY_VERSION,
> sizeof authIdent,
> const_cast<wchar_t*>(pszPrincipal), //*****Compilie error
[snip]

> };
>
> The struct is defined as follows in the sspi.h header file
>
> #define SEC_WINNT_AUTH_IDENTITY_EX SEC_WINNT_AUTH_IDENTITY_EXW
>
> typedef struct _SEC_WINNT_AUTH_IDENTITY_EXW {
> unsigned long Version;
> unsigned long Length;
> unsigned short SEC_FAR *User;
[snip]
> } SEC_WINNT_AUTH_IDENTITY_EXW, *PSEC_WINNT_AUTH_IDENTITY_EXW;

It's arguably a bug in the Platform SDK. For a long time, wchar_t was
not a distinct type but simply a typedef for unsigned short, so sspi.h
header got away with this blatant abuse. Starting with VC7, a compiler
option became available to make wchar_t a predefined fundamental type,
but it was off by default. In VC8 (aka VC 2005) this option is the
default (you can still switch to old behavior with /Zc:wchar_t- but I
won't recommend it).

The easiest solution in the short term is simply to do reinterpret_cast:

reinterpret_cast<unsigned short*>(pszPrincipal),

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


0 new messages