私はMFCの新人です。OLEINitialize() を使用して COM オブジェクトを初期化し、COCreateInstance() を使用してオブジェクト (EXCEL.EXE) のインスタンスを作成し、OLEUnInitialize() を使用して com オブジェクトを非初期化しています。しかし、Windows XP では、OLEUnInitialize() の呼び出し後に、EXCEL.EXE が終了します。しかし、Windows 7ではそうではありません。皆さんに何か考えがあれば、私を助けてください。前もって感謝します。
コンストラクタは次のとおりです。
CXLAutomation::CXLAutomation(BOOL bVisible)
{
m_pdispExcelApp = NULL;
m_pdispWorkbook = NULL;
m_pdispWorksheet = NULL;
m_pdispActiveChart = NULL;
InitOLE();
StartExcel();
SetExcelVisible(bVisible);
CreateWorkSheet();
}
デストラクタ:
CXLAutomation::~CXLAutomation()
{
ReleaseDispatch();
OleUninitialize();
}
機能:
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;
}