Share via

How to use CoInitializeSecurity by using pointer to AppID in pSecDesc parameter?

Sivalingam Somasundaram 20 Reputation points
2026-04-22T13:43:55.27+00:00

One of the microsoft recommendation is not to use nullptr for first parameter of CoInitializeSecurity. So how can I use CoInitializeSecurity by using pointer to AppID in first parameter (pSecDesc)?. Appreciate your help in this.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

  1. Q&A Assist
    2026-04-22T13:44:17.5933333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To use CoInitializeSecurity with an AppID instead of passing nullptr for pSecDesc, pass a pointer to the AppID GUID and set the EOAC_APPID capability flag. When this is done, all other CoInitializeSecurity parameters are ignored and COM takes the security settings from the AppID’s registry key.

    Conceptually, the steps are:

    1. Ensure the AppID exists in the registry under: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\{AppID_GUID} and that its security-related values (for example AuthenticationLevel, AccessPermission) are configured as desired.
    2. In code, define the AppID GUID and call CoInitializeSecurity like this (simplified pattern):
    // Example GUID – must match the AppID registered in HKLM\SOFTWARE\Classes\AppID
    // DEFINE_GUID(APPID_MyServer, ...);
    
    GUID appId = APPID_MyServer;
    
    HRESULT hr = CoInitializeSecurity(
        &appId,          // pSecDesc: pointer to AppID GUID
        -1,              // cAuthSvc (ignored when EOAC_APPID is set)
        nullptr,         // asAuthSvc (ignored)
        nullptr,         // pReserved1
        RPC_C_AUTHN_LEVEL_DEFAULT, // dwAuthnLevel (ignored)
        RPC_C_IMP_LEVEL_IDENTIFY,  // dwImpLevel (ignored)
        nullptr,         // pAuthList (ignored)
        EOAC_APPID,      // dwCapabilities: must include EOAC_APPID
        nullptr          // pReserved3
    );
    

    Key rules from the documentation:

    • pSecDesc can be a pointer to an AppID, an IAccessControl object, or a SECURITY_DESCRIPTOR.
    • If pSecDesc points to an AppID, EOAC_APPID must be set in dwCapabilities, and all other parameters to CoInitializeSecurity are ignored.
    • When EOAC_APPID is used, CoInitializeSecurity looks up the authentication level and other security settings under the AppID key in the registry and uses those as the process-wide defaults.
    • Passing nullptr for pSecDesc is strongly discouraged because, unless EOAC_APPID or EOAC_ACCESS_CONTROL is set, it effectively allows all callers including local and remote anonymous users.

    So, to follow the recommendation not to use nullptr, either:

    • Pass a properly constructed SECURITY_DESCRIPTOR, or
    • Pass a pointer to the AppID GUID and set EOAC_APPID, relying on the AppID’s registry configuration for security.

    References:

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Taki Ly (WICLOUD CORPORATION) 1,020 Reputation points Microsoft External Staff Moderator
    2026-04-23T06:30:19.7866667+00:00

    Hello @Sivalingam Somasundaram ,

    To use CoInitializeSecurity with a pointer to an AppID in the first parameter, you need to fulfill two specific requirements:

    1. Cast the GUID pointer: You must take the address of your AppID (GUID) and cast it to PSECURITY_DESCRIPTOR for the first parameter.
    2. Use the EOAC_APPID flag: You must pass the EOAC_APPID flag in the 8th parameter (dwCapabilities). This flag explicitly indicates to the COM infrastructure that the pSecDesc parameter is a pointer to an AppID GUID. COM will then use this GUID to look up your security settings in the Windows Registry.

    Below is a C++ example snippet that I want to demonstrate how to do this:

    #include <windows.h>
    #include <objbase.h>
    #include <iostream>
    
    // Link the COM library for Visual Studio users
    #pragma comment(lib, "ole32.lib")
    
    // 1. Define your AppID (Replace with your application's actual GUID)
    // Example: {12345678-1234-1234-1234-1234567890AB}
    const GUID AppID_MyApp = 
    { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab } };
    
    int main() {
        // Initialize the COM library
        HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    
        if (FAILED(hr)) {
            std::cout << "Failed to initialize COM library." << std::endl;
            return -1;
        }
    
        // 2. Call CoInitializeSecurity using the AppID pointer
        hr = CoInitializeSecurity(
            (PSECURITY_DESCRIPTOR)&AppID_MyApp, // Parameter 1: Cast the address of your AppID
            -1,                                 // cAuthSvc
            NULL,                               // asAuthSvc
            NULL,                               // pReserved1
            RPC_C_AUTHN_LEVEL_DEFAULT,          // dwAuthnLevel
            RPC_C_IMP_LEVEL_IDENTIFY,           // dwImpLevel
            NULL,                               // pAuthList
            EOAC_APPID,                         // Parameter 8: REQUIRED FLAG to indicate pSecDesc is an AppID
            NULL                                // pReserved3
        );
    
        if (hr == S_OK) {
            std::cout << "Successfully initialized COM security using AppID!" << std::endl;
        } else if (hr == REGDB_E_CLASSNOTREG) {
            std::cout << "Failed: AppID not found in the Registry (0x80040154)." << std::endl;
        } else {
            std::cout << "Failed with HRESULT: 0x" << std::hex << hr << std::endl;
        }
    
        // Cleanup COM
        CoUninitialize();
        return 0;
    }
    

    Important Registry note is for this to work at runtime and not return REGDB_E_CLASSNOTREG, your AppID must actually exist in the Windows Registry under HKEY_CLASSES_ROOT\AppID\{Your-GUID} with the appropriate security configurations (like AccessPermission or LaunchPermission). If it's not in the registry, COM won't be able to fetch the security settings for your application.

    Hope this helps clarify your question. If you found my response helpful, please follow this guide to provide feedback.

    Thank you.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.