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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
| #include <windows.h>
// Program Demonstrates Late Bound OLE COM Access To MS Excel Spreadsheet //Using C++.
#include <tchar.h>
// "Hello, World! Is Written To Cell A1 Of Sheet #1 In Visible Workbook. //IDispatch
#include <cstdio>
// Interface Using GetIDsOfNames() And Invoke() Used Throughout.
const CLSID CLSID_XLApplication = {0x00024500,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}}; // CLSID of Excel
const IID IID_Application = {0x000208D5,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}}; // IID of _Application
int main()
{
DISPPARAMS NoArgs={NULL,NULL,0,0};
// This variable is used in easiest Invoke() call when the method has no //parameters. When
IDispatch* pXLApp=NULL;
// using the IDispatch interface in conjunction with Invoke() method //parameters must be loaded
DISPPARAMS DispParams;
// into a DISPPARAMS struct. The actual parameters are loaded //into VARIANTs, and one of the
VARIANT CallArgs[1];
// members of the DISPPARAMS struct is a pointer to the array //of VARIANT. The other members
VARIANT vResult;
// of the DISPARAMS struct tell Invoke() how many parameters //are being passed, as well as other
DISPID dispid;
// specifics such as the type of the call (propput, propget, etc.).
HRESULT hr;
CoInitialize(NULL);
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp);
if(SUCCEEDED(hr))
{
OLECHAR* szVisible=(OLECHAR*)L"Visible";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szVisible,1,GetUserDefaultLCID(),&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);
// Call or Invoke _Application::Visible(true);
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT,&DispParams,&vResult,NULL,NULL);
OLECHAR* szWorkbooks=(OLECHAR*)L"Workbooks";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szWorkbooks,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLBooks=NULL; // Get Workbooks Collection
VariantInit(&vResult);
// Invoke _Application::Workbooks(&pXLBooks) << returns IDispatch** of //Workbooks Collection
hr=pXLApp->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLBooks=vResult.pdispVal;
IDispatch* pXLBook=NULL; // Try to add Workbook
OLECHAR* szAdd=(OLECHAR*)L"Add";
hr=pXLBooks->GetIDsOfNames(IID_NULL,&szAdd,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
VariantInit(&vResult);
// Invoke Workbooks::Add(&Workbook) << returns IDispatch** of Workbook //Object
hr=pXLBooks->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD|DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLBook=vResult.pdispVal;
OLECHAR* szActiveSheet=(OLECHAR*)L"ActiveSheet";
hr=pXLApp->GetIDsOfNames(IID_NULL,&szActiveSheet,1,GetUserDefaultLCID(),&dispid);
if(SUCCEEDED(hr))
{
IDispatch* pXLSheet=NULL; // Try To Get ActiveSheet
VariantInit(&vResult);
// Invoke _Application::ActiveSheet(&pXLSheet); << ret IDispatch** to Worksheet (Worksheet)
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; // Try to get Range
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
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]);
CallArgs[0].vt = VT_BSTR;
CallArgs[0].bstrVal = SysAllocString(L"Hello, World!");
//Try to set data to cell A1 using pXLRange
DispParams.rgvarg = CallArgs;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
// Try to write to Value member of Range dispinterface
DispParams.cNamedArgs = 1;
// Invoke Range::Value(L"Hello, World!")
hr=pXLRange->Invoke(dispid,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT,&DispParams,NULL,NULL,NULL);
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;
} |