SOL9 2.0 Class: ComThreadModel

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * ComThreadModel.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2009/12/24 Modified to catch an exception from setDefaultSecurityLevel
//

#pragma once

#define _WIN32_DCOM

#include <sol\Object.h>

namespace SOL {

class ComThreadModel :public Object {

public:
    ComThreadModel(DWORD threadModel, bool initializeCom, bool setDefaultSecurity=true)
    {
        HRESULT hr = E_FAIL;
        if (initializeCom ) {
            if (FAILED(hr = CoInitializeEx(NULL, threadModel))) {
                printf("Failed CoIntialize %0x\n", hr);
                throw hr;
            }
    
            if (setDefaultSecurity) {
                //2009/12/24
                try {
                    setDefaultSecurityLevels();
                } catch (...) {
                    ;//
                }
            }
        }
    }


public:
    /**
     * Destructor
     */
    ~ComThreadModel() {
        CoUninitialize();
    }

    /**
     * Set Default COM security levels with default authentication and Impersonation.
     */
    void setDefaultSecurityLevels() {
        HRESULT hr = S_OK;
        if (FAILED (hr=  CoInitializeSecurity(
            NULL, 
            -1,                          // COM negotiates service
            NULL,                        // Authentication services
            NULL,                        // Reserved
            RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
            RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
            NULL,                        // Authentication info
            EOAC_NONE,                   // Additional capabilities 
            NULL                         // Reserved
            ))) {
            printf("Failed to setDefaultSecurityLevel %0x\n", hr);
            throw hr; 
        }
    }

};


}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.