vc6。0
代码拷贝自msdn//--------------------------------------------------------------------
//   In this and all other sample and example code, 
//   use the #define and #include statements listed 
//   under #includes and #defines.#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void HandleError(char *s);void main(void) 

//--------------------------------------------------------------------
// Declare and initialize variables.HCRYPTPROV hCryptProv;        // Handle for the 
                              // cryptographic provider context.
HCRYPTKEY hKey;               // Public/private key handle.
CHAR szUserName[100];         // Buffer to hold the name 
                              // of the key container.
DWORD dwUserNameLen = 100;    // Length of the buffer.
LPCSTR UserName= NULL;        // Optionally enter the user's name here
                              // to be used as the key container
                              // name (limited to 100 characters).
//--------------------------------------------------------------------
// Begin processing. Attempt to acquire a context with a default key
// container.//  To create a new key container, substitute a string for the
//  NULL second parameter here and in the next call to 
//  CryptAcquireContext. if(CryptAcquireContext(
   &hCryptProv,               // Handle to the CSP
   UserName,                  // Container name 
   NULL,                      // Use the default provider
   PROV_RSA_FULL,             // Provider type
   0))                        // Flag values
{
    printf("A crypto context with the %s key container \n", UserName);
    printf("has been acquired.\n\n");
}
else

//--------------------------------------------------------------------
// Some sort of error occurred in acquiring the context. 
// Create a new default key container.    if(CryptAcquireContext(
      &hCryptProv, 
      UserName, 
      NULL, 
      PROV_RSA_FULL, 
      CRYPT_NEWKEYSET)) 
   {
      printf("A new key container has been created.\n");
   }
   else
   {
      HandleError("Could not create a new key container.\n");
    }
} // End of else
//--------------------------------------------------------------------
// A cryptographic context with a key container is available. Get the
// name of the key container. 
if(CryptGetProvParam(
    hCryptProv,               // Handle to the CSP
    PP_CONTAINER,             // Get the key container name 
    (BYTE *)szUserName,       // Pointer to the key container name
    &dwUserNameLen,           // Length of name, preset to 100
    0)) 
{
    printf("A crypto context has been acquired and \n");
    printf("The name on the key container is %s\n\n",szUserName);
}
else
{
    // Error getting key container name
    HandleError("A context was acquired or created, but\
      an error occurred getting the key container name.\n");
} //--------------------------------------------------------------------
// A context with a key container is available,
// Attempt to get the handle to key exchange key. if(CryptGetUserKey(
   hCryptProv,                     // Handle to the CSP
   AT_SIGNATURE,                   // Key specification
   &hKey))                         // Handle to the key
{
    printf("A signature key is available.\n");
}
else
{
    printf("No signature key is available.\n");
    if(GetLastError() == NTE_NO_KEY) 
    {
    //----------------------------------------------------------------
    // The error was that there is a container but no key.    // Create a signature key pair. 
       printf("The signature key does not exist.\n");
       printf("Create a signature key pair.\n"); 
       if(CryptGenKey(
          hCryptProv,
          AT_SIGNATURE,
          0,
          &hKey)) 
       {
          printf("Created a signature key pair.\n");
       }
       else
       {
          HandleError("Error occurred creating a signature key.\n"); 
       }
    }
    else
    {
        HandleError("An error other than NTE_NO_KEY getting signature\
            key.\n");
    }
} // End of ifprintf("A signature key pair existed, or one was created.\n\n");
CryptDestroyKey(hKey); // Next, check the exchange key. 
if(CryptGetUserKey(
   hCryptProv,
   AT_KEYEXCHANGE,
   &hKey)) 
{
   printf("An exchange key exists. \n");
}
else
{
     printf("No exchange key is available.\n");
     // Check to see if one needs to be created.
     if(GetLastError()==NTE_NO_KEY) 
     { 
       // Create an key exchange key pair.
       printf("The exchange key does not exist.\n");
       printf("Attempting to create an exchange key pair.\n");
       if(CryptGenKey(
           hCryptProv,
           AT_KEYEXCHANGE,
           0,
           &hKey)) 
       {
           printf("Exchange key pair created.\n");
       }
       else
       {
          HandleError("Error occurred attempting to create \
             an exchange key.\n");
       }
    }
    else
    {
       HandleError("An error other than NTE_NO_KEY occurred.\n");
     }
}printf("An exchange key pair existed, or one was created.\n\n");
CryptDestroyKey(hKey); 
CryptReleaseContext(hCryptProv,0); 
printf("Everything is okay. A signature key\n");
printf("pair and an exchange key exist in\n");
printf("the %s key container.\n",UserName);  
} // End of main//--------------------------------------------------------------------
//  This example uses the function HandleError, a simple error
//  handling function, to print an error message and exit 
//  the program. 
//  For most applications, replace this function with one 
//  that does more extensive error reporting.void HandleError(char *s)
{
    printf("An error occurred in running the program.\n");
    printf("%s\n",s);
    printf("Error number %x\n.",GetLastError());
    printf("Program terminating.\n");
    exit(1);
}

