您的位置:首页 > 编程语言 > C语言/C++

[Automation]Force word.exe process to quit by using VC++

2007-11-15 11:21 447 查看
Sometimes, Word.exe process Still Runs After Automation Session , it will be still avaliable in Task Mananger.

refer to : http://support.microsoft.com/kb/212675

We have to invoke wordApp.quit to quit the whole application.

The following method is from google search, using VC++ to get the existed WORD.EXE instance, to kill it used by the automation method. Of course, whereelse, you can get the WORD.EXE process directly, and call TerminateProcess.


AfxOleInit();//it is very neccessary here. or ::CoInitialize(NULL)/CoUninitialize


CLSID clsid;


HRESULT hr;


hr=::CLSIDFromProgID(L"Word.Application", &clsid); //get the CLSID through ProgID


if(FAILED(hr))




...{


AfxMessageBox(_T("NO OFFICE INSTALLED!"));


return;


}




IUnknown *pUnknown=NULL;


IDispatch *pDispatch=NULL;


_Application app=NULL;




hr=::GetActiveObject(clsid,NULL,&pUnknown); //check whether the instance has existed


if(FAILED(hr))




...{


//AfxMessageBox(_T("No running WORD!"));


return;


}




try




...{


hr=pUnknown->QueryInterface(IID_IDispatch, (LPVOID *)&app);


if(FAILED(hr))


throw(_T("Failed to get IDispatchPtr"));




pUnknown->Release();


pUnknown=NULL;


}


catch(LPCTSTR lpErr)




...{


AfxMessageBox(lpErr);


}




if(pUnknown)


pUnknown->Release();




CComVariant SaveChanges(false); // hint save or not


CComVariant OriginalFormat, RouteDocument;


app.Quit(&SaveChanges, &OriginalFormat, &RouteDocument);


app.ReleaseDispatch();


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