1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <windows.h>
#include <tchar.h>
#include <cstdio>
const CLSID CLSID_XLApplication = {0x00024500,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
const IID IID_Application = {0x000208D5,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
int main()
{
DISPPARAMS NoArgs={NULL,NULL,0,0};
IDispatch* pXLApp=NULL;
IDispatch* pXLBooks=NULL;
IDispatch* pXLBook=NULL;
DISPPARAMS DispParams;
VARIANT CallArgs[1];
VARIANT vResult;
DISPID dispid;
HRESULT hr;
LCID lcid;
CoInitialize(NULL);
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp);
if(SUCCEEDED(hr))
{
OLECHAR* szVisible=(OLECHAR*)L"Visible";
lcid=GetUserDefaultLCID();
hr=pXLApp->GetIDsOfNames(IID_NULL,&szVisible,1,lcid,&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&CallArgs[0]);
CallArgs[0].vt=VT_BOOL;
CallArgs[0].boolVal=TRUE;
DISPID dispidNamed=DISPID_PROPERTYPUT;
DispParams.rgvarg=CallArgs;
DispParams.rgdispidNamedArgs=&dispidNamed;
DispParams.cArgs=1;
DispParams.cNamedArgs=1;
VariantInit(&vResult);
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
OLECHAR* szWorkbooks=(OLECHAR*)L"Workbooks";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szWorkbooks,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&vResult);
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLBooks=vResult.pdispVal;
OLECHAR* szOpen=(OLECHAR*)L"Open";
hr=pXLBooks->GetIDsOfNames(IID_NULL,&szOpen,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&vResult);
CallArgs[0].vt = VT_BSTR;
CallArgs[0].bstrVal = SysAllocString(L"C:\\Code\\CodeBlks\\XL_Dispatch\\Book1.xls");
DispParams.rgvarg = CallArgs;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLBooks->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD|DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLBook=vResult.pdispVal;
IDispatch* pXLSheets=NULL;
OLECHAR* szSheets=(OLECHAR*)L"Sheets";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szSheets,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantClear(&vResult);
VariantInit(&vResult);
CallArgs[0].vt=VT_BSTR;
CallArgs[0].bstrVal=SysAllocString(L"Sheet2");
DispParams.rgvarg=CallArgs;
dispidNamed=DISPID_PROPERTYPUT;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs=1;
DispParams.cNamedArgs=0;
hr=pXLBook->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
printf("You Won't See This Because The Call To Select sheet2 Will Fail!\n");
pXLSheets=vResult.pdispVal;
pXLSheets->Release();
}
OLECHAR* szActiveSheet=(OLECHAR*)L"ActiveSheet";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szActiveSheet,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLSheet=NULL;
VariantInit(&vResult);
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLSheet=vResult.pdispVal;
OLECHAR* szRange=(OLECHAR*)L"Range";
hr=pXLSheet->GetIDsOfNames(IID_NULL,&szRange,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLRange=NULL;
VariantInit(&vResult);
CallArgs[0].vt=VT_BSTR,
CallArgs[0].bstrVal=SysAllocString(L"A1");
DispParams.rgvarg=CallArgs;
DispParams.rgdispidNamedArgs=0;
DispParams.cArgs=1;
DispParams.cNamedArgs=0;
hr=pXLSheet->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLRange=vResult.pdispVal;
OLECHAR* szValue=(OLECHAR*)L"Value";
hr=pXLRange->GetIDsOfNames(IID_NULL,&szValue,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantClear(&CallArgs[0]);
hr=pXLRange->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
wprintf(L"vResult.bstrVal = %s\n",vResult.bstrVal);
VariantClear(&vResult);
}
pXLRange->Release();
}
}
}
pXLSheet->Release();
}
}
}
pXLBook->Release();
}
}
pXLBooks->Release();
}
}
}
getchar();
}
VariantInit(&vResult); // Try to do _Application::Close()
hr=pXLApp->Invoke(0x0000012e,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&NoArgs,&vResult,NULL,NULL);
pXLApp->Release();
}
CoUninitialize();
return 0;
}
| |