1 /* Copyright (c) 2012 Tobias Wolf, All Rights Reserved
2  *
3  * The contents of this file is dual-licensed under 2
4  * alternative Open Source/Free licenses: LGPL 2.1 or later and
5  * Apache License 2.0. (starting with JNA version 4.0.0).
6  *
7  * You can freely decide which license you want to apply to
8  * the project.
9  *
10  * You may obtain a copy of the LGPL License at:
11  *
12  * http://www.gnu.org/licenses/licenses.html
13  *
14  * A copy is also included in the downloadable source code package
15  * containing JNA, in file "LGPL2.1".
16  *
17  * You may obtain a copy of the Apache License at:
18  *
19  * http://www.apache.org/licenses/
20  *
21  * A copy is also included in the downloadable source code package
22  * containing JNA, in file "AL2.0".
23  */
24 package com.sun.jna.platform.win32;
25 
26 import com.sun.jna.Memory;
27 import java.util.List;
28 
29 import com.sun.jna.Native;
30 import com.sun.jna.Pointer;
31 import com.sun.jna.Structure;
32 import com.sun.jna.Structure.FieldOrder;
33 import com.sun.jna.platform.win32.Guid.GUID;
34 import com.sun.jna.platform.win32.OaIdl.DISPID;
35 import com.sun.jna.platform.win32.OaIdl.SAFEARRAY;
36 import com.sun.jna.platform.win32.OaIdl.SAFEARRAYBOUND;
37 import com.sun.jna.platform.win32.Variant.VARIANT;
38 import com.sun.jna.platform.win32.Variant.VariantArg;
39 import com.sun.jna.platform.win32.WTypes.BSTR;
40 import com.sun.jna.platform.win32.WTypes.VARTYPE;
41 import com.sun.jna.platform.win32.WTypes.VARTYPEByReference;
42 import com.sun.jna.platform.win32.WinBase.SYSTEMTIME;
43 import com.sun.jna.platform.win32.WinDef.LCID;
44 import com.sun.jna.platform.win32.WinDef.LONG;
45 import com.sun.jna.platform.win32.WinDef.PVOID;
46 import com.sun.jna.platform.win32.WinDef.UINT;
47 import com.sun.jna.platform.win32.WinNT.HRESULT;
48 import com.sun.jna.ptr.DoubleByReference;
49 import com.sun.jna.ptr.PointerByReference;
50 import com.sun.jna.win32.StdCallLibrary;
51 import com.sun.jna.win32.W32APIOptions;
52 
53 /**
54  * Oleaut32.dll Interface.
55  *
56  * @author scott.palmer
57  */
58 public interface OleAuto extends StdCallLibrary {
59 
60     /**
61      * The instance.
62      */
63     OleAuto INSTANCE = Native.load("OleAut32", OleAuto.class, W32APIOptions.DEFAULT_OPTIONS);
64 
65     /* Flags for IDispatch::Invoke */
66     /**
67      * The Constant DISPATCH_METHOD.
68      */
69     int DISPATCH_METHOD = 0x1;
70 
71     /**
72      * The Constant DISPATCH_PROPERTYGET.
73      */
74     int DISPATCH_PROPERTYGET = 0x2;
75 
76     /**
77      * The Constant DISPATCH_PROPERTYPUT.
78      */
79     int DISPATCH_PROPERTYPUT = 0x4;
80 
81     /**
82      * The Constant DISPATCH_PROPERTYPUTREF.
83      */
84     int DISPATCH_PROPERTYPUTREF = 0x8;
85 
86     /**
87      * An array that is allocated on the stac.
88      */
89     int FADF_AUTO = 0x0001;
90 
91     /**
92      * An array that is statically allocated.
93      */
94     int FADF_STATIC = 0x0002;
95 
96     /**
97      * An array that is embedded in a structure.
98      */
99     int FADF_EMBEDDED = 0x0004;
100 
101     /**
102      * An array that is embedded in a structure.
103      */
104     int FADF_FIXEDSIZE = 0x0010;
105 
106     /**
107      * An array that is embedded in a structure.
108      */
109     int FADF_RECORD = 0x0020;
110 
111     /**
112      * An array that is embedded in a structure.
113      */
114     int FADF_HAVEIID = 0x0040;
115 
116     /**
117      * An array that has a variant type. The variant type can be retrieved with
118      * SafeArrayGetVartype.
119      */
120     int FADF_HAVEVARTYPE = 0x0080;
121 
122     /**
123      * An array of BSTRs.
124      */
125     int FADF_BSTR = 0x0100;
126 
127     /**
128      * An array of IUnknown*.
129      */
130     int FADF_UNKNOWN = 0x0200;
131 
132     /**
133      * An array of IDispatch*.
134      */
135     int FADF_DISPATCH = 0x0400;
136 
137     /**
138      * An array of VARIANTs.
139      */
140     int FADF_VARIANT = 0x0800;
141 
142     /**
143      * Bits reserved for future use.
144      */
145     int FADF_RESERVED = 0xF008;
146 
147     /**
148      * This function allocates a new string and copies the passed string into
149      * it.
150      *
151      * @param sz Null-terminated UNICODE string to copy.
152      *
153      * @return Null if there is insufficient memory or if a null pointer is
154      *         passed in.
155      */
SysAllocString(String sz)156     BSTR SysAllocString(String sz);
157 
158     /**
159      * This function frees a string allocated previously by SysAllocString,
160      * SysAllocStringByteLen, SysReAllocString, SysAllocStringLen, or
161      * SysReAllocStringLen.
162      *
163      * @param bstr Unicode string that was allocated previously, or NULL.
164      *             Setting this parameter to NULL causes the function to simply
165      *             return.
166      */
SysFreeString(BSTR bstr)167     void SysFreeString(BSTR bstr);
168 
169     /**
170      * Returns the length (in bytes) of a BSTR.
171      *
172      * @param bstr Unicode string that was allocated previously.
173      */
SysStringByteLen(BSTR bstr)174     int SysStringByteLen(BSTR bstr);
175 
176     /**
177      * Returns the length of a BSTR.
178      *
179      * @param bstr Unicode string that was allocated previously.
180      */
SysStringLen(BSTR bstr)181     int SysStringLen(BSTR bstr);
182 
183     /**
184      * The VariantInit function initializes the VARIANTARG by setting the vt
185      * field to VT_EMPTY. Unlike VariantClear, this function does not interpret
186      * the current contents of the VARIANTARG. Use VariantInit to initialize new
187      * local variables of type VARIANTARG (or VARIANT).
188      *
189      * @param pvarg The variant to initialize.
190      */
VariantInit(VARIANT.ByReference pvarg)191     void VariantInit(VARIANT.ByReference pvarg);
192 
193     /**
194      * The VariantInit function initializes the VARIANTARG by setting the vt
195      * field to VT_EMPTY. Unlike VariantClear, this function does not interpret
196      * the current contents of the VARIANTARG. Use VariantInit to initialize new
197      * local variables of type VARIANTARG (or VARIANT).
198      *
199      * @param pvarg The variant to initialize.
200      */
VariantInit(VARIANT pvarg)201     void VariantInit(VARIANT pvarg);
202 
203     /**
204      * First, free any memory that is owned by pvargDest, such as VariantClear
205      * (pvargDest must point to a valid initialized variant, and not simply to
206      * an uninitialized memory location). Then pvargDest receives an exact copy
207      * of the contents of pvargSrc.
208      * <p>
209      * If pvargSrc is a VT_BSTR, a copy of the string is made. If pvargSrcis a
210      * VT_ARRAY, the entire array is copied. If pvargSrc is a VT_DISPATCH or
211      * VT_UNKNOWN, AddRef is called to increment the object's reference count.
212      * <p>
213      * If the variant to be copied is a COM object that is passed by reference,
214      * the vtfield of the pvargSrcparameter is VT_DISPATCH | VT_BYREF or
215      * VT_UNKNOWN | VT_BYREF. In this case, VariantCopy does not increment the
216      * reference count on the referenced object. Because the variant being
217      * copied is a pointer to a reference to an object, VariantCopy has no way
218      * to determine if it is necessary to increment the reference count of the
219      * object. It is therefore the responsibility of the caller to call
220      * IUnknown::AddRef on the object or not, as appropriate.
221      * <p>
222      * Note The VariantCopy method is not threadsafe.
223      *
224      * @param pvargDest [out] The destination variant.
225      * @param pvargSrc [in] The source variant.
226      *
227      * @return the hresult
228      */
VariantCopy(Pointer pvargDest, VARIANT pvargSrc)229     HRESULT VariantCopy(Pointer pvargDest, VARIANT pvargSrc);
230 
231     /**
232      * Use this function to clear variables of type VARIANTARG (or VARIANT)
233      * before the memory containing the VARIANTARG is freed (as when a local
234      * variable goes out of scope).
235      * <p>
236      * The function clears a VARIANTARG by setting the vt field to VT_EMPTY. The
237      * current contents of the VARIANTARG are released first. If the vtfield is
238      * VT_BSTR, the string is freed. If the vtfield is VT_DISPATCH, the object
239      * is released. If the vt field has the VT_ARRAY bit set, the array is
240      * freed.
241      * <p>
242      * If the variant to be cleared is a COM object that is passed by reference,
243      * the vtfield of the pvargparameter is VT_DISPATCH | VT_BYREF or VT_UNKNOWN
244      * | VT_BYREF. In this case, VariantClear does not release the object.
245      * Because the variant being cleared is a pointer to a reference to an
246      * object, VariantClear has no way to determine if it is necessary to
247      * release the object. It is therefore the responsibility of the caller to
248      * release the object or not, as appropriate.
249      * <p>
250      * In certain cases, it may be preferable to clear a variant in code without
251      * calling VariantClear. For example, you can change the type of a VT_I4
252      * variant to another type without calling this function. Safearrays of BSTR
253      * will have SysFreeString called on each element not VariantClear. However,
254      * you must call VariantClear if a VT_type is received but cannot be
255      * handled. Safearrays of variant will also have VariantClear called on each
256      * member. Using VariantClear in these cases ensures that code will continue
257      * to work if Automation adds new variant types in the future.
258      * <p>
259      * Do not use VariantClear on unitialized variants; use VariantInit to
260      * initialize a new VARIANTARG or VARIANT.
261      * <p>
262      * Variants containing arrays with outstanding references cannot be cleared.
263      * Attempts to do so will return an HRESULT containing DISP_E_ARRAYISLOCKED.
264      *
265      * @param pvarg [in, out] The variant to clear.
266      *
267      * @return the hresult
268      */
VariantClear(VARIANT pvarg)269     HRESULT VariantClear(VARIANT pvarg);
270 
271     public static final short VARIANT_NOVALUEPROP = 0x01;
272     /**
273      * For VT_BOOL to VT_BSTR conversions, convert to "True"/"False" instead of
274      * "-1"/"0"
275      */
276     public static final short VARIANT_ALPHABOOL = 0x02;
277     /**
278      * For conversions to/from VT_BSTR, passes LOCALE_NOUSEROVERRIDE to core
279      * coercion routines
280      */
281     public static final short VARIANT_NOUSEROVERRIDE = 0x04;
282     public static final short VARIANT_CALENDAR_HIJRI = 0x08;
283     /**
284      * For VT_BOOL to VT_BSTR and back, convert to local language rather than
285      * English
286      */
287     public static final short VARIANT_LOCALBOOL = 0x10;
288     /**
289      * SOUTHASIA calendar support
290      */
291     public static final short VARIANT_CALENDAR_THAI = 0x20;
292     /**
293      * SOUTHASIA calendar support
294      */
295     public static final short VARIANT_CALENDAR_GREGORIAN = 0x40;
296     /**
297      * NLS function call support
298      */
299     public static final short VARIANT_USE_NLS = 0x80;
300 
301     /**
302      * Converts a variant from one type to another.
303      *
304      * @param pvargDest [out] The destination variant. If this is the same as
305      *                  pvarSrc, the variant will be converted in place.
306      * @param pvarSrc   [in] The variant to convert.
307      * @param wFlags    Combination of the following flags
308      * <table>
309      * <thead>
310      * <tr><th><!--indent under wFlags comment--><div style="visibility: hidden">wFlags</div></th><th>Value</th><th>Meaning</th></tr>
311      * </thead>
312      * <tbody valign="top">
313      * <tr><th></th><td>{@link #VARIANT_NOVALUEPROP}</td><td>Prevents the
314      * function from attempting to coerce an object to a fundamental type by
315      * getting the Value property. Applications should set this flag only if
316      * necessary, because it makes their behavior inconsistent with other
317      * applications.</td></tr>
318      * <tr><th></th><td>{@link #VARIANT_ALPHABOOL}</td><td>Converts a
319      * {@link Variant#VT_BOOL VT_BOOL} value to a string containing either
320      * "True" or "False".</td></tr>
321      * <tr><th></th><td>{@link #VARIANT_NOUSEROVERRIDE}</td><td>For conversions
322      * to or from {@link Variant#VT_BSTR VT_BSTR}, passes LOCALE_NOUSEROVERRIDE
323      * to the core coercion routines.</td></tr>
324      * <tr><th></th><td>{@link #VARIANT_LOCALBOOL}</td><td>For conversions from
325      * {@link Variant#VT_BOOL VT_BOOL} to {@link Variant#VT_BSTR VT_BSTR} and
326      * back, uses the language specified by the locale in use on the local
327      * computer.</td></tr>
328      * </tbody>
329      * </table>
330      * @param vt        The type to convert to. If the return code is
331      *                  {@link WinError#S_OK S_OK}, the vt field of the vargDest
332      *                  is guaranteed to be equal to this value.
333      *
334      * @return This function can return one of these values:
335      * <table>
336      * <thead>
337      * <tr><th>Return code</th><th>Description</th></tr>
338      * </thead>
339      * <tbody valign="top">
340      * <tr><td>{@link WinError#S_OK S_OK}</td><td>Success.</td></tr>
341      * <tr><td>{@link WinError#DISP_E_BADVARTYPE DISP_E_BADVARTYPE}</td><td>The
342      * variant type is not a valid type of variant.</td></tr>
343      * <tr><td>{@link WinError#DISP_E_OVERFLOW DISP_E_OVERFLOW}</td><td>The data
344      * pointed to by pvarSrc does not fit in the destination type.</td></tr>
345      * <tr><td>{@link WinError#DISP_E_TYPEMISMATCH DISP_E_TYPEMISMATCH}</td><td>The
346      * argument could not be coerced to the specified type.</td></tr>
347      * <tr><td>{@link WinError#E_INVALIDARG E_INVALIDARG}</td><td>One of the
348      * arguments is not valid.</td></tr>
349      * <tr><td>{@link WinError#E_OUTOFMEMORY E_OUTOFMEMORY}</td><td>Insufficient
350      * memory to complete the operation.</td></tr>
351      * </tbody>
352      * </table>
353      * </p>
354      * <b>Remarks</b>
355      * </p>
356      * The VariantChangeType function handles coercions between the fundamental
357      * types (including numeric-to-string and string-to-numeric coercions). The
358      * pvarSrc argument is changed during the conversion process. For example,
359      * if the source variant is of type {@link Variant#VT_BOOL VT_BOOL} and the
360      * destination is of type {@link Variant#VT_UINT VT_UINT}, the pvarSrc
361      * argument is first converted to {@link Variant#VT_I2 VT_I2} and then the
362      * conversion proceeds. A variant that has {@link Variant#VT_BYREF VT_BYREF}
363      * set is coerced to a value by obtaining the referenced value. An object is
364      * coerced to a value by invoking the object's Value property
365      * ({@link OaIdl#DISPID_VALUE DISPID_VALUE}).
366      * </p>
367      * Typically, the implementor of
368      * {@link com.sun.jna.platform.win32.COM.IDispatch#Invoke IDispatch.Invoke}
369      * determines which member is being accessed, and then calls
370      * VariantChangeType to get the value of one or more arguments. For example,
371      * if the IDispatch call specifies a SetTitle member that takes one string
372      * argument, the implementor would call VariantChangeType to attempt to
373      * coerce the argument to {@link Variant#VT_BSTR VT_BSTR}. If
374      * VariantChangeType does not return an error, the argument could then be
375      * obtained directly from the
376      * {@link Variant.VARIANT._VARIANT.__VARIANT#bstrVal bstrVal} field of the
377      * {@link Variant.VARIANT VARIANT}. If VariantChangeType returns
378      * {@link WinError#DISP_E_TYPEMISMATCH DISP_E_TYPEMISMATCH}, the implementor
379      * would set {@link com.sun.jna.platform.win32.COM.IDispatch#Invoke Invoke}
380      * <code> puArgErr</code> parameter referenced value to 0 (indicating the
381      * argument in error) and return DISP_E_TYPEMISMATCH from Invoke.
382      * </p>
383      * Arrays of one type cannot be converted to arrays of another type with
384      * this function.
385      * </p>
386      * <b>Note</b> The type of a {@link Variant.VARIANT VARIANT} should not be
387      * changed in the {@link DISPPARAMS#rgvarg rgvarg} array in place.
388      */
VariantChangeType(VARIANT pvargDest, VARIANT pvarSrc, short wFlags, VARTYPE vt)389     HRESULT VariantChangeType(VARIANT pvargDest, VARIANT pvarSrc, short wFlags, VARTYPE vt);
390 
391     /**
392      * Converts a variant from one type to another.
393      * @param pvargDest [out] The destination variant. If this is the same as
394      *                      pvarSrc, the variant will be converted in place.
395      * @param pvarSrc [in] The variant to convert.
396      * @param wFlags Combination of the following flags
397      * <table>
398      *     <thead>
399      *         <tr><th><!--indent under wFlags comment--><div style="visibility: hidden">wFlags</div></th><th>Value</th><th>Meaning</th></tr>
400      *     </thead>
401      *     <tbody valign="top">
402      *         <tr><th></th><td>{@link #VARIANT_NOVALUEPROP}</td><td>Prevents the function from attempting to coerce an object to a fundamental type by getting the Value property. Applications should set this flag only if necessary, because it makes their behavior inconsistent with other applications.</td></tr>
403      *         <tr><th></th><td>{@link #VARIANT_ALPHABOOL}</td><td>Converts a {@link Variant#VT_BOOL VT_BOOL} value to a string containing either "True" or "False".</td></tr>
404      *         <tr><th></th><td>{@link #VARIANT_NOUSEROVERRIDE}</td><td>For conversions to or from {@link Variant#VT_BSTR VT_BSTR}, passes LOCALE_NOUSEROVERRIDE to the core coercion routines.</td></tr>
405      *         <tr><th></th><td>{@link #VARIANT_LOCALBOOL}</td><td>For conversions from {@link Variant#VT_BOOL VT_BOOL} to {@link Variant#VT_BSTR VT_BSTR} and back, uses the language specified by the locale in use on the local computer.</td></tr>
406      *     </tbody>
407      * </table>
408      * @param vt The type to convert to. If the return code is {@link WinError#S_OK S_OK}, the vt
409      *           field of the vargDest is guaranteed to be equal to this value.
410      * @return This function can return one of these values:
411      * <table>
412      *     <thead>
413      *         <tr><th>Return code</th><th>Description</th></tr>
414      *     </thead>
415      *     <tbody valign="top">
416      *         <tr><td>{@link WinError#S_OK S_OK}</td><td>Success.</td></tr>
417      *         <tr><td>{@link WinError#DISP_E_BADVARTYPE DISP_E_BADVARTYPE}</td><td>The variant type is not a valid type of variant.</td></tr>
418      *         <tr><td>{@link WinError#DISP_E_OVERFLOW DISP_E_OVERFLOW}</td><td>The data pointed to by pvarSrc does not fit in the destination type.</td></tr>
419      *         <tr><td>{@link WinError#DISP_E_TYPEMISMATCH DISP_E_TYPEMISMATCH}</td><td>The argument could not be coerced to the specified type.</td></tr>
420      *         <tr><td>{@link WinError#E_INVALIDARG E_INVALIDARG}</td><td>One of the arguments is not valid.</td></tr>
421      *         <tr><td>{@link WinError#E_OUTOFMEMORY E_OUTOFMEMORY}</td><td>Insufficient memory to complete the operation.</td></tr>
422      *     </tbody>
423      * </table>
424      *</p>
425      * <b>Remarks</b>
426      *</p>
427      * The VariantChangeType function handles coercions between the fundamental
428      * types (including numeric-to-string and string-to-numeric coercions). The
429      * pvarSrc argument is changed during the conversion process. For example,
430      * if the source variant is of type {@link Variant#VT_BOOL VT_BOOL} and the
431      * destination is of type {@link Variant#VT_UINT VT_UINT}, the pvarSrc
432      * argument is first converted to {@link Variant#VT_I2 VT_I2} and then the
433      * conversion proceeds. A variant that has {@link Variant#VT_BYREF VT_BYREF}
434      * set is coerced to a value by obtaining the referenced value. An object is
435      * coerced to a value by invoking the object's Value property
436      * ({@link OaIdl#DISPID_VALUE DISPID_VALUE}).
437      *</p>
438      * Typically, the implementor of
439      * {@link com.sun.jna.platform.win32.COM.IDispatch#Invoke IDispatch.Invoke}
440      * determines which member is being accessed, and then calls
441      * VariantChangeType to get the value of one or more arguments. For example,
442      * if the IDispatch call specifies a SetTitle member that takes one string
443      * argument, the implementor would call VariantChangeType to attempt to
444      * coerce the argument to {@link Variant#VT_BSTR VT_BSTR}. If
445      * VariantChangeType does not return an error, the argument could then be
446      * obtained directly from the
447      * {@link Variant.VARIANT._VARIANT.__VARIANT#bstrVal bstrVal} field of the
448      * {@link Variant.VARIANT VARIANT}. If VariantChangeType returns
449      * {@link WinError#DISP_E_TYPEMISMATCH DISP_E_TYPEMISMATCH}, the implementor
450      * would set {@link com.sun.jna.platform.win32.COM.IDispatch#Invoke Invoke}
451      * <code> puArgErr</code> parameter referenced value to 0 (indicating the
452      * argument in error) and return DISP_E_TYPEMISMATCH from Invoke.
453      *</p>
454      * Arrays of one type cannot be converted to arrays of another type with
455      * this function.
456      *</p>
457      * <b>Note</b> The type of a {@link Variant.VARIANT VARIANT} should not be
458      * changed in the {@link DISPPARAMS#rgvarg rgvarg} array in place.
459      */
VariantChangeType(VARIANT.ByReference pvargDest, VARIANT.ByReference pvarSrc, short wFlags, VARTYPE vt)460     HRESULT VariantChangeType(VARIANT.ByReference pvargDest, VARIANT.ByReference pvarSrc, short wFlags, VARTYPE vt);
461 
462 
463     /**
464      * Creates a new array descriptor, allocates and initializes the data for
465      * the array, and returns a pointer to the new array descriptor.
466      *
467      * @param vt [in] The base type of the array (the VARTYPE of each
468      *                  element of the array). The VARTYPE is restricted to a
469      *                  subset of the variant types. Neither the VT_ARRAY nor
470      *                  the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are
471      *                  not valid base types for the array. All other types are
472      *                  legal. cDims
473      *
474      * @param cDims the number of dims
475      * @param rgsabound the rgsabound
476      *
477      * @return Return value
478      *
479      * A safe array descriptor, or null if the array could not be created.
480      */
SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND[] rgsabound)481     SAFEARRAY.ByReference SafeArrayCreate(VARTYPE vt, UINT cDims,
482             SAFEARRAYBOUND[] rgsabound);
483 
484     /**
485      * Stores the data element at the specified location in the array.
486      *
487      * @param psa [in] An array descriptor created by SafeArrayCreate.
488      * @param idx the idx
489      * @param pv [in] The data to assign to the array. The variant types
490      *            VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not
491      *            require another level of indirection.
492      *
493      * @return Return value
494      *
495      * This function can return one of these values.
496      *
497      * <dl>
498      * <dt>S_OK</dt><dd>Success.</dd>
499      * <dt>DISP_E_BADINDEX</dt><dd>The specified index is not valid.</dd>
500      * <dt>E_INVALIDARG</dt><dd>One of the arguments is not valid.</dd>
501      * <dt>E_OUTOFMEMORY</dt><dd>Memory could not be allocated for the
502      * element.</dd>
503      * </dl>
504      */
SafeArrayPutElement(SAFEARRAY psa, LONG[] idx, Pointer pv)505     HRESULT SafeArrayPutElement(SAFEARRAY psa, LONG[] idx, Pointer pv);
506 
507     /**
508      * Retrieve the upper bound for the specified dimension of the supplied
509      * array
510      *
511      * @param psa [in] An array descriptor created by SafeArrayCreate.
512      * @param nDim [in] the dimension, one based
513      * @param bound [out] upper bound for the supplied dimension
514      *
515      * @return Return value
516      *
517      * This function can return one of these values.
518      *
519      * <dl>
520      * <dt>S_OK</dt><dd>Success.</dd>
521      * <dt>DISP_E_BADINDEX</dt><dd>The specified index is not valid.</dd>
522      * <dt>E_INVALIDARG</dt><dd>One of the arguments is not valid.</dd>
523      * </dl>
524      */
SafeArrayGetUBound(SAFEARRAY psa, UINT nDim, WinDef.LONGByReference bound)525     HRESULT SafeArrayGetUBound(SAFEARRAY psa, UINT nDim, WinDef.LONGByReference bound);
526 
527     /**
528      * Retrieve the lower bound for the specified dimension of the supplied
529      * array
530      *
531      * @param psa [in] An array descriptor created by SafeArrayCreate.
532      * @param nDim [in] the dimension, one based
533      * @param bound [out] lower bound for the supplied dimension
534      *
535      * @return Return value
536      *
537      * This function can return one of these values.
538      *
539      * <dl>
540      * <dt>S_OK</dt><dd>Success.</dd>
541      * <dt>DISP_E_BADINDEX</dt><dd>The specified index is not valid.</dd>
542      * <dt>E_INVALIDARG</dt><dd>One of the arguments is not valid.</dd>
543      * </dl>
544      */
SafeArrayGetLBound(SAFEARRAY psa, UINT nDim, WinDef.LONGByReference bound)545     HRESULT SafeArrayGetLBound(SAFEARRAY psa, UINT nDim, WinDef.LONGByReference bound);
546 
547     /**
548      * Retrieves a single element of the array.
549      * <p>
550      * The array is automaticly locked via SafeArrayLock and SafeArrayUnlock.
551      *
552      * @param psa [in] An array descriptor created by SafeArrayCreate.
553      * @param rgIndices [in] A vector of indexes for each dimension of the
554      *                  array. The right-most (least significant) dimension is
555      *                  rgIndices[0]. The left-most dimension is stored at
556      *                  rgIndices[psa-&gt;cDims - 1].
557      * @param pv [out] The element of the array.
558      *
559      * @return Return value
560      *
561      * This function can return one of these values.
562      *
563      * <dl>
564      * <dt>S_OK</dt><dd>Success.</dd>
565      * <dt>DISP_E_BADINDEX</dt><dd>The specified index is not valid.</dd>
566      * <dt>E_INVALIDARG</dt><dd>One of the arguments is not valid.</dd>
567      * <dt>E_OUTOFMEMORY</dt><dd>Memory could not be allocated for the
568      * element.</dd>
569      * </dl>
570      */
SafeArrayGetElement(SAFEARRAY psa, LONG[] rgIndices, Pointer pv)571     HRESULT SafeArrayGetElement(SAFEARRAY psa, LONG[] rgIndices, Pointer pv);
572 
573     /**
574      * Retrieves the pointer to a single element of the array.
575      * <p>
576      * <p>
577      * The caller is responsible for locking.</p>
578      *
579      * @param psa [in] An array descriptor created by SafeArrayCreate.
580      * @param rgIndices [in] A vector of indexes for each dimension of the
581      *                  array. The right-most (least significant) dimension is
582      *                  rgIndices[0]. The left-most dimension is stored at
583      *                  rgIndices[psa-&gt;cDims - 1].
584      * @param ppv [out] The element of the array.
585      *
586      * @return Return value
587      *
588      * This function can return one of these values.
589      *
590      * <dl>
591      * <dt>S_OK</dt><dd>Success.</dd>
592      * <dt>DISP_E_BADINDEX</dt><dd>The specified index is not valid.</dd>
593      * <dt>E_INVALIDARG</dt><dd>One of the arguments is not valid.</dd>
594      * </dl>
595      */
SafeArrayPtrOfIndex(SAFEARRAY psa, LONG[] rgIndices, PointerByReference ppv)596     HRESULT SafeArrayPtrOfIndex(SAFEARRAY psa, LONG[] rgIndices, PointerByReference ppv);
597 
598     /**
599      * Increments the lock count of an array, and places a pointer to the array
600      * data in pvData of the array descriptor.
601      *
602      * @param psa [in] An array descriptor created by SafeArrayCreate.
603      *
604      * @return Return value
605      *
606      * This function can return one of these values.
607      *
608      * <dl>
609      * <dt>S_OK</dt><dd>Success.</dd>
610      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
611      * <dt>E_UNEXPECTED</dt><dd>The array could not be locked.</dd>
612      * </dl>
613      */
SafeArrayLock(SAFEARRAY psa)614     HRESULT SafeArrayLock(SAFEARRAY psa);
615 
616     /**
617      * Decrements the lock count of an array so it can be freed or resized.
618      *
619      * @param psa [in] An array descriptor created by SafeArrayCreate.
620      *
621      * @return Return value
622      *
623      * This function can return one of these values.
624      *
625      * <dl>
626      * <dt>S_OK</dt><dd>Success.</dd>
627      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
628      * <dt>E_UNEXPECTED</dt><dd>The array could not be locked.</dd>
629      * </dl>
630      */
SafeArrayUnlock(SAFEARRAY psa)631     HRESULT SafeArrayUnlock(SAFEARRAY psa);
632 
633     /**
634      * Destroys an existing array descriptor and all of the data in the array.
635      * If objects are stored in the array, Release is called on each object in
636      * the array.
637      *
638      * @param psa [in] An array descriptor created by SafeArrayCreate.
639      *
640      * @return Return value
641      *
642      * This function can return one of these values.
643      *
644      * <dl>
645      * <dt>S_OK</dt><dd>Success.</dd>
646      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
647      * <dt>DISP_E_ARRAYISLOCKED</dt><dd>The array could not be locked.</dd>
648      * </dl>
649      */
SafeArrayDestroy(SAFEARRAY psa)650     HRESULT SafeArrayDestroy(SAFEARRAY psa);
651 
652     /**
653      * Changes the right-most (least significant) bound of the specified safe
654      * array.
655      *
656      * @param psa [in, out] An array descriptor created by
657      *                    SafeArrayCreate.
658      * @param psaboundNew [in] New bounds for the least significant dimension
659      *
660      * @return Return value
661      *
662      * This function can return one of these values.
663      *
664      * <dl>
665      * <dt>S_OK</dt><dd>Success.</dd>
666      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
667      * <dt>DISP_E_ARRAYISLOCKED</dt><dd>The array could not be locked.</dd>
668      * </dl>
669      */
SafeArrayRedim(SAFEARRAY psa, SAFEARRAYBOUND psaboundNew)670     HRESULT SafeArrayRedim(SAFEARRAY psa, SAFEARRAYBOUND psaboundNew);
671 
672     /**
673      * Return VARTYPE of the SAFEARRAY
674      *
675      * @param psa [in] An array descriptor created by SafeArrayCreate.
676      * @param pvt [in] Vartype of the SAFEARRAY
677      *
678      * @return Return value
679      *
680      * This function can return one of these values.
681      *
682      * <dl>
683      * <dt>S_OK</dt><dd>Success.</dd>
684      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
685      * </dl>
686      */
SafeArrayGetVartype(SAFEARRAY psa, VARTYPEByReference pvt)687     HRESULT SafeArrayGetVartype(SAFEARRAY psa, VARTYPEByReference pvt);
688 
689     /**
690      * Return number of dimensions of the SAFEARRAY
691      *
692      * @param psa [in] An array descriptor created by SafeArrayCreate.
693      *
694      * @return Return count of dimensions
695      */
SafeArrayGetDim(SAFEARRAY psa)696     UINT SafeArrayGetDim(SAFEARRAY psa);
697 
698     /**
699      * Lock array and retrieve pointer to data
700      *
701      * @param psa [in] An array descriptor created by SafeArrayCreate.
702      * @param ppvData [in] pointer to the data array
703      *
704      * @return Return value
705      *
706      * This function can return one of these values.
707      *
708      * <dl>
709      * <dt>S_OK</dt><dd>Success.</dd>
710      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
711      * <dt>E_UNEXPECTED</dt><dd>The array could not be locked.</dd>
712      * </dl>
713      */
SafeArrayAccessData(SAFEARRAY psa, PointerByReference ppvData)714     HRESULT SafeArrayAccessData(SAFEARRAY psa, PointerByReference ppvData);
715 
716     /**
717      * Unlock array and invalidate the pointer retrieved via SafeArrayAccessData
718      *
719      * @param psa [in] An array descriptor created by SafeArrayCreate.
720      *
721      * @return Return value
722      *
723      * This function can return one of these values.
724      *
725      * <dl>
726      * <dt>S_OK</dt><dd>Success.</dd>
727      * <dt>E_INVALIDARG</dt><dd>The argument psa is not valid.</dd>
728      * <dt>E_UNEXPECTED</dt><dd>The array could not be locked.</dd>
729      * </dl>
730      */
SafeArrayUnaccessData(SAFEARRAY psa)731     HRESULT SafeArrayUnaccessData(SAFEARRAY psa);
732 
733     /**
734      * Get size of one element in bytes
735      *
736      * @param psa [in] An array descriptor created by SafeArrayCreate.
737      *
738      * @return size in bytes
739      */
SafeArrayGetElemsize(SAFEARRAY psa)740     UINT SafeArrayGetElemsize(SAFEARRAY psa);
741 
742     /**
743      * Retrieves a pointer to a running object that has been registered with
744      * OLE.
745      *
746      * @param rclsid [in] The class identifier (CLSID) of the active object
747      *                   from the OLE registration database.
748      * @param pvReserved Reserved for future use. Must be null.
749      * @param ppunk [out] The requested active object.
750      *
751      * @return Return value
752      *
753      * If this function succeeds, it returns S_OK. Otherwise, it returns an
754      * HRESULT error code.
755      */
GetActiveObject(GUID rclsid, PVOID pvReserved, PointerByReference ppunk)756     HRESULT GetActiveObject(GUID rclsid, PVOID pvReserved, PointerByReference ppunk);
757 
758     /**
759      * The Class DISPPARAMS.
760      */
761     @FieldOrder({"rgvarg", "rgdispidNamedArgs", "cArgs", "cNamedArgs"})
762     public class DISPPARAMS extends Structure {
763 
764         /**
765          * The Class ByReference.
766          */
767         public static class ByReference extends DISPPARAMS implements
768             Structure.ByReference {
769         }
770 
771         /**
772          * The rgvarg.
773          */
774         public VariantArg.ByReference rgvarg;
775 
776         /**
777          * The rgdispid named args.
778          */
779         public Pointer rgdispidNamedArgs = Pointer.NULL;
780 
781         /**
782          * The c args. - use setArgs to update arguments
783          */
784         public UINT cArgs = new UINT(0);
785 
786         /**
787          * The c named args. - use setRgdispidNamedArgs to update named
788          * arguments map
789          */
790         public UINT cNamedArgs = new UINT(0);
791 
getRgdispidNamedArgs()792         public DISPID[] getRgdispidNamedArgs() {
793             DISPID[] namedArgs = null;
794             int count = cNamedArgs.intValue();
795             if (rgdispidNamedArgs != null && count > 0) {
796                 int[] rawData = rgdispidNamedArgs.getIntArray(0, count);
797                 namedArgs = new DISPID[count];
798                 for (int i = 0; i < count; i++) {
799                     namedArgs[i] = new DISPID(rawData[i]);
800                 }
801             } else {
802                 namedArgs = new DISPID[0];
803             }
804             return namedArgs;
805         }
806 
setRgdispidNamedArgs(DISPID[] namedArgs)807         public void setRgdispidNamedArgs(DISPID[] namedArgs) {
808             if (namedArgs == null) {
809                 namedArgs = new DISPID[0];
810             }
811             cNamedArgs = new UINT(namedArgs.length);
812             rgdispidNamedArgs = new Memory(DISPID.SIZE * namedArgs.length);
813             int[] rawData = new int[namedArgs.length];
814             for (int i = 0; i < rawData.length; i++) {
815                 rawData[i] = namedArgs[i].intValue();
816             }
817             rgdispidNamedArgs.write(0, rawData, 0, namedArgs.length);
818         }
819 
getArgs()820         public VARIANT[] getArgs() {
821             if (this.rgvarg != null) {
822                 this.rgvarg.setArraySize(cArgs.intValue());
823                 return this.rgvarg.variantArg;
824             } else {
825                 return new VARIANT[0];
826             }
827         }
828 
setArgs(VARIANT[] arguments)829         public void setArgs(VARIANT[] arguments) {
830             if (arguments == null) {
831                 arguments = new VARIANT[0];
832             }
833 
834             rgvarg = new VariantArg.ByReference(arguments);
835             cArgs = new UINT(arguments.length);
836         }
837 
838         /**
839          * Instantiates a new dispparams.
840          */
DISPPARAMS()841         public DISPPARAMS() {
842             super();
843         }
844 
845         /**
846          * Instantiates a new dispparams.
847          *
848          * @param memory the memory
849          */
DISPPARAMS(Pointer memory)850         public DISPPARAMS(Pointer memory) {
851             super(memory);
852             this.read();
853         }
854     }
855 
856     /**
857      * Uses registry information to load a type library.
858      *
859      * @param rguid The GUID of the library.
860      * @param wVerMajor The major version of the library.
861      * @param wVerMinor The minor version of the library.
862      * @param lcid The national language code of the library.
863      * @param pptlib The loaded type library.
864      *
865      * This function can return one of these values: S_OK Success.
866      *
867      * E_INVALIDARG One or more of the arguments is not valid.
868      *
869      * E_OUTOFMEMORY Insufficient memory to complete the operation.
870      *
871      * TYPE_E_IOERROR The function could not write to the file.
872      *
873      * TYPE_E_INVALIDSTATE The type library could not be opened.
874      *
875      * TYPE_E_INVDATAREAD The function could not read from the file.
876      *
877      * TYPE_E_UNSUPFORMAT The type library has an older format.
878      *
879      * TYPE_E_UNKNOWNLCID The LCID could not be found in the OLE-supported DLLs.
880      *
881      * TYPE_E_CANTLOADLIBRARY The type library or DLL could not be loaded.
882      *
883      * @return status
884      */
LoadRegTypeLib(GUID rguid, int wVerMajor, int wVerMinor, LCID lcid, PointerByReference pptlib)885     HRESULT LoadRegTypeLib(GUID rguid, int wVerMajor, int wVerMinor, LCID lcid, PointerByReference pptlib);
886 
887     /**
888      * Loads and registers a type library.
889      *
890      * @param szFile The name of the file from which the method should attempt
891      *               to load a type library.
892      *
893      * @param pptlib The loaded type library. Return value
894      *
895      * This function can return one of these values.
896      *
897      * S_OK Success.
898      *
899      * E_INVALIDARG One or more of the arguments is not valid.
900      *
901      * E_OUTOFMEMORY Insufficient memory to complete the operation.
902      *
903      * TYPE_E_IOERROR The function could not write to the file.
904      *
905      * TYPE_E_INVALIDSTATE The type library could not be opened.
906      *
907      * TYPE_E_INVDATAREAD The function could not read from the file.
908      *
909      * TYPE_E_UNSUPFORMAT The type library has an older format.
910      *
911      * TYPE_E_UNKNOWNLCID The LCID could not be found in the OLE-supported DLLs.
912      *
913      * TYPE_E_CANTLOADLIBRARY The type library or DLL could not be loaded.
914      *
915      * @return status
916      */
LoadTypeLib(String szFile, PointerByReference pptlib)917     HRESULT LoadTypeLib(String szFile, PointerByReference pptlib);
918 
919     /**
920      * Converts a system time to a variant representation.
921      *
922      * @param lpSystemTime [in] The system time.
923      *
924      * @param pvtime [out] The variant time.
925      *
926      * @return The function returns TRUE on success and FALSE otherwise.
927      */
SystemTimeToVariantTime(SYSTEMTIME lpSystemTime, DoubleByReference pvtime)928     int SystemTimeToVariantTime(SYSTEMTIME lpSystemTime, DoubleByReference pvtime);
929 }
930