安裝新功能
將新功能安裝到記憶體中可以改善效能。 在搜尋 DLL 登錄之前,CryptoAPI函式會先搜尋功能的記憶體。 安裝功能之前,必須先載入 DLL。
CryptInstallOIDFunctionAddress 會安裝新功能的位址。 它應該放在 DLL 的 DllMain 函式中。
如果 hModule 傳遞至 CryptInstallOIDFunctionAddress,一旦安裝之後,DLL 就不會卸載,直到卸載Crypt32.dll為止。
下列範例會呼叫 CryptInstallOIDFunctionAddress 函式。
#include <windows.h>
#include <stdio.h>
#define X509_ENCODE_FUNC_COUNT (sizeof(X509EncodeFuncTable) / \
sizeof(X509EncodeFuncTable[0]))
static BOOL WINAPI OssX509CtlUsageEncode(
IN DWORD dwCertEncodingType,
IN LPCSTR lpszStructType,
IN PCTL_USAGE pInfo,
OUT BYTE *pbEncoded,
IN OUT DWORD *pcbEncoded
);
static const CRYPT_OID_FUNC_ENTRY X509EncodeFuncTable[] = {
X509_ENHANCED_KEY_USAGE, OssX509CtlUsageEncode,
};
BOOL WINAPI DllMain(
HMODULE hModule,
ULONG ulReason,
LPVOID lpReserved)
{
switch (ulReason)
{
case DLL_PROCESS_ATTACH:
if (!CryptInstallOIDFunctionAddress(
hModule,
X509_ASN_ENCODING,
CRYPT_OID_ENCODE_OBJECT_FUNC,
X509_ENCODE_FUNC_COUNT,
X509EncodeFuncTable,
0))
{
printf("Install OID function address failed.");
return FALSE;
}
break;
default:
break;
}
return TRUE;
}
//-------------------------------------------------------------------
// CTL Usage (Enhanced Key Usage) Encode (OSS X509)
//-------------------------------------------------------------------
static BOOL WINAPI OssX509CtlUsageEncode(
IN DWORD /*dwCertEncodingType*/,
IN LPCSTR /*lpszStructType*/,
IN PCTL_USAGE pInfo,
OUT BYTE *pbEncoded,
IN OUT DWORD *pcbEncoded)
{
//Encoding logic goes here.
}