CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0);在一个EXE中,调用没问题
我现在在一个DLL中使用了同样函数,却返回错误,GetLastError为2
请问如何解决?

解决方案 »

  1.   

    BOOL   Encrypt(char* szPin, BYTE* szCode, DWORD dwSize)
    {
    char code[32];
    DWORD size = 32;
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    BOOL result = FALSE;
    __try
    {
    if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    __leave; if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
    __leave; if (!CryptHashData(hHash, (const BYTE*)LPCTSTR(szPin), strlen(szPin), 0))
    __leave; if (!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)code, &size, 0))
    __leave; memcpy(szCode, code, min(size, dwSize));
    result = TRUE;
    }
    __finally
    {
    if (hHash)
    CryptDestroyHash(hHash);
    if (hProv)
    CryptReleaseContext(hProv, 0);
    }
    return result;
    }
      

  2.   

    this is my code ,maybe help you,and do you define 
    #define _WIN32_WINNT 0x0400
      

  3.   

    You can look at the following knowledge base article about the flags and 
    its use in CryptAcquireContext() API callQ238187 - INFO: Understanding CryptAcquireContext() Use
    http://support.microsoft.com/support/kb/articles/Q238/1/87.aspIf you are using session keys to encrypt/decrypt, make one Crypto API call 
    like
     
    if (!CryptAcquireContext(&hProv, NULL,MS_DEF_PROV, PROV_RSA_FULL, 
    CRYPT_VERIFYCONTEXT))
    {
    }
     
    This should resolve your problem and it is the recommended method if you 
    are using session keys for encryption/decryption.
      

  4.   

    The following table lists the error codes most commonly returned by the GetLastError function.Error code Description 
    ERROR_INVALID_PARAMETER One of the parameters contains an invalid value. This is most often an illegal pointer. 
    ERROR_NOT_ENOUGH_MEMORY The operating system ran out of memory during the operation. 
    NTE_BAD_FLAGS The dwFlags parameter has an illegal value. 
    NTE_BAD_KEYSET The registry entry for the key container could not be opened and may not exist. 
    NTE_BAD_KEYSET_PARAM The pszContainer or pszProvider parameter is set to an illegal value. 
    NTE_BAD_PROV_TYPE The value of the dwProvType parameter is out of range. All provider types must be from 1 to 999, inclusive. 
    NTE_BAD_SIGNATURE The provider DLL signature could not be verified. Either the DLL or the digital signature has been tampered with. 
    NTE_EXISTS The dwFlags parameter is CRYPT_NEWKEYSET, but the key container already exists. 
    NTE_KEYSET_ENTRY_BAD The registry entry for the pszContainer key container was found (in the HKEY_CURRENT_USER window), but is corrupt. See System Administration for details about the CryptoAPI registry usage. 
    NTE_KEYSET_NOT_DEF No registry entry exists in the HKEY_CURRENT_USER window for the key container specified by pszContainer. 
    NTE_NO_MEMORY The CSP ran out of memory during the operation. 
    NTE_PROV_DLL_NOT_FOUND The provider DLL file does not exist or is not on the current path. 
    NTE_PROV_TYPE_ENTRY_BAD The registry entry for the provider type specified by dwProvType is corrupt. This error may relate to either the user default CSP list or the machine default CSP list. See System Administration for details about the CryptoAPI registry usage. 
    NTE_PROV_TYPE_NO_MATCH The provider type specified by dwProvType does not match the provider type found in the registry. Note that this error can only occur when pszProvider specifies an actual CSP name. 
    NTE_PROV_TYPE_NOT_DEF No registry entry exists for the provider type specified by dwProvType. 
    NTE_PROVIDER_DLL_FAIL The provider DLL file could not be loaded, and may not exist. If it exists, then the file is not a valid DLL. 
    NTE_SIGNATURE_FILE_BAD An error occurred while loading the DLL file image, prior to verifying its signature. 
      

  5.   

    because:
    #define ERROR_FILE_NOT_FOUND             2L
    so;
    why your GetLastError() return value is 2 ?????
      

  6.   

    thanks 我也碰到这个问题了,没想到在这里找到答案了.