A evo i programa koji zatvara word (moze da se kompaljira samo sa msvc)
Code:
// killword.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <comdef.h>
#include <iostream>
#ifdef UNICODE
#define tclog std::wclog
#else
#define tclog std::clog
#endif
#pragma comment(lib,"comsuppwd.lib")
struct ole {
ole(void): shouldRelease(false) {
HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
shouldRelease = ((hr == S_OK) || (hr == S_FALSE));
}
~ole(void) {
docleanup();
}
private:
volatile bool shouldRelease;
void docleanup(void) {
if (shouldRelease) {
shouldRelease = false;
::CoUninitialize();
}
}
};
/*_com_dispatch_help
L"\x0009" - IDispatch*
L"\x0008" - BSTR
L"\x000b" - boolean
L"\x000c" - enum
L"\x400c" - VARIANT*
*/
HRESULT CloseWord(void) {
ole ol;
HRESULT result = S_OK;
try {
IUnknownPtr ppunk;
IDispatchPtr ppdisp;
result = ppunk.GetActiveObject("Word.Application");
_com_util::CheckError(result);
{
IDispatch *pdisp = NULL;
result = ppunk.QueryInterface(IID_IDispatch,pdisp);
_com_util::CheckError(result);
ppdisp.Attach(pdisp);
ppunk.Release();
}
LPOLESTR name=L"Quit";
DISPID dispid = NULL;
result = ppdisp->GetIDsOfNames (
IID_NULL,
&name,
1,
LOCALE_USER_DEFAULT,
&dispid);
_com_util::CheckError(result);
variant_t save(false);
variant_t empty;
result = _com_dispatch_method( ppdisp
,dispid
,DISPATCH_METHOD
,VT_EMPTY
,NULL
,L"\x400c\x400c\x400c"
,&save
,&empty
,&empty
);
} catch(const _com_error& e) {
tclog << e.ErrorMessage() << std::endl;
}
return result;
}
int __cdecl _tmain(int /*argc*/, _TCHAR** /*argv[]*/) {
if ( SUCCEEDED(CloseWord()) ) {
tclog << TEXT("Closed Word instance.") << std::endl;
}
return 0;
}
[Ovu poruku je menjao kiklop74 dana 07.12.2010. u 20:45 GMT+1]
Tko leti vrijedi