KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > ole > win32 > OleAutomation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.swt.ole.win32;
12
13
14 import org.eclipse.swt.internal.ole.win32.*;
15 import org.eclipse.swt.internal.win32.*;
16
17 /**
18  * OleAutomation provides a generic mechanism for accessing functionality that is
19  * specific to a particular ActiveX Control or OLE Document.
20  *
21  * <p>The OLE Document or ActiveX Control must support the IDispatch interface in order to provide
22  * OleAutomation support. The additional functionality provided by the OLE Object is specified in
23  * its IDL file. The additional methods can either be to get property values (<code>getProperty</code>),
24  * to set property values (<code>setProperty</code>) or to invoke a method (<code>invoke</code> or
25  * <code>invokeNoReply</code>). Arguments are passed around in the form of <code>Variant</code>
26  * objects.
27  *
28  * <p>Here is a sample IDL fragment:
29  *
30  * <pre>
31  * interface IMyControl : IDispatch
32  * {
33  * [propget, id(0)] HRESULT maxFileCount([retval, out] int *c);
34  * [propput, id(0)] HRESULT maxFileCount([in] int c);
35  * [id(1)] HRESULT AddFile([in] BSTR fileName);
36  * };
37  * </pre>
38  *
39  * <p>An example of how to interact with this extended functionality is shown below:
40  *
41  * <code><pre>
42  * OleAutomation automation = new OleAutomation(myControlSite);
43  *
44  * // Look up the ID of the maxFileCount parameter
45  * int[] rgdispid = automation.getIDsOfNames(new String[]{"maxFileCount"});
46  * int maxFileCountID = rgdispid[0];
47  *
48  * // Set the property maxFileCount to 100:
49  * if (automation.setProperty(maxFileCountID, new Variant(100))) {
50  * System.out.println("Max File Count was successfully set.");
51  * }
52  *
53  * // Get the new value of the maxFileCount parameter:
54  * Variant pVarResult = automation.getProperty(maxFileCountID);
55  * if (pVarResult != null) {
56  * System.out.println("Max File Count is "+pVarResult.getInt());
57  * }
58  *
59  * // Invoke the AddFile method
60  * // Look up the IDs of the AddFile method and its parameter
61  * rgdispid = automation.getIDsOfNames(new String[]{"AddFile", "fileName"});
62  * int dispIdMember = rgdispid[0];
63  * int[] rgdispidNamedArgs = new int[] {rgdispid[1]};
64  *
65  * // Convert arguments to Variant objects
66  * Variant[] rgvarg = new Variant[1];
67  * String fileName = "C:\\testfile";
68  * rgvarg[0] = new Variant(fileName);
69  *
70  * // Call the method
71  * Variant pVarResult = automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
72  *
73  * // Check the return value
74  * if (pVarResult == null || pVarResult.getInt() != OLE.S_OK){
75  * System.out.println("Failed to add file "+fileName);
76  * }
77  *
78  * automation.dispose();
79  *
80  * </pre></code>
81  */

82 public final class OleAutomation {
83     private IDispatch objIDispatch;
84     private String JavaDoc exceptionDescription;
85     private ITypeInfo objITypeInfo;
86     
87 OleAutomation(IDispatch idispatch) {
88     if (idispatch == null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS);
89     objIDispatch = idispatch;
90     objIDispatch.AddRef();
91     
92     int[] ppv = new int[1];
93     int result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, ppv);
94     if (result == OLE.S_OK) {
95         objITypeInfo = new ITypeInfo(ppv[0]);
96         objITypeInfo.AddRef();
97     }
98 }
99 /**
100  * Creates an OleAutomation object for the specified client.
101  *
102  * @param clientSite the site for the OLE Document or ActiveX Control whose additional functionality
103  * you need to access
104  *
105  * @exception IllegalArgumentException <ul>
106  * <li>ERROR_INVALID_INTERFACE_ADDRESS when called with an invalid client site
107  * </ul>
108  */

109  public OleAutomation(OleClientSite clientSite) {
110     if (clientSite == null) OLE.error(OLE.ERROR_INVALID_INTERFACE_ADDRESS);
111     objIDispatch = clientSite.getAutomationObject();
112
113     int[] ppv = new int[1];
114     int result = objIDispatch.GetTypeInfo(0, COM.LOCALE_USER_DEFAULT, ppv);
115     if (result == OLE.S_OK) {
116         objITypeInfo = new ITypeInfo(ppv[0]);
117         objITypeInfo.AddRef();
118     }
119  }
120 /**
121  * Disposes the automation object.
122  * <p>
123  * This method releases the IDispatch interface on the OLE Document or ActiveX Control.
124  * Do not use the OleAutomation object after it has been disposed.
125  */

