Mercurial > projects > dwt2
comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/ole/win32/OleAutomation.d @ 0:6dd524f61e62
add dwt win and basic java stuff
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 02 Mar 2009 14:44:16 +0100 |
parents | |
children | 0ecb2b338560 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6dd524f61e62 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.ole.win32.OleAutomation; | |
14 | |
15 | |
16 import org.eclipse.swt.internal.ole.win32.COM; | |
17 import org.eclipse.swt.internal.ole.win32.COMTYPES; | |
18 import org.eclipse.swt.internal.ole.win32.OAIDL; | |
19 // import org.eclipse.swt.internal.ole.win32.DISPPARAMS; | |
20 // import org.eclipse.swt.internal.ole.win32.EXCEPINFO; | |
21 // import org.eclipse.swt.internal.ole.win32.FUNCDESC; | |
22 // import org.eclipse.swt.internal.ole.win32.GUID; | |
23 // import org.eclipse.swt.internal.ole.win32.IDispatch; | |
24 // import org.eclipse.swt.internal.ole.win32.ITypeInfo; | |
25 // import org.eclipse.swt.internal.ole.win32.TYPEATTR; | |
26 // import org.eclipse.swt.internal.ole.win32.VARDESC; | |
27 import org.eclipse.swt.internal.win32.OS; | |
28 | |
29 import org.eclipse.swt.ole.win32.OleClientSite; | |
30 import org.eclipse.swt.ole.win32.OlePropertyDescription; | |
31 import org.eclipse.swt.ole.win32.OleFunctionDescription; | |
32 import org.eclipse.swt.ole.win32.OleParameterDescription; | |
33 import org.eclipse.swt.ole.win32.Variant; | |
34 import org.eclipse.swt.ole.win32.OLE; | |
35 | |
36 import java.lang.all; | |
37 | |
38 /** | |
39 * OleAutomation provides a generic mechanism for accessing functionality that is | |
40 * specific to a particular ActiveX Control or OLE Document. | |
41 * | |
42 * <p>The OLE Document or ActiveX Control must support the IDispatch interface in order to provide | |
43 * OleAutomation support. The additional functionality provided by the OLE Object is specified in | |
44 * its IDL file. The additional methods can either be to get property values (<code>getProperty</code>), | |
45 * to set property values (<code>setProperty</code>) or to invoke a method (<code>invoke</code> or | |
46 * <code>invokeNoReply</code>). Arguments are passed around in the form of <code>Variant</code> | |
47 * objects. | |
48 * | |
49 * <p>Here is a sample IDL fragment: | |
50 * | |
51 * <pre> | |
52 * interface IMyControl : IDispatch | |
53 * { | |
54 * [propget, id(0)] HRESULT maxFileCount([retval, out] int *c); | |
55 * [propput, id(0)] HRESULT maxFileCount([in] int c); | |
56 * [id(1)] HRESULT AddFile([in] BSTR fileName); | |
57 * }; | |
58 * </pre> | |
59 * | |
60 * <p>An example of how to interact with this extended functionality is shown below: | |
61 * | |
62 * <code><pre> | |
63 * OleAutomation automation = new OleAutomation(myControlSite); | |
64 * | |
65 * // Look up the ID of the maxFileCount parameter | |
66 * int[] rgdispid = automation.getIDsOfNames(new String[]{"maxFileCount"}); | |
67 * int maxFileCountID = rgdispid[0]; | |
68 * | |
69 * // Set the property maxFileCount to 100: | |
70 * if (automation.setProperty(maxFileCountID, new Variant(100))) { | |
71 * System.out.println("Max File Count was successfully set."); | |
72 * } | |
73 * | |
74 * // Get the new value of the maxFileCount parameter: | |
75 * Variant pVarResult = automation.getProperty(maxFileCountID); | |
76 * if (pVarResult !is null) { | |
77 * System.out.println("Max File Count is "+pVarResult.getInt()); | |
78 * } | |
79 * | |
80 * // Invoke the AddFile method | |
81 * // Look up the IDs of the AddFile method and its parameter | |
82 * rgdispid = automation.getIDsOfNames(new String[]{"AddFile", "fileName"}); | |
83 * int dispIdMember = rgdispid[0]; | |
84 * int[] rgdispidNamedArgs = new int[] {rgdispid[1]}; | |
85 * | |
86 * // Convert arguments to Variant objects | |
87 * Variant[] rgvarg = new Variant[1]; | |
88 * String fileName = "C:\\testfile"; | |
89 * rgvarg[0] = new Variant(fileName); | |
90 * | |
91 * // Call the method | |
92 * Variant pVarResult = automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs); | |
93 * | |
94 * // Check the return value | |
95 * if (pVarResult is null || pVarResult.getInt() !is OLE.S_OK){ | |
96 * System.out.println("Failed to add file "+fileName); | |
97 * } | |
98 * | |
99 * automation.dispose(); | |
100 * | |
101 * </pre></code> | |
102 * | |
103 * @see <a href="http://www.eclipse.org/swt/snippets/#ole">OLE and ActiveX snippets</a> | |
104 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: OLEExample, OleWebBrowser</a> | |
105 */ | |
106 public final class OleAutomation { | |
107 private IDispatch objIDispatch; | |
108 private String exceptionDescription; | |
109 private ITypeInfo objITypeInfo; | |
110 | |
111 this(IDispatch idispatch) { | |
112 if (idispatch is null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS); | |
113 objIDispatch = idispatch; | |
114 objIDispatch.AddRef(); | |
115 | |
116 int result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, &objITypeInfo); | |
117 if (result is OLE.S_OK) { | |
118 objITypeInfo.AddRef(); | |
119 } | |
120 } | |
121 /** | |
122 * Creates an OleAutomation object for the specified client. | |
123 * | |
124 * @param clientSite the site for the OLE Document or ActiveX Control whose additional functionality | |
125 * you need to access | |
126 * | |
127 * @exception IllegalArgumentException <ul> | |
128 * <li>ERROR_INVALID_INTERFACE_ADDRESS when called with an invalid client site | |
129 * </ul> | |
130 */ | |
131 public this(OleClientSite clientSite) { | |
132 if (clientSite is null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS); | |
133 objIDispatch = clientSite.getAutomationObject(); | |
134 | |
135 auto result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, &objITypeInfo); | |
136 if (result is OLE.S_OK) { | |
137 objITypeInfo.AddRef(); | |
138 } | |
139 } | |
140 /** | |
141 * Disposes the automation object. | |
142 * <p> | |
143 * This method releases the IDispatch interface on the OLE Document or ActiveX Control. | |
144 * Do not use the OleAutomation object after it has been disposed. | |
145 */ | |
146 public void dispose() { | |
147 | |
148 if (objIDispatch !is null){ | |
149 objIDispatch.Release(); | |
150 } | |
151 objIDispatch = null; | |
152 | |
153 if (objITypeInfo !is null){ | |
154 objITypeInfo.Release(); | |
155 } | |
156 objITypeInfo = null; | |
157 | |
158 } | |
159 IDispatch getAddress() { | |
160 return objIDispatch; | |
161 } | |
162 /** | |
163 * Returns the fully qualified name of the Help file for the given member ID. | |
164 * | |
165 * @param dispId the member ID whose Help file is being retrieved. | |
166 * @return a string representing the fully qualified name of a Help | |
167 * file or null. | |
168 */ | |
169 public String getHelpFile(int dispId) { | |
170 if (objITypeInfo is null) return null; | |
171 BSTR file; | |
172 HRESULT rc = objITypeInfo.GetDocumentation(dispId, null, null, null, &file ); | |
173 if (rc is OLE.S_OK) { | |
174 String str = WCHARzToStr( file, -1 ); | |
175 COM.SysFreeString(file); | |
176 return str; | |
177 } | |
178 return null; | |
179 } | |
180 /** | |
181 * Returns the documentation string for the given member ID. | |
182 * | |
183 * @param dispId the member ID in which the documentation is being retrieved. | |
184 * @return the documentation string if it exists; otherwise return null. | |
185 */ | |
186 public String getDocumentation(int dispId) { | |
187 if (objITypeInfo is null) return null; | |
188 BSTR doc; | |
189 HRESULT rc = objITypeInfo.GetDocumentation(dispId, null, &doc, null, null ); | |
190 if (rc == OLE.S_OK) { | |
191 String s = WCHARzToStr(doc, -1); | |
192 COM.SysFreeString(doc); | |
193 return s; | |
194 } | |
195 return null; | |
196 } | |
197 /** | |
198 * Returns the property description of a variable at the given index. | |
199 * | |
200 * @param index the index of a variable whose property is being retrieved. | |
201 * @return an OlePropertyDescription for a variable at the given index. | |
202 */ | |
203 public OlePropertyDescription getPropertyDescription(int index) { | |
204 if (objITypeInfo is null) return null; | |
205 VARDESC* vardesc; | |
206 HRESULT rc = objITypeInfo.GetVarDesc(index, &vardesc); | |
207 if (rc != OLE.S_OK) return null; | |
208 // VARDESC* vardesc = new VARDESC(); | |
209 // COM.MoveMemory(vardesc, ppVarDesc[0], VARDESC.sizeof); | |
210 | |
211 OlePropertyDescription data = new OlePropertyDescription(); | |
212 data.id = vardesc.memid; | |
213 data.name = getName(vardesc.memid); | |
214 data.type = vardesc.elemdescVar.tdesc.vt; | |
215 if (data.type == OLE.VT_PTR) { | |
216 // short[] vt = new short[1]; | |
217 // COM.MoveMemory(vt, vardesc.elemdescVar.tdesc_union + 4, 2); | |
218 // TODO: | |
219 data.type = vardesc.elemdescVar.tdesc.vt; | |
220 } | |
221 data.flags = vardesc.wVarFlags; | |
222 data.kind = vardesc.varkind; | |
223 data.description = getDocumentation(vardesc.memid); | |
224 data.helpFile = getHelpFile(vardesc.memid); | |
225 | |
226 objITypeInfo.ReleaseVarDesc(vardesc); | |
227 return data; | |
228 } | |
229 /** | |
230 * Returns the description of a function at the given index. | |
231 * | |
232 * @param index the index of a function whose property is being retrieved. | |
233 * @return an OleFunctionDescription for a function at the given index. | |
234 */ | |
235 public OleFunctionDescription getFunctionDescription(int index) { | |
236 if (objITypeInfo is null) return null; | |
237 FUNCDESC* funcdesc; | |
238 HRESULT rc = objITypeInfo.GetFuncDesc(index, &funcdesc); | |
239 if (rc != OLE.S_OK) return null; | |
240 | |
241 OleFunctionDescription data = new OleFunctionDescription(); | |
242 | |
243 data.id = funcdesc.memid; | |
244 data.optionalArgCount = funcdesc.cParamsOpt; | |
245 data.invokeKind = funcdesc.invkind; | |
246 data.funcKind = funcdesc.funckind; | |
247 data.flags = funcdesc.wFuncFlags; | |
248 data.callingConvention = funcdesc.callconv; | |
249 data.documentation = getDocumentation(funcdesc.memid); | |
250 data.helpFile = getHelpFile(funcdesc.memid); | |
251 | |
252 String[] names = getNames(funcdesc.memid, funcdesc.cParams + 1); | |
253 if (names.length > 0) { | |
254 data.name = names[0]; | |
255 } | |
256 data.args = new OleParameterDescription[funcdesc.cParams]; | |
257 for (int i = 0; i < data.args.length; i++) { | |
258 data.args[i] = new OleParameterDescription(); | |
259 if (names.length > i + 1) { | |
260 data.args[i].name = names[i + 1]; | |
261 } | |
262 short[1] vt; | |
263 COM.MoveMemory(vt.ptr, (cast(void*)funcdesc.lprgelemdescParam) + i * 16 + 4, 2); | |
264 if (vt[0] is OLE.VT_PTR) { | |
265 int[1] pTypedesc; | |
266 COM.MoveMemory(pTypedesc.ptr, (cast(void*)funcdesc.lprgelemdescParam) + i * 16, 4); | |
267 short[1] vt2; | |
268 COM.MoveMemory(vt2.ptr, pTypedesc[0] + 4, 2); | |
269 vt[0] = cast(short)(vt2[0] | COM.VT_BYREF); | |
270 } | |
271 data.args[i].type = vt[0]; | |
272 short[1] wParamFlags; | |
273 COM.MoveMemory(wParamFlags.ptr, (cast(void*)funcdesc.lprgelemdescParam) + i * 16 + 12, 2); | |
274 data.args[i].flags = wParamFlags[0]; | |
275 } | |
276 | |
277 data.returnType = funcdesc.elemdescFunc.tdesc.vt; | |
278 if (data.returnType is OLE.VT_PTR) { | |
279 ushort[1] vt; | |
280 COM.MoveMemory(vt.ptr, funcdesc.elemdescFunc.tdesc.u.lpadesc, 2); | |
281 data.returnType = vt[0]; | |
282 } | |
283 | |
284 objITypeInfo.ReleaseFuncDesc(funcdesc); | |
285 return data; | |
286 } | |
287 /** | |
288 * Returns the type info of the current object referenced by the automation. | |
289 * The type info contains information about the object such as the function descriptions, | |
290 * the member descriptions and attributes of the type. | |
291 * | |
292 * @return the type info of the receiver | |
293 */ | |
294 public TYPEATTR* getTypeInfoAttributes() { | |
295 if (objITypeInfo is null) return null; | |
296 TYPEATTR* ppTypeAttr; | |
297 HRESULT rc = objITypeInfo.GetTypeAttr(&ppTypeAttr); | |
298 if (rc !is OLE.S_OK) return null; | |
299 TYPEATTR* typeattr = new TYPEATTR(); | |
300 COM.MoveMemory(typeattr, ppTypeAttr, TYPEATTR.sizeof); | |
301 objITypeInfo.ReleaseTypeAttr(ppTypeAttr); | |
302 return typeattr; | |
303 } | |
304 /** | |
305 * Returns the name of the given member ID. | |
306 * | |
307 * @param dispId the member ID in which the name is being retrieved. | |
308 * @return the name if it exists; otherwise return null. | |
309 */ | |
310 public String getName(int dispId) { | |
311 if (objITypeInfo is null) return null; | |
312 BSTR name; | |
313 HRESULT rc = objITypeInfo.GetDocumentation(dispId, &name, null, null, null ); | |
314 if (rc == OLE.S_OK) { | |
315 String s = WCHARzToStr(name, -1); | |
316 COM.SysFreeString(name); | |
317 return s; | |
318 } | |
319 return null; | |
320 } | |
321 /** | |
322 * Returns the name of a function and parameter names for the specified function ID. | |
323 * | |
324 * @param dispId the function ID in which the name and parameters are being retrieved. | |
325 * @param maxSize the maximum number of names to retrieve. | |
326 * @return an array of name containing the function name and the parameter names | |
327 */ | |
328 public String[] getNames(int dispId, int maxSize) { | |
329 if (objITypeInfo is null) return new String[0]; | |
330 BSTR[] names = new BSTR[maxSize]; | |
331 uint count; | |
332 HRESULT rc = objITypeInfo.GetNames(dispId, names.ptr, maxSize, &count); | |
333 if (rc == OLE.S_OK) { | |
334 String[] newNames = new String[count]; | |
335 for(int i=0; i<count; ++i){ | |
336 newNames[i] = WCHARzToStr(names[i], -1); | |
337 COM.SysFreeString(names[i]); | |
338 } | |
339 return newNames; | |
340 } | |
341 return null; | |
342 } | |
343 /** | |
344 * Returns the positive integer values (IDs) that are associated with the specified names by the | |
345 * IDispatch implementor. If you are trying to get the names of the parameters in a method, the first | |
346 * String in the names array must be the name of the method followed by the names of the parameters. | |
347 * | |
348 * @param names an array of names for which you require the identifiers | |
349 * | |
350 * @return positive integer values that are associated with the specified names in the same | |
351 * order as the names where provided; or null if the names are unknown | |
352 */ | |
353 public int[] getIDsOfNames(String[] names) { | |
354 | |
355 int count = names.length; | |
356 wchar*[] wcNames = new wchar*[count]; | |
357 for(int i=0; i<count; ++i){ | |
358 wcNames[i] = StrToWCHARz(names[i]); | |
359 } | |
360 int[] rgdispid = new int[count]; | |
361 // TODO: NULL GUID ?? | |
362 GUID id; | |
363 HRESULT result = objIDispatch.GetIDsOfNames(&id, wcNames.ptr, count, COM.LOCALE_USER_DEFAULT, rgdispid.ptr); | |
364 if (result != COM.S_OK) return null; | |
365 | |
366 return rgdispid; | |
367 } | |
368 /** | |
369 * Returns a description of the last error encountered. | |
370 * | |
371 * @return a description of the last error encountered | |
372 */ | |
373 public String getLastError() { | |
374 | |
375 return exceptionDescription; | |
376 | |
377 } | |
378 /** | |
379 * Returns the value of the property specified by the dispIdMember. | |
380 * | |
381 * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the | |
382 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
383 * | |
384 * @return the value of the property specified by the dispIdMember or null | |
385 */ | |
386 public Variant getProperty(int dispIdMember) { | |
387 Variant pVarResult = new Variant(); | |
388 HRESULT result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, null, null, pVarResult); | |
389 return (result is OLE.S_OK) ? pVarResult : null; | |
390 } | |
391 /** | |
392 * Returns the value of the property specified by the dispIdMember. | |
393 * | |
394 * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the | |
395 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
396 * | |
397 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
398 * read only unless the Variant is a By Reference Variant type. | |
399 * | |
400 * @return the value of the property specified by the dispIdMember or null | |
401 * | |
402 * @since 2.0 | |
403 */ | |
404 public Variant getProperty(int dispIdMember, Variant[] rgvarg) { | |
405 Variant pVarResult = new Variant(); | |
406 HRESULT result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, rgvarg, null, pVarResult); | |
407 return (result is OLE.S_OK) ? pVarResult : null; | |
408 | |
409 } | |
410 /** | |
411 * Returns the value of the property specified by the dispIdMember. | |
412 * | |
413 * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the | |
414 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
415 * | |
416 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
417 * read only unless the Variant is a By Reference Variant type. | |
418 * | |
419 * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the | |
420 * parameter IDs must be in the same order as their corresponding values; | |
421 * all arguments must have an identifier - identifiers can be obtained using | |
422 * OleAutomation.getIDsOfNames | |
423 * | |
424 * @return the value of the property specified by the dispIdMember or null | |
425 * | |
426 * @since 2.0 | |
427 */ | |
428 public Variant getProperty(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) { | |
429 Variant pVarResult = new Variant(); | |
430 HRESULT result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, rgvarg, rgdispidNamedArgs, pVarResult); | |
431 return (result is OLE.S_OK) ? pVarResult : null; | |
432 } | |
433 | |
434 /** | |
435 * Invokes a method on the OLE Object; the method has no parameters. | |
436 * | |
437 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
438 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
439 * | |
440 * @return the result of the method or null if the method failed to give result information | |
441 */ | |
442 public Variant invoke(int dispIdMember) { | |
443 Variant pVarResult = new Variant(); | |
444 HRESULT result = invoke(dispIdMember, COM.DISPATCH_METHOD, null, null, pVarResult); | |
445 return (result is COM.S_OK) ? pVarResult : null; | |
446 } | |
447 /** | |
448 * Invokes a method on the OLE Object; the method has no optional parameters. | |
449 * | |
450 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
451 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
452 * | |
453 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
454 * read only unless the Variant is a By Reference Variant type. | |
455 * | |
456 * @return the result of the method or null if the method failed to give result information | |
457 */ | |
458 public Variant invoke(int dispIdMember, Variant[] rgvarg) { | |
459 Variant pVarResult = new Variant(); | |
460 HRESULT result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, null, pVarResult); | |
461 return (result is COM.S_OK) ? pVarResult : null; | |
462 } | |
463 /** | |
464 * Invokes a method on the OLE Object; the method has optional parameters. It is not | |
465 * necessary to specify all the optional parameters, only include the parameters for which | |
466 * you are providing values. | |
467 * | |
468 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
469 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
470 * | |
471 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
472 * read only unless the Variant is a By Reference Variant type. | |
473 * | |
474 * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the | |
475 * parameter IDs must be in the same order as their corresponding values; | |
476 * all arguments must have an identifier - identifiers can be obtained using | |
477 * OleAutomation.getIDsOfNames | |
478 * | |
479 * @return the result of the method or null if the method failed to give result information | |
480 */ | |
481 public Variant invoke(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) { | |
482 Variant pVarResult = new Variant(); | |
483 HRESULT result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, rgdispidNamedArgs, pVarResult); | |
484 return (result is COM.S_OK) ? pVarResult : null; | |
485 } | |
486 private int invoke(int dispIdMember, ushort wFlags, Variant[] rgvarg, int[] rgdispidNamedArgs, Variant pVarResult) { | |
487 assert(objIDispatch); | |
488 | |
489 // get the IDispatch interface for the control | |
490 if (objIDispatch is null) return COM.E_FAIL; | |
491 | |
492 // create a DISPPARAMS structure for the input parameters | |
493 DISPPARAMS pDispParams; | |
494 | |
495 // store arguments in rgvarg | |
496 if (rgvarg !is null && rgvarg.length > 0) { | |
497 VARIANT[] tempArgs = new VARIANT[rgvarg.length]; | |
498 for (int i = 0; i < rgvarg.length ; ++i) { | |
499 rgvarg[i].getData(&tempArgs[i]); | |
500 } | |
501 // the reverse sequency | |
502 tempArgs.reverse; | |
503 pDispParams.cArgs = tempArgs.length; | |
504 pDispParams.rgvarg = tempArgs.ptr; | |
505 } | |
506 | |
507 // if arguments have ids, store the ids in rgdispidNamedArgs | |
508 if (rgdispidNamedArgs !is null && rgdispidNamedArgs.length > 0) { | |
509 DISPID[] tempArgs = rgdispidNamedArgs.dup; | |
510 // the reverse sequency | |
511 tempArgs.reverse; | |
512 pDispParams.cNamedArgs = tempArgs.length; | |
513 pDispParams.rgdispidNamedArgs = tempArgs.ptr; | |
514 } | |
515 | |
516 // invoke the method | |
517 EXCEPINFO excepInfo; | |
518 uint pArgErr; | |
519 VARIANT* pVarResultAddress = null; | |
520 if (pVarResult !is null) | |
521 pVarResultAddress = new VARIANT(); | |
522 | |
523 GUID id; // IID_NULL | |
524 /* | |
525 HRESULT Invoke( | |
526 [in] DISPID dispIdMember, | |
527 [in] REFIID riid, | |
528 [in] LCID lcid, | |
529 [in] WORD wFlags, | |
530 [in, out] DISPPARAMS * pDispParams, | |
531 [out] VARIANT * pVarResult, | |
532 [out] EXCEPINFO * pExcepInfo, | |
533 [out] UINT * puArgErr | |
534 ); | |
535 */ | |
536 HRESULT result = objIDispatch.Invoke(dispIdMember, &id, COM.LOCALE_USER_DEFAULT, wFlags, &pDispParams, pVarResultAddress, &excepInfo, &pArgErr); | |
537 | |
538 if (pVarResultAddress !is null){ | |
539 pVarResult.setData(pVarResultAddress); | |
540 COM.VariantClear(pVarResultAddress); | |
541 } | |
542 | |
543 // free the Dispparams resources | |
544 if (pDispParams.rgvarg !is null) { | |
545 for (int i = 0, length = rgvarg.length; i < length; i++){ | |
546 COM.VariantClear(&pDispParams.rgvarg[i]); | |
547 } | |
548 pDispParams.rgvarg = null; | |
549 } | |
550 pDispParams.rgdispidNamedArgs = null; | |
551 | |
552 // save error string and cleanup EXCEPINFO | |
553 manageExcepinfo(result, &excepInfo); | |
554 | |
555 return result; | |
556 } | |
557 /** | |
558 * Invokes a method on the OLE Object; the method has no parameters. In the early days of OLE, | |
559 * the IDispatch interface was not well defined and some applications (mainly Word) did not support | |
560 * a return value. For these applications, call this method instead of calling | |
561 * <code>public void invoke(int dispIdMember)</code>. | |
562 * | |
563 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
564 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
565 * | |
566 * @exception org.eclipse.swt.SWTException <ul> | |
567 * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails | |
568 * </ul> | |
569 */ | |
570 public void invokeNoReply(int dispIdMember) { | |
571 HRESULT result = invoke(dispIdMember, COM.DISPATCH_METHOD, null, null, null); | |
572 if (result !is COM.S_OK) | |
573 OLE.error(__FILE__, __LINE__, OLE.ERROR_ACTION_NOT_PERFORMED, result); | |
574 } | |
575 /** | |
576 * Invokes a method on the OLE Object; the method has no optional parameters. In the early days of OLE, | |
577 * the IDispatch interface was not well defined and some applications (mainly Word) did not support | |
578 * a return value. For these applications, call this method instead of calling | |
579 * <code>public void invoke(int dispIdMember, Variant[] rgvarg)</code>. | |
580 * | |
581 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
582 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
583 * | |
584 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
585 * read only unless the Variant is a By Reference Variant type. | |
586 * | |
587 * @exception org.eclipse.swt.SWTException <ul> | |
588 * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails | |
589 * </ul> | |
590 */ | |
591 public void invokeNoReply(int dispIdMember, Variant[] rgvarg) { | |
592 int result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, null, null); | |
593 if (result !is COM.S_OK) | |
594 OLE.error(__FILE__, __LINE__, OLE.ERROR_ACTION_NOT_PERFORMED, result); | |
595 } | |
596 /** | |
597 * Invokes a method on the OLE Object; the method has optional parameters. It is not | |
598 * necessary to specify all the optional parameters, only include the parameters for which | |
599 * you are providing values. In the early days of OLE, the IDispatch interface was not well | |
600 * defined and some applications (mainly Word) did not support a return value. For these | |
601 * applications, call this method instead of calling | |
602 * <code>public void invoke(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs)</code>. | |
603 * | |
604 * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the | |
605 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
606 * | |
607 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
608 * read only unless the Variant is a By Reference Variant type. | |
609 * | |
610 * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the | |
611 * parameter IDs must be in the same order as their corresponding values; | |
612 * all arguments must have an identifier - identifiers can be obtained using | |
613 * OleAutomation.getIDsOfNames | |
614 * | |
615 * @exception org.eclipse.swt.SWTException <ul> | |
616 * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails | |
617 * </ul> | |
618 */ | |
619 public void invokeNoReply(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) { | |
620 HRESULT result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, rgdispidNamedArgs, null); | |
621 if (result !is COM.S_OK) | |
622 OLE.error(__FILE__, __LINE__, OLE.ERROR_ACTION_NOT_PERFORMED, result); | |
623 } | |
624 private void manageExcepinfo(int hResult, EXCEPINFO* excepInfo) { | |
625 | |
626 if (hResult is COM.S_OK){ | |
627 exceptionDescription = "No Error"; //$NON-NLS-1$ | |
628 return; | |
629 } | |
630 | |
631 // extract exception info | |
632 if (hResult is COM.DISP_E_EXCEPTION) { | |
633 if (excepInfo.bstrDescription !is null){ | |
634 exceptionDescription = WCHARzToStr(excepInfo.bstrDescription); | |
635 } else { | |
636 exceptionDescription = ("OLE Automation Error Exception "); //$NON-NLS-1$ | |
637 if (excepInfo.wCode != 0){ | |
638 exceptionDescription ~= "code = "; | |
639 exceptionDescription ~= cast(int)(excepInfo.wCode); //$NON-NLS-1$ | |
640 } else if (excepInfo.scode != 0){ | |
641 exceptionDescription ~= "code = "; | |
642 exceptionDescription ~= (excepInfo.scode); //$NON-NLS-1$ | |
643 } | |
644 } | |
645 } else { | |
646 exceptionDescription = ("OLE Automation Error HResult : ") ~ toHex(hResult); //$NON-NLS-1$ | |
647 } | |
648 | |
649 // cleanup EXCEPINFO struct | |
650 if (excepInfo.bstrDescription !is null) | |
651 COM.SysFreeString(excepInfo.bstrDescription); | |
652 if (excepInfo.bstrHelpFile !is null) | |
653 COM.SysFreeString(excepInfo.bstrHelpFile); | |
654 if (excepInfo.bstrSource !is null) | |
655 COM.SysFreeString(excepInfo.bstrSource); | |
656 } | |
657 /** | |
658 * Sets the property specified by the dispIdMember to a new value. | |
659 * | |
660 * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the | |
661 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
662 * @param rgvarg the new value of the property | |
663 * | |
664 * @return true if the operation was successful | |
665 */ | |
666 public bool setProperty(int dispIdMember, Variant rgvarg) { | |
667 | |
668 Variant[] rgvarg2 = new Variant[1]; | |
669 rgvarg2[0] = rgvarg; | |
670 int[] rgdispidNamedArgs; | |
671 rgdispidNamedArgs ~= COM.DISPID_PROPERTYPUT; | |
672 ushort dwFlags = COM.DISPATCH_PROPERTYPUT; | |
673 if ((rgvarg.getType() & COM.VT_BYREF) == COM.VT_BYREF) | |
674 dwFlags = COM.DISPATCH_PROPERTYPUTREF; | |
675 Variant pVarResult = new Variant(); | |
676 HRESULT result = invoke(dispIdMember, dwFlags, rgvarg2, rgdispidNamedArgs, pVarResult); | |
677 return (result == COM.S_OK); | |
678 } | |
679 /** | |
680 * Sets the property specified by the dispIdMember to a new value. | |
681 * | |
682 * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the | |
683 * value for the ID can be obtained using OleAutomation.getIDsOfNames | |
684 * @param rgvarg an array of arguments for the method. All arguments are considered to be | |
685 * read only unless the Variant is a By Reference Variant type. | |
686 * | |
687 * @return true if the operation was successful | |
688 * | |
689 * @since 2.0 | |
690 */ | |
691 public bool setProperty(int dispIdMember, Variant[] rgvarg) { | |
692 int[] rgdispidNamedArgs; | |
693 rgdispidNamedArgs ~= COM.DISPID_PROPERTYPUT; | |
694 ushort dwFlags = COM.DISPATCH_PROPERTYPUT; | |
695 for (int i = 0; i < rgvarg.length; i++) { | |
696 if ((rgvarg[i].getType() & COM.VT_BYREF) == COM.VT_BYREF) | |
697 dwFlags = COM.DISPATCH_PROPERTYPUTREF; | |
698 } | |
699 Variant pVarResult = new Variant(); | |
700 HRESULT result = invoke(dispIdMember, dwFlags, rgvarg, rgdispidNamedArgs, pVarResult); | |
701 return (result == COM.S_OK); | |
702 } | |
703 } | |
704 |