C2440 Error with SSPI

I have project dependent on "SSPI.h".
However, during the conversion to the .NET 2.0 Framework,
when I try to complie "SSPI.h", I get the following error.

error C2440: '=' : cannot convert from '__const_Char_ptr' to 'wchar_t __pin *' 

Following is the piece of code which gives error:

wchar_t __pin* pwszServerPrincipalName = NULL;

if ((credential->SecurityPackage == Credential::Package::Kerberos) || (credential->SecurityPackage == Credential::Package::Negotiate))

pwszServerPrincipalName = PtrToStringChars(serverPrincipalName);

I tried out by changing Project properties->C/C++->Language->Treat wchar_t as BuiltInType to "No (/Zc:wchar_t-)" and comiling again. But still the problem is there.
Any suggestions how to proceed.



Answer this question

C2440 Error with SSPI

  • xRuntime

    Hi Kevin,

    Thanks a lot. After using - (SEC_WCHAR*)pwszServerPrincipalName I got below error:

    Error 3 error LNK2001: unresolved external symbol " .cctor@@$$FYMXXZ" ( .cctor@@$$FYMXXZ) SSPI.obj

    But adding "msvcmrt.lib" in linkers list solved the problem.
    To add it in the list I used the following option Project properties->Configuration Properties->Linkers->Input->Additional Dependencies.

    Thanks again. I was stuck up with the issue from last one week.

    HV


  • RPalmer

    I fixed the previews erros, but i have these error when i try to compile in Visual Studio 2005.
    any suggestion

    ------ Build started: Project: Microsoft.Samples.Security.SSPI, Configuration: Debug Win32 ------
    Compiling...
    SSPI.cpp
    c:\sspi\microsoft\samples\security\sspi\sspi\SSPI.h(326) : error C2838: 'Client' : illegal qualified name in member declaration
    c:\sspi\microsoft\samples\security\sspi\sspi\SSPI.h(347) : error C2838: 'Server' : illegal qualified name in member declaration
    Build log was saved at "file://c:\SSPI\Microsoft\Samples\Security\SSPI\SSPI\Debug\BuildLog.htm"
    Microsoft.Samples.Security.SSPI - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • Wyatt Earp

    Try (SEC_WCHAR*)pwszServerPrincipalName instead - the trouble isn't the __pin, it's the const.

    Hope that helps

    Kevin Frei
    Visual C++

  • GILLT

    You're missing a const:

        __wchar_t __pin * str1 = NULL;
        if (argc)
            str1 = PtrToStringChars(str);


    gives me the same error.  But:

        const __wchar_t __pin * str1 = NULL;
        if (argc)
            str1 = PtrToStringChars(str);

    works fine.

    Hope that helps!

    Kevin Frei
    Visual C++

  • JamesPMiller

    Thanks,

    This post saved my day.

    -Sada


  • Lorry Craig

    Lagear, Look for

    Code Snippet
    Package::Client

    and
    Code Snippet
    Package::Server

    and change them to
    Code Snippet
    CredentialType::Client

    and
    Code Snippet
    CredentialType::Server

    .

    Cheers,

    Todd.


  • abhishek thakur

    .NET 2.0 actually has a class called NegotiateStream which does all the authentication for you.
    However, I'm trying to use a .NET 2.0 server authenticate a non .NET client and NegotiateStream didn't seem to work. Now I'm trying to get this .NET 1.0 solution to work in 2.0 and this post was a great help.

  • exulted

    Hi Kevin,

    Thank you for the solution.
    After giving a const, C2440 error is resolved but it gives me another set of error.
    It is as below:

    error C2664: 'InitializeSecurityContextW' : cannot convert parameter 3 from 'const wchar_t __pin *' to 'SEC_WCHAR *'

    Code changes I made in SSPI.h is as below:

    const wchar_t __pin*  pwszServerPrincipalName = NULL;
                if ((credential->SecurityPackage == Credential::Package::Kerberos) || (credential->SecurityPackage == Credential::Package::Negotiate))
                pwszServerPrincipalName = PtrToStringChars(serverPrincipalName); 
                          
         
    SECURITY_STATUS sResult = InitializeSecurityContext(
        phCredential,         
        this->contextHandle,       
        pwszServerPrincipalName,       // [in] name of the target of the context. 
        reqContextAttributes,           SECURITY_NATIVE_DREP,        // 
        &inBuffDesc,             
        this->contextHandle,       
        &outBuffDesc,    
        pulContextAttributes,
         &tsLifeSpan );

    InitializeSecurityContext Declaration:

    SECURITY_STATUS SEC_ENTRY
    InitializeSecurityContextW(
        PCredHandle phCredential,             
        PCtxtHandle phContext,                 
    #if ISSP_MODE == 0
        PSECURITY_STRING pTargetName,
    #else
        SEC_WCHAR SEC_FAR * pszTargetName,      // Name of target
    #endif
        unsigned long fContextReq,             
        unsigned long Reserved1,              
        unsigned long TargetDataRep,          
        PSecBufferDesc pInput,                 
        unsigned long Reserved2,              
        PCtxtHandle phNewContext,              
        PSecBufferDesc pOutput,                
        unsigned long SEC_FAR * pfContextAttr, 
        PTimeStamp ptsExpiry                   
        );

    Here what happens is  "pwszServerPrincipalName" is a const wchar_t __pin*, it is further used in  "InitializeSecurityContext" and the structure of "InitializeSecurityContext" has 3rd parameter of type "SEC_WCHAR SEC_FAR * pszTargetName". Thus it gives C2664 error,"cannot convert const wchar_t__pin* to SEC_WCHAR *". I have highlighted the code which gives error. I tried giving (wchar_t __pin*)pwszServerPrincipalName. But it's not working fine.
    Please suggest me what can be done to resolve the C2664 error.

    HV


  • C2440 Error with SSPI