解决方案 »

  1.   

    什么错误?贴出来!有可能是你的sdk版本太低,没有其中的某些函数,或者没有包含某些必要的lib文件,察看这些api函数需要哪些库。如果还不行,就上microsoft网站去升级你的sdk。
      

  2.   

    vc7.0(vs.net)里的错误说明:
    ------ 已启动生成:项目:cry, 配置:Debug Win32 ------正在链接...
    LIBCD.lib(wincrt0.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16 ,该符号在函数 _WinMainCRTStartup 中被引用
    .\Debug/cry.exe : fatal error LNK1120: 1 个无法解析的外部命令生成日志保存在“file://f:\myfile\vc\cry\Debug\BuildLog.htm”中
    cry - 2 错误,0 警告
    ---------------------- 完成 ---------------------    生成:0 已成功, 1 已失败, 0 已跳过
    vc6。0的错误:--------------------Configuration: cry - Win32 Debug--------------------
    Compiling...
    ma.c
    f:\myfile\vc\cry\ma.c(17) : error C2065: 'HCRYPTPROV' : undeclared identifier
    f:\myfile\vc\cry\ma.c(17) : error C2146: syntax error : missing ';' before identifier 'hCryptProv'
    f:\myfile\vc\cry\ma.c(17) : error C2065: 'hCryptProv' : undeclared identifier
    f:\myfile\vc\cry\ma.c(19) : error C2065: 'HCRYPTKEY' : undeclared identifier
    f:\myfile\vc\cry\ma.c(19) : error C2146: syntax error : missing ';' before identifier 'hKey'
    f:\myfile\vc\cry\ma.c(19) : error C2065: 'hKey' : undeclared identifier
    f:\myfile\vc\cry\ma.c(20) : error C2275: 'CHAR' : illegal use of this type as an expression
            e:\microsoft visual studio\vc98\include\winnt.h(134) : see declaration of 'CHAR'
    f:\myfile\vc\cry\ma.c(20) : error C2146: syntax error : missing ';' before identifier 'szUserName'
    f:\myfile\vc\cry\ma.c(20) : error C2065: 'szUserName' : undeclared identifier
    f:\myfile\vc\cry\ma.c(20) : error C2109: subscript requires array or pointer type
    f:\myfile\vc\cry\ma.c(22) : error C2275: 'DWORD' : illegal use of this type as an expression
            e:\microsoft visual studio\vc98\include\windef.h(141) : see declaration of 'DWORD'
    f:\myfile\vc\cry\ma.c(22) : error C2146: syntax error : missing ';' before identifier 'dwUserNameLen'
    f:\myfile\vc\cry\ma.c(22) : error C2065: 'dwUserNameLen' : undeclared identifier
    f:\myfile\vc\cry\ma.c(23) : error C2275: 'LPCSTR' : illegal use of this type as an expression
            e:\microsoft visual studio\vc98\include\winnt.h(167) : see declaration of 'LPCSTR'
    f:\myfile\vc\cry\ma.c(23) : error C2146: syntax error : missing ';' before identifier 'UserName'
    f:\myfile\vc\cry\ma.c(23) : error C2065: 'UserName' : undeclared identifier
    f:\myfile\vc\cry\ma.c(23) : warning C4047: '=' : 'int ' differs in levels of indirection from 'void *'
    f:\myfile\vc\cry\ma.c(34) : warning C4013: 'CryptAcquireContext' undefined; assuming extern returning int
    f:\myfile\vc\cry\ma.c(38) : error C2065: 'PROV_RSA_FULL' : undeclared identifier
    f:\myfile\vc\cry\ma.c(55) : error C2065: 'CRYPT_NEWKEYSET' : undeclared identifier
    f:\myfile\vc\cry\ma.c(67) : warning C4013: 'CryptGetProvParam' undefined; assuming extern returning int
    f:\myfile\vc\cry\ma.c(69) : error C2065: 'PP_CONTAINER' : undeclared identifier
    f:\myfile\vc\cry\ma.c(88) : warning C4013: 'CryptGetUserKey' undefined; assuming extern returning int
    f:\myfile\vc\cry\ma.c(90) : error C2065: 'AT_SIGNATURE' : undeclared identifier
    f:\myfile\vc\cry\ma.c(106) : warning C4013: 'CryptGenKey' undefined; assuming extern returning int
    f:\myfile\vc\cry\ma.c(127) : warning C4013: 'CryptDestroyKey' undefined; assuming extern returning int
    f:\myfile\vc\cry\ma.c(132) : error C2065: 'AT_KEYEXCHANGE' : undeclared identifier
    f:\myfile\vc\cry\ma.c(168) : warning C4013: 'CryptReleaseContext' undefined; assuming extern returning int
    Error executing cl.exe.
    Creating browse info file...cry.exe - 21 error(s), 7 warning(s)
      

  3.   

    把void main(void) 改成 void __stdcall WinMain(void )试试
      

  4.   

    明显是个console application,不用改主函数的声明。
    应该是SDK的版本太老了,下载个新的装上吧。
      

  5.   

    你的VC 库中没有#include <wincrypt.h>的.dll文件和.lib文件.
      

  6.   

    最有可能的是没有Wincrypt.h文件以及.lib文件,你可以去查找一下。