126 public void dispose() {
127
128     if (objIDispatch != null){
129         objIDispatch.Release();
130     }
131     objIDispatch = null;
132     
133     if (objITypeInfo != null){
134         objITypeInfo.Release();
135     }
136     objITypeInfo = null;
137
138 }
139 int getAddress() {
140     return objIDispatch.getAddress();
141 }
142 public String JavaDoc getHelpFile(int dispId) {
143     if (objITypeInfo == null) return null;
144     String JavaDoc[] file = new String JavaDoc[1];
145     int rc = objITypeInfo.GetDocumentation(dispId, null, null, null, file );
146     if (rc == OLE.S_OK) return file[0];
147     return null;
148 }
149 public String JavaDoc getDocumentation(int dispId) {
150     if (objITypeInfo == null) return null;
151     String JavaDoc[] doc = new String JavaDoc[1];
152     int rc = objITypeInfo.GetDocumentation(dispId, null, doc, null, null );
153     if (rc == OLE.S_OK) return doc[0];
154     return null;
155 }
156 public OlePropertyDescription getPropertyDescription(int index) {
157     if (objITypeInfo == null) return null;
158     int[] ppVarDesc = new int[1];
159     int rc = objITypeInfo.GetVarDesc(index, ppVarDesc);
160     if (rc != OLE.S_OK) return null;
161     VARDESC vardesc = new VARDESC();
162     COM.MoveMemory(vardesc, ppVarDesc[0], VARDESC.sizeof);
163     
164     OlePropertyDescription data = new OlePropertyDescription();
165     data.id = vardesc.memid;
166     data.name = getName(vardesc.memid);
167     data.type = vardesc.elemdescVar_tdesc_vt;
168     if (data.type == OLE.VT_PTR) {
169         short[] vt = new short[1];
170         COM.MoveMemory(vt, vardesc.elemdescVar_tdesc_union + 4, 2);
171         data.type = vt[0];
172     }
173     data.flags = vardesc.wVarFlags;
174     data.kind = vardesc.varkind;
175     data.description = getDocumentation(vardesc.memid);
176     data.helpFile = getHelpFile(vardesc.memid);
177     
178     objITypeInfo.ReleaseVarDesc(ppVarDesc[0]);
179     return data;
180 }
181 public OleFunctionDescription getFunctionDescription(int index) {
182     if (objITypeInfo == null) return null;
183     int[] ppFuncDesc = new int[1];
184     int rc = objITypeInfo.GetFuncDesc(index, ppFuncDesc);
185     if (rc != OLE.S_OK) return null;
186     FUNCDESC funcdesc = new FUNCDESC();
187     COM.MoveMemory(funcdesc, ppFuncDesc[0], FUNCDESC.sizeof);
188     
189     OleFunctionDescription data = new OleFunctionDescription();
190     
191     data.id = funcdesc.memid;
192     data.optionalArgCount = funcdesc.cParamsOpt;
193     data.invokeKind = funcdesc.invkind;
194     data.funcKind = funcdesc.funckind;
195     data.flags = funcdesc.wFuncFlags;
196     data.callingConvention = funcdesc.callconv;
197     data.documentation = getDocumentation(funcdesc.memid);
198     data.helpFile = getHelpFile(funcdesc.memid);
199     
200     String JavaDoc[] names = getNames(funcdesc.memid, funcdesc.cParams + 1);
201     if (names.length > 0) {
202         data.name = names[0];
203     }
204     data.args = new OleParameterDescription[funcdesc.cParams];
205     for (int i = 0; i < data.args.length; i++) {
206         data.args[i] = new OleParameterDescription();
207         if (names.length > i + 1) {
208             data.args[i].name = names[i + 1];
209         }
210         short[] vt = new short[1];
211         COM.MoveMemory(vt, funcdesc.lprgelemdescParam + i * 16 + 4, 2);
212         if (vt[0] == OLE.VT_PTR) {
213             int[] pTypedesc = new int[1];
214             COM.MoveMemory(pTypedesc, funcdesc.lprgelemdescParam + i * 16, 4);
215             short[] vt2 = new short[1];
216             COM.MoveMemory(vt2, pTypedesc[0] + 4, 2);
217             vt[0] = (short)(vt2[0] | COM.VT_BYREF);
218         }
219         data.args[i].type = vt[0];
220         short[] wParamFlags = new short[1];
221         COM.MoveMemory(wParamFlags, funcdesc.lprgelemdescParam + i * 16 + 12, 2);
222         data.args[i].flags = wParamFlags[0];
223     }
224     
225     data.returnType = funcdesc.elemdescFunc_tdesc_vt;
226     if (data.returnType == OLE.VT_PTR) {
227         short[] vt = new short[1];
228         COM.MoveMemory(vt, funcdesc.elemdescFunc_tdesc_union + 4, 2);
229         data.returnType = vt[0];
230     }
231
232     objITypeInfo.ReleaseFuncDesc(ppFuncDesc[0]);
233     return data;
234 }
235 public TYPEATTR getTypeInfoAttributes() {
236     if (objITypeInfo == null) return null;
237     int[] ppTypeAttr = new int[1];
238     int rc = objITypeInfo.GetTypeAttr(ppTypeAttr);
239     if (rc != OLE.S_OK) return null;
240     TYPEATTR typeattr = new TYPEATTR();
241     COM.MoveMemory(typeattr, ppTypeAttr[0], TYPEATTR.sizeof);
242     objITypeInfo.ReleaseTypeAttr(ppTypeAttr[0]);
243     return typeattr;
244 }
245 public String JavaDoc getName(int dispId) {
246     if (objITypeInfo == null) return null;
247     String JavaDoc[] name = new String JavaDoc[1];
248     int rc = objITypeInfo.GetDocumentation(dispId, name, null, null, null );
249     if (rc == OLE.S_OK) return name[0];
250     return null;
251 }
252 public String JavaDoc[] getNames(int dispId, int maxSize) {
253     if (objITypeInfo == null) return new String JavaDoc[0];
254     String JavaDoc[] names = new String JavaDoc[maxSize];
255     int[] count = new int[1];
256     int rc = objITypeInfo.GetNames(dispId, names, maxSize, count);
257     if (rc == OLE.S_OK) {
258         String JavaDoc[] newNames = new String JavaDoc[count[0]];
259         System.arraycopy(names, 0, newNames, 0, count[0]);
260         return newNames;
261     }
262     return new String JavaDoc[0];
263 }
264 /**
265  * Returns the positive integer values (IDs) that are associated with the specified names by the
266  * IDispatch implementor. If you are trying to get the names of the parameters in a method, the first
267  * String in the names array must be the name of the method followed by the names of the parameters.
268  *
269  * @param names an array of names for which you require the identifiers
270  *
271  * @return positive integer values that are associated with the specified names in the same
272  * order as the names where provided; or null if the names are unknown
273  */

