[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();
}
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();
}
相关文章推荐
- 安装完pip后出现unable to create process using "python.exe"的报错
- Fatal error in launcher: Unable to create process using '"D:\pytghon2.7\python.exe" "D:\python2.7\S
- Step-By-Step Guide to Get Started with Salesforce REST API using Java
- To pack your python scripts to EXE by using PyInstaller3.0
- Word Process Using OLE Automation
- Python:Fatal error in launcher: Unable to create process using pip.exe
- [转]How to create a Windows service by using Sc.exe
- pip运行报错Fatal error in launcher: Unable to create process using pip.exe
- How to invoke active dll written by VB using VC - VC如何调用VB写的Active Dll - 详解
- How to debug user-mode process using kernel-mode windbg in Win7
- JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.
- how to force opened by browser
- vc 6 中使用 attach to process
- Using script to submit INV Manager to process MTI/MMTT
- 以不同用户身份运行程序,/savecred只需要输入一次密码(GetTokenByName取得EXPLORER.EXE的令牌,然后调用CreateProcessAsUser,而且使用LoadUserProfile解决另存文件的问题)good
- How to build a one-click process with DocuSign for Salesforce
- Another git process semms to be running in this repository, e.g. an editor opened by ‘git commit’. P
- how to get Multilevel BOM in Windchill by using...
- How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
- python2用pip进行安装时报错Fatal error in launcher: Unable to create process using '"'