Installing the New Functionality

To improve performance, it is possible to install the new functionality into memory. The CryptoAPI function searches memory for the functionality before searching the registry for the DLL. For this to work, the DLL must have been loaded previously.

The CryptoAPI CryptInstallOIDFunctionAddress function is used to install the address of the new functionality. It should be placed in the DllMain function of the DLL, as shown in the following example code fragment:

#define NEW_CERT_TYPE_ENCODE_COUNT 
             (sizeof(NewCertificateTypeEncodeObject)/
                sizeof(NewCertificateTypeEncodeObject[0]))
BOOL
WINAPI
DllMain(
        HMODULE hModule,
        ULONG  ulReason,
        LPVOID lpReserved)
{
  static const CRYPT_OID_FUNC_ENTRY NewCertificateTypeEncodeObject[] = 
    {
        szOID_NEW_CERTIFICATE_TYPE, NewCertificateTypeEncodeObject
    };

    switch (ulReason)
    {
        case DLL_PROCESS_ATTACH:
            if (!CryptInstallOIDFunctionAddress(
                  hModule,                      // Module handle
                  X509_ASN_ENCODING,            // Encoding type
                  CRYPT_OID_ENCODE_OBJECT_FUNC, // Function name
                  NEW_CERT_TYPE_ENCODE_COUNT,   // Number of 
                                                //   array elements.
                  NewCertificateTypeEncodeObject, // Functions array
                  0))                             // dwFlags
            {
                printf("Install OID function address failed"); 
                return FALSE;
            }
            break;
        default:
            break;
    }
    return TRUE;
}
 

As stated earlier, to install new functionality into memory, the DLL must have been loaded. To facilitate this, it is good programming practice to always provide the code for both registering and installing the new functionality. This way, the first call to the function causes the DLL to get loaded (via the registry), and any subsequent calls will be found in memory, thereby improving performance.

If hModule is passed to CryptInstallOIDFunctionAddress, once installed, the DLL is not unloaded until the Crypt32.dll is unloaded.