botol
botol

Reputation: 1

OLEUnInitialize() in windows 7

I am new at MFC. I am Using OLEINitialize() to initialize a COM object, COCreateInstance() to create an instance of the object(EXCEL.EXE), & OLEUnInitialize() to uninitialize the com object. But in windows XP, after the call of OLEUnInitialize(), the EXCEL.EXE ends. But In windows 7 it doesn't. If you guys have any idea please help me. Thanks in advance.

Here is the Constructor:

CXLAutomation::CXLAutomation(BOOL bVisible)
{
    m_pdispExcelApp = NULL;
    m_pdispWorkbook = NULL;
    m_pdispWorksheet = NULL;
    m_pdispActiveChart = NULL;
    InitOLE();       
    StartExcel();
    SetExcelVisible(bVisible);
    CreateWorkSheet();

}

Destructor:

CXLAutomation::~CXLAutomation()
{
    ReleaseDispatch();
    OleUninitialize();
}

Functions:

BOOL CXLAutomation::InitOLE()
{
    DWORD dwOleVer;

    dwOleVer = CoBuildVersion();

    // check the OLE library version
    if (rmm != HIWORD(dwOleVer)) 
    {
        MessageBox(NULL, L"Incorrect version of OLE libraries.", L"Failed", MB_OK | MB_ICONSTOP);
        return FALSE;
    }

    // could also check for minor version, but this application is
    // not sensitive to the minor version of OLE

    // initialize OLE, fail application if we can't get OLE to init.
    if (FAILED(OleInitialize(NULL))) 
    {
        MessageBox(NULL, L"Cannot initialize OLE.", L"Failed", MB_OK | MB_ICONSTOP);
        return FALSE;
    }


    return TRUE;

}

BOOL CXLAutomation::StartExcel()
{
    CLSID clsExcelApp;

    // if Excel is already running, return with current instance
    if (m_pdispExcelApp != NULL)
        return TRUE;

    /* Obtain the CLSID that identifies EXCEL.APPLICATION
     * This value is universally unique to Excel versions 5 and up, and
     * is used by OLE to identify which server to start.  We are obtaining
     * the CLSID from the ProgID.
     */
    if (FAILED(CLSIDFromProgID(L"Excel.Application", &clsExcelApp))) 
    {
        MessageBox(NULL, L"Cannot obtain CLSID from ProgID", L"Failed", MB_OK | MB_ICONSTOP);
        return FALSE;
    }

    // start a new copy of Excel, grab the IDispatch interface
    if (FAILED(CoCreateInstance(clsExcelApp, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&m_pdispExcelApp))) 
    {
        MessageBox(NULL, L"Cannot start an instance of Excel for Automation.", L"Failed", MB_OK | MB_ICONSTOP);
        return FALSE;
    }

    return TRUE;

}

Upvotes: 0

Views: 1348

Answers (2)

Paul Mitchell
Paul Mitchell

Reputation: 3281

You'll need to call Release() on the COM object you've created in order to free it.

Upvotes: 0

i_am_jorf
i_am_jorf

Reputation: 54630

OleInitialize and OleUninitialize are not for COM objects. They are for initializing and uninitializing the COM library on a given thread. You should not call OleUninitialize until you are done using COM, i.e. you have no more COM objects.

Upvotes: 1

Related Questions