274 public int[] getIDsOfNames(String JavaDoc[] names) {
275
276     int[] rgdispid = new int[names.length];
277     int result = objIDispatch.GetIDsOfNames(new GUID(), names, names.length, COM.LOCALE_USER_DEFAULT, rgdispid);
278     if (result != COM.S_OK) return null;
279     
280     return rgdispid;
281 }
282 /**
283  * Returns a description of the last error encountered.
284  *
285  * @return a description of the last error encountered
286  */

287 public String JavaDoc getLastError() {
288     
289     return exceptionDescription;
290
291 }
292 /**
293  * Returns the value of the property specified by the dispIdMember.
294  *
295  * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the
296  * value for the ID can be obtained using OleAutomation.getIDsOfNames
297  *
298  * @return the value of the property specified by the dispIdMember or null
299  */

300 public Variant getProperty(int dispIdMember) {
301     Variant pVarResult = new Variant();
302     int result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, null, null, pVarResult);
303     return (result == OLE.S_OK) ? pVarResult : null;
304 }
305 /**
306  * Returns the value of the property specified by the dispIdMember.
307  *
308  * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the
309  * value for the ID can be obtained using OleAutomation.getIDsOfNames
310  *
311  * @param rgvarg an array of arguments for the method. All arguments are considered to be
312  * read only unless the Variant is a By Reference Variant type.
313  *
314  * @return the value of the property specified by the dispIdMember or null
315  *
316  * @since 2.0
317  */

318 public Variant getProperty(int dispIdMember, Variant[] rgvarg) {
319     Variant pVarResult = new Variant();
320     int result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, rgvarg, null, pVarResult);
321     return (result == OLE.S_OK) ? pVarResult : null;
322     
323 }
324 /**
325  * Returns the value of the property specified by the dispIdMember.
326  *
327  * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the
328  * value for the ID can be obtained using OleAutomation.getIDsOfNames
329  *
330  * @param rgvarg an array of arguments for the method. All arguments are considered to be
331  * read only unless the Variant is a By Reference Variant type.
332  *
333  * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the
334  * parameter IDs must be in the same order as their corresponding values;
335  * all arguments must have an identifier - identifiers can be obtained using
336  * OleAutomation.getIDsOfNames
337  *
338  * @return the value of the property specified by the dispIdMember or null
339  *
340  * @since 2.0
341  */

342 public Variant getProperty(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) {
343     Variant pVarResult = new Variant();
344     int result = invoke(dispIdMember, COM.DISPATCH_PROPERTYGET, rgvarg, rgdispidNamedArgs, pVarResult);
345     return (result == OLE.S_OK) ? pVarResult : null;
346 }
347
348 /**
349  * Invokes a method on the OLE Object; the method has no parameters.
350  *
351  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
352  * value for the ID can be obtained using OleAutomation.getIDsOfNames
353  *
354  * @return the result of the method or null if the method failed to give result information
355  */

356 public Variant invoke(int dispIdMember) {
357     Variant pVarResult = new Variant();
358     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, null, null, pVarResult);
359     return (result == COM.S_OK) ? pVarResult : null;
360 }
361 /**
362  * Invokes a method on the OLE Object; the method has no optional parameters.
363  *
364  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
365  * value for the ID can be obtained using OleAutomation.getIDsOfNames
366  *
367  * @param rgvarg an array of arguments for the method. All arguments are considered to be
368  * read only unless the Variant is a By Reference Variant type.
369  *
370  * @return the result of the method or null if the method failed to give result information
371  */

372 public Variant invoke(int dispIdMember, Variant[] rgvarg) {
373     Variant pVarResult = new Variant();
374     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, null, pVarResult);
375     return (result == COM.S_OK) ? pVarResult : null;
376 }
377 /**
378  * Invokes a method on the OLE Object; the method has optional parameters. It is not
379  * necessary to specify all the optional parameters, only include the parameters for which
380  * you are providing values.
381  *
382  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
383  * value for the ID can be obtained using OleAutomation.getIDsOfNames
384  *
385  * @param rgvarg an array of arguments for the method. All arguments are considered to be
386  * read only unless the Variant is a By Reference Variant type.
387  *
388  * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the
389  * parameter IDs must be in the same order as their corresponding values;
390  * all arguments must have an identifier - identifiers can be obtained using
391  * OleAutomation.getIDsOfNames
392  *
393  * @return the result of the method or null if the method failed to give result information
394  */

395 public Variant invoke(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) {
396     Variant pVarResult = new Variant();
397     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, rgdispidNamedArgs, pVarResult);
398     return (result == COM.S_OK) ? pVarResult : null;
399 }
400 private int invoke(int dispIdMember, int wFlags, Variant[] rgvarg, int[] rgdispidNamedArgs, Variant pVarResult) {
401
402     // get the IDispatch interface for the control
403
if (objIDispatch == null) return COM.E_FAIL;
404     
405     // create a DISPPARAMS structure for the input parameters
406
DISPPARAMS pDispParams = new DISPPARAMS();
407     // store arguments in rgvarg
408
if (rgvarg != null && rgvarg.length > 0) {
409         pDispParams.cArgs = rgvarg.length;
410         pDispParams.rgvarg = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, Variant.sizeof * rgvarg.length);
411         int offset = 0;
412         for (int i = rgvarg.length - 1; i >= 0 ; i--) {
413             rgvarg[i].getData(pDispParams.rgvarg + offset);
414             offset += Variant.sizeof;
415         }
416     }
417
418     // if arguments have ids, store the ids in rgdispidNamedArgs
419
if (rgdispidNamedArgs != null && rgdispidNamedArgs.length > 0) {
420         pDispParams.cNamedArgs = rgdispidNamedArgs.length;
421         pDispParams.rgdispidNamedArgs = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, 4 * rgdispidNamedArgs.length);
422         int offset = 0;
423         for (int i = rgdispidNamedArgs.length; i > 0; i--) {
424             COM.MoveMemory(pDispParams.rgdispidNamedArgs + offset, new int[] {rgdispidNamedArgs[i-1]}, 4);
425             offset += 4;
426         }
427     }
428
429     // invoke the method
430
EXCEPINFO excepInfo = new EXCEPINFO();
431     int[] pArgErr = new int[1];
432     int pVarResultAddress = 0;
433     if (pVarResult != null) pVarResultAddress = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, Variant.sizeof);
434     int result = objIDispatch.Invoke(dispIdMember, new GUID(), COM.LOCALE_USER_DEFAULT, wFlags, pDispParams, pVarResultAddress, excepInfo, pArgErr);
435
436     if (pVarResultAddress != 0){
437         pVarResult.setData(pVarResultAddress);
438         COM.VariantClear(pVarResultAddress);
439         OS.GlobalFree(pVarResultAddress);
440     }
441     
442     // free the Dispparams resources
443
if (pDispParams.rgdispidNamedArgs != 0){
444         OS.GlobalFree(pDispParams.rgdispidNamedArgs);
445     }
446     if (pDispParams.rgvarg != 0) {
447         int offset = 0;
448         for (int i = 0, length = rgvarg.length; i < length; i++){
449             COM.VariantClear(pDispParams.rgvarg + offset);
450             offset += Variant.sizeof;
451         }
452         OS.GlobalFree(pDispParams.rgvarg);
453     }
454
455     // save error string and cleanup EXCEPINFO
456
manageExcepinfo(result, excepInfo);
457         
458     return result;
459 }
460 /**
461  * Invokes a method on the OLE Object; the method has no parameters. In the early days of OLE,
462  * the IDispatch interface was not well defined and some applications (mainly Word) did not support
463  * a return value. For these applications, call this method instead of calling
464  * <code>public void invoke(int dispIdMember)</code>.
465  *
466  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
467  * value for the ID can be obtained using OleAutomation.getIDsOfNames
468  *
469  * @exception org.eclipse.swt.SWTException <ul>
470  * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails
471  * </ul>
472  */

473 public void invokeNoReply(int dispIdMember) {
474     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, null, null, null);
475     if (result != COM.S_OK)
476         OLE.error(OLE.ERROR_ACTION_NOT_PERFORMED, result);
477 }
478 /**
479  * Invokes a method on the OLE Object; the method has no optional parameters. In the early days of OLE,
480  * the IDispatch interface was not well defined and some applications (mainly Word) did not support
481  * a return value. For these applications, call this method instead of calling
482  * <code>public void invoke(int dispIdMember, Variant[] rgvarg)</code>.
483  *
484  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
485  * value for the ID can be obtained using OleAutomation.getIDsOfNames
486  *
487  * @param rgvarg an array of arguments for the method. All arguments are considered to be
488  * read only unless the Variant is a By Reference Variant type.
489  *
490  * @exception org.eclipse.swt.SWTException <ul>
491  * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails
492  * </ul>
493  */

494 public void invokeNoReply(int dispIdMember, Variant[] rgvarg) {
495     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, null, null);
496     if (result != COM.S_OK)
497         OLE.error(OLE.ERROR_ACTION_NOT_PERFORMED, result);
498 }
499 /**
500  * Invokes a method on the OLE Object; the method has optional parameters. It is not
501  * necessary to specify all the optional parameters, only include the parameters for which
502  * you are providing values. In the early days of OLE, the IDispatch interface was not well
503  * defined and some applications (mainly Word) did not support a return value. For these
504  * applications, call this method instead of calling
505  * <code>public void invoke(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs)</code>.
506  *
507  * @param dispIdMember the ID of the method as specified by the IDL of the ActiveX Control; the
508  * value for the ID can be obtained using OleAutomation.getIDsOfNames
509  *
510  * @param rgvarg an array of arguments for the method. All arguments are considered to be
511  * read only unless the Variant is a By Reference Variant type.
512  *
513  * @param rgdispidNamedArgs an array of identifiers for the arguments specified in rgvarg; the
514  * parameter IDs must be in the same order as their corresponding values;
515  * all arguments must have an identifier - identifiers can be obtained using
516  * OleAutomation.getIDsOfNames
517  *
518  * @exception org.eclipse.swt.SWTException <ul>
519  * <li>ERROR_ACTION_NOT_PERFORMED when method invocation fails
520  * </ul>
521  */

522 public void invokeNoReply(int dispIdMember, Variant[] rgvarg, int[] rgdispidNamedArgs) {
523     int result = invoke(dispIdMember, COM.DISPATCH_METHOD, rgvarg, rgdispidNamedArgs, null);
524     if (result != COM.S_OK)
525         OLE.error(OLE.ERROR_ACTION_NOT_PERFORMED, result);
526 }
527 private void manageExcepinfo(int hResult, EXCEPINFO excepInfo) {
528
529     if (hResult == COM.S_OK){
530         exceptionDescription = "No Error"; //$NON-NLS-1$
531
return;
532     }
533
534     // extract exception info
535
if (hResult == COM.DISP_E_EXCEPTION) {
536         if (excepInfo.bstrDescription != 0){
537             int size = COM.SysStringByteLen(excepInfo.bstrDescription);
538             char[] buffer = new char[(size + 1) /2];
539             COM.MoveMemory(buffer, excepInfo.bstrDescription, size);
540             exceptionDescription = new String JavaDoc(buffer);
541         } else {
542             exceptionDescription = "OLE Automation Error Exception "; //$NON-NLS-1$
543
if (excepInfo.wCode != 0){
544                 exceptionDescription += "code = "+excepInfo.wCode; //$NON-NLS-1$
545
} else if (excepInfo.scode != 0){
546                 exceptionDescription += "code = "+excepInfo.scode; //$NON-NLS-1$
547
}
548         }
549     } else {
550         exceptionDescription = "OLE Automation Error HResult : " + hResult; //$NON-NLS-1$
551
}
552
553     // cleanup EXCEPINFO struct
554
if (excepInfo.bstrDescription != 0)
555         COM.SysFreeString(excepInfo.bstrDescription);
556     if (excepInfo.bstrHelpFile != 0)
557         COM.SysFreeString(excepInfo.bstrHelpFile);
558     if (excepInfo.bstrSource != 0)
559         COM.SysFreeString(excepInfo.bstrSource);
560 }
561 /**
562  * Sets the property specified by the dispIdMember to a new value.
563  *
564  * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the
565  * value for the ID can be obtained using OleAutomation.getIDsOfNames
566  * @param rgvarg the new value of the property
567  *
568  * @return true if the operation was successful
569  */

570 public boolean setProperty(int dispIdMember, Variant rgvarg) {
571     Variant[] rgvarg2 = new Variant[] {rgvarg};
572     int[] rgdispidNamedArgs = new int[] {COM.DISPID_PROPERTYPUT};
573     int dwFlags = COM.DISPATCH_PROPERTYPUT;
574     if ((rgvarg.getType() & COM.VT_BYREF) == COM.VT_BYREF)
575         dwFlags = COM.DISPATCH_PROPERTYPUTREF;
576     Variant pVarResult = new Variant();
577     int result = invoke(dispIdMember, dwFlags, rgvarg2, rgdispidNamedArgs, pVarResult);
578     return (result == COM.S_OK);
579 }
580 /**
581  * Sets the property specified by the dispIdMember to a new value.
582  *
583  * @param dispIdMember the ID of the property as specified by the IDL of the ActiveX Control; the
584  * value for the ID can be obtained using OleAutomation.getIDsOfNames
585  * @param rgvarg an array of arguments for the method. All arguments are considered to be
586  * read only unless the Variant is a By Reference Variant type.
587  *
588  * @return true if the operation was successful
589  *
590  * @since 2.0
591  */

592 public boolean setProperty(int dispIdMember, Variant[] rgvarg) {
593     int[] rgdispidNamedArgs = new int[] {COM.DISPID_PROPERTYPUT};
594     int dwFlags = COM.DISPATCH_PROPERTYPUT;
595     for (int i = 0; i < rgvarg.length; i++) {
596         if ((rgvarg[i].getType() & COM.VT_BYREF) == COM.VT_BYREF)
597         dwFlags = COM.DISPATCH_PROPERTYPUTREF;
598     }
599     Variant pVarResult = new Variant();
600     int result = invoke(dispIdMember, dwFlags, rgvarg, rgdispidNamedArgs, pVarResult);
601     return (result == COM.S_OK);
602 }
603 }
604
Popular Tags