1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.ole.win32;
16 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.ole.win32.COM;
19 import org.eclipse.swt.internal.win32.OS;
20 import org.eclipse.swt.internal.win32.TCHAR;
25 * OLE contains all the constants used to create an ActiveX Control or an OLE Document.
27 * <p>Definitions for these constants can be found in MSDN.
30 public class OLE extends SWT {
32 public static final int S_FALSE = 1; // Used for functions that semantically return a Boolean FALSE result to indicate that the function succeeded.
33 public static final int S_OK = 0; // Function succeeded.
34 public static final int E_FAIL = -2147467259; // Unspecified failure.
35 public static final int E_INVALIDARG = -2147024809; // Invalid argument
36 public static final int E_NOINTERFACE = -2147467262; // QueryInterface did not recognize the requested interface.
37 public static final int E_NOTIMPL = -2147467263; // Not implemented
39 public static final String IID_IUNKNOWN = "{00000000-0000-0000-C000-000000000046}"; //$NON-NLS-1$
40 public static final String IID_IDISPATCH = "{00020400-0000-0000-C000-000000000046}"; //$NON-NLS-1$
42 // Verbs that can be invoked on this client
43 public static final int OLEIVERB_DISCARDUNDOSTATE = -6; // close the OLE object and discard the undo state
44 public static final int OLEIVERB_HIDE = -3; // hide the OLE object
45 public static final int OLEIVERB_INPLACEACTIVATE = -5; // open the OLE for editing in-place
46 public static final int OLEIVERB_OPEN = -2; // open the OLE object for editing in a separate window
47 public static final int OLEIVERB_PRIMARY = 0; // opens the OLE object for editing
48 public static final int OLEIVERB_PROPERTIES = -7; // request the OLE object properties dialog
49 public static final int OLEIVERB_SHOW = -1; // show the OLE object
50 public static final int OLEIVERB_UIACTIVATE = -4; // activate the UI for the OLE object
52 public static final int PROPERTY_CHANGING = 0;
53 public static final int PROPERTY_CHANGED = 1;
56 * Error code for OleError - No specific error information available
58 public static final int HRESULT_UNSPECIFIED = 0;
60 * Error code for OleError - Failed to create file
62 public static final int ERROR_CANNOT_CREATE_FILE = 1000;
64 * Error code for OleError - Failed to create Ole Client
66 public static final int ERROR_CANNOT_CREATE_OBJECT = 1001;
68 * Error code for OleError - File does not exist, is not accessible to user or does not have the correct format
70 public static final int ERROR_CANNOT_OPEN_FILE = 1002;
72 * Error code for OleError - Failed to find requested interface on OLE Object
74 public static final int ERROR_INTERFACE_NOT_FOUND = 1003;
76 * Error code for OleError - Class ID not found in registry
78 public static final int ERROR_INVALID_CLASSID = 1004;
80 * Error code for OleError - Failed to get the class factory for the specified classID
82 public static final int ERROR_CANNOT_ACCESS_CLASSFACTORY = 1005;
84 * Error code for OleError - Failed to create Licensed instance
86 public static final int ERROR_CANNOT_CREATE_LICENSED_OBJECT = 1006;
88 * Error code for OleError - Out of Memory
90 public static final int ERROR_OUT_OF_MEMORY = 1007;
92 * Error code for OleError - Failed to change Variant type
94 public static final int ERROR_CANNOT_CHANGE_VARIANT_TYPE = 1010;
96 * Error code for OleError - Invalid address received for Ole Interface
98 public static final int ERROR_INVALID_INTERFACE_ADDRESS = 1011;
100 * Error code for OleError - Unable to find Application
102 public static final int ERROR_APPLICATION_NOT_FOUND = 1013;
104 * Error code for OleError - Action can not be performed
106 public static final int ERROR_ACTION_NOT_PERFORMED = 1014;
108 public static final int OLECMDF_SUPPORTED = 1;
109 public static final int OLECMDF_ENABLED = 2;
110 public static final int OLECMDF_LATCHED = 4;
111 public static final int OLECMDF_NINCHED = 8;
113 public static final int OLECMDTEXTF_NONE = 0;
114 public static final int OLECMDTEXTF_NAME = 1;
115 public static final int OLECMDTEXTF_STATUS = 2;
117 public static final int OLECMDEXECOPT_DODEFAULT = 0;
118 public static final int OLECMDEXECOPT_PROMPTUSER = 1;
119 public static final int OLECMDEXECOPT_DONTPROMPTUSER = 2;
120 public static final int OLECMDEXECOPT_SHOWHELP = 3;
122 public static final int OLECMDID_OPEN = 1;
123 public static final int OLECMDID_NEW = 2;
124 public static final int OLECMDID_SAVE = 3;
125 public static final int OLECMDID_SAVEAS = 4;
126 public static final int OLECMDID_SAVECOPYAS = 5;
127 public static final int OLECMDID_PRINT = 6;
128 public static final int OLECMDID_PRINTPREVIEW = 7;
129 public static final int OLECMDID_PAGESETUP = 8;
130 public static final int OLECMDID_SPELL = 9;
131 public static final int OLECMDID_PROPERTIES = 10;
132 public static final int OLECMDID_CUT = 11;
133 public static final int OLECMDID_COPY = 12;
134 public static final int OLECMDID_PASTE = 13;
135 public static final int OLECMDID_PASTESPECIAL = 14;
136 public static final int OLECMDID_UNDO = 15;
137 public static final int OLECMDID_REDO = 16;
138 public static final int OLECMDID_SELECTALL = 17;
139 public static final int OLECMDID_CLEARSELECTION = 18;
140 public static final int OLECMDID_ZOOM = 19;
141 public static final int OLECMDID_GETZOOMRANGE = 20;
142 public static final int OLECMDID_UPDATECOMMANDS = 21;
143 public static final int OLECMDID_REFRESH = 22;
144 public static final int OLECMDID_STOP = 23;
145 public static final int OLECMDID_HIDETOOLBARS = 24;
146 public static final int OLECMDID_SETPROGRESSMAX = 25;
147 public static final int OLECMDID_SETPROGRESSPOS = 26;
148 public static final int OLECMDID_SETPROGRESSTEXT = 27;
149 public static final int OLECMDID_SETTITLE = 28;
150 public static final int OLECMDID_SETDOWNLOADSTATE = 29;
151 public static final int OLECMDID_STOPDOWNLOAD = 30;
153 /* Ole Property Description flags */
154 public static int VARFLAG_FREADONLY = 0x1;
155 public static int VARFLAG_FSOURCE = 0x2;
156 public static int VARFLAG_FBINDABLE = 0x4;
157 public static int VARFLAG_FREQUESTEDIT = 0x8;
158 public static int VARFLAG_FDISPLAYBIND = 0x10;
159 public static int VARFLAG_FDEFAULTBIND = 0x20;
160 public static int VARFLAG_FHIDDEN = 0x40;
161 public static int VARFLAG_FRESTRICTED = 0x80;
162 public static int VARFLAG_FDEFAULTCOLLELEM = 0x100;
163 public static int VARFLAG_FUIDEFAULT = 0x200;
164 public static int VARFLAG_FNONBROWSABLE = 0x400;
165 public static int VARFLAG_FREPLACEABLE = 0x800;
166 public static int VARFLAG_FIMMEDIATEBIND = 0x1000;
168 /* Ole Property Description kind */
169 public static int VAR_PERINSTANCE = 0;
170 public static int VAR_STATIC = 1;
171 public static int VAR_CONST = 2;
172 public static int VAR_DISPATCH = 3;
174 /* Ole Parameter Description flags */
175 public static short IDLFLAG_NONE = 0;
176 public static short IDLFLAG_FIN = 1;
177 public static short IDLFLAG_FOUT = 2;
178 public static short IDLFLAG_FLCID = 4;
179 public static short IDLFLAG_FRETVAL = 8;
181 /* Ole Description types */
182 public static final short VT_BOOL = 11; // Boolean; True=-1, False=0.
183 public static final short VT_BSTR = 8; // Binary String.
184 public static final short VT_BYREF = 16384; // By reference - must be combined with one of the other VT values
185 public static final short VT_CY = 6; // Currency.
186 public static final short VT_DATE = 7; // Date.
187 public static final short VT_DISPATCH = 9; // IDispatch
188 public static final short VT_EMPTY = 0; // Not specified.
189 public static final short VT_ERROR = 10; // Scodes.
190 public static final short VT_I2 = 2; // 2-byte signed int.
191 public static final short VT_I4 = 3; // 4-byte signed int.
192 public static final short VT_NULL = 1; // Null.
193 public static final short VT_R4 = 4; // 4-byte real.
194 public static final short VT_R8 = 5; // 8-byte real.
195 public static final short VT_UI1 = 17; // Unsigned char.
196 public static final short VT_UI4 = 19; // Unsigned int.
197 public static final short VT_UNKNOWN = 13; // IUnknown FAR*.
198 public static final short VT_VARIANT = 12; // VARIANT FAR*.
199 public static final short VT_PTR = 26;
200 public static final short VT_USERDEFINED = 29;
201 public static final short VT_HRESULT = 25;
202 public static final short VT_DECIMAL = 14;
203 public static final short VT_I1 = 16;
204 public static final short VT_UI2 = 18;
205 public static final short VT_I8 = 20;
206 public static final short VT_UI8 = 21;
207 public static final short VT_INT = 22;
208 public static final short VT_UINT = 23;
209 public static final short VT_VOID = 24;
210 public static final short VT_SAFEARRAY = 27;
211 public static final short VT_CARRAY = 28;
212 public static final short VT_LPSTR = 30;
213 public static final short VT_LPWSTR = 31;
214 public static final short VT_RECORD = 36;
215 public static final short VT_FILETIME = 64;
216 public static final short VT_BLOB = 65;
217 public static final short VT_STREAM = 66;
218 public static final short VT_STORAGE = 67;
219 public static final short VT_STREAMED_OBJECT = 68;
220 public static final short VT_STORED_OBJECT = 69;
221 public static final short VT_BLOB_OBJECT = 70;
222 public static final short VT_CF = 71;
223 public static final short VT_CLSID = 72;
224 public static final short VT_VERSIONED_STREAM = 73;
225 public static final short VT_BSTR_BLOB = 0xfff;
226 public static final short VT_VECTOR = 0x1000;
227 public static final short VT_ARRAY = 0x2000;
229 /* Ole Function Description Invoke Kind values */
230 public static final int INVOKE_FUNC = 1;
231 public static final int INVOKE_PROPERTYGET = 2;
232 public static final int INVOKE_PROPERTYPUT = 4;
233 public static final int INVOKE_PROPERTYPUTREF = 8;
235 /* Ole Function Description function kind */
236 public static final int FUNC_VIRTUAL = 0;
237 public static final int FUNC_PUREVIRTUAL = 1;
238 public static final int FUNC_NONVIRTUAL = 2;
239 public static final int FUNC_STATIC = 3;
240 public static final int FUNC_DISPATCH = 4;
242 /* Ole Function Description function flags */
243 public static final short FUNCFLAG_FRESTRICTED = 1;
244 public static final short FUNCFLAG_FSOURCE = 0x2;
245 public static final short FUNCFLAG_FBINDABLE = 0x4;
246 public static final short FUNCFLAG_FREQUESTEDIT = 0x8;
247 public static final short FUNCFLAG_FDISPLAYBIND = 0x10;
248 public static final short FUNCFLAG_FDEFAULTBIND = 0x20;
249 public static final short FUNCFLAG_FHIDDEN = 0x40;
250 public static final short FUNCFLAG_FUSESGETLASTERROR = 0x80;
251 public static final short FUNCFLAG_FDEFAULTCOLLELEM = 0x100;
252 public static final short FUNCFLAG_FUIDEFAULT = 0x200;
253 public static final short FUNCFLAG_FNONBROWSABLE = 0x400;
254 public static final short FUNCFLAG_FREPLACEABLE = 0x800;
255 public static final short FUNCFLAG_FIMMEDIATEBIND = 0x1000;
257 /* Ole Function Description calling convention */
258 public static final int CC_FASTCALL = 0;
259 public static final int CC_CDECL = 1;
260 public static final int CC_MSCPASCAL = 2;
261 public static final int CC_PASCAL = 2;
262 public static final int CC_MACPASCAL = 3;
263 public static final int CC_STDCALL = 4;
264 public static final int CC_FPFASTCALL = 5;
265 public static final int CC_SYSCALL = 6;
266 public static final int CC_MPWCDECL = 7;
267 public static final int CC_MPWPASCAL = 8;
268 public static final int CC_MAX = 9;
270 static final String ERROR_NOT_IMPLEMENTED_MSG = "Required functionality not currently supported.";//$NON-NLS-1$
271 static final String ERROR_CANNOT_CREATE_FILE_MSG = "Failed to create file.";//$NON-NLS-1$
272 static final String ERROR_CANNOT_CREATE_OBJECT_MSG = "Failed to create Ole Client.";//$NON-NLS-1$
273 static final String ERROR_CANNOT_OPEN_FILE_MSG = "File does not exist, is not accessible to user or does not have the correct format.";//$NON-NLS-1$
274 static final String ERROR_INTERFACE_NOT_FOUND_MSG = "Failed to find requested interface on OLE Object.";//$NON-NLS-1$
275 static final String ERROR_INVALID_CLASSID_MSG = "Class ID not found in registry";//$NON-NLS-1$
276 static final String ERROR_CANNOT_ACCESS_CLASSFACTORY_MSG = "Failed to get the class factory for the specified classID";//$NON-NLS-1$
277 static final String ERROR_CANNOT_CREATE_LICENSED_OBJECT_MSG = "Failed to create Licensed instance";//$NON-NLS-1$
278 static final String ERROR_OUT_OF_MEMORY_MSG = "Out of Memory";//$NON-NLS-1$
279 static final String ERROR_CANNOT_CHANGE_VARIANT_TYPE_MSG = "Failed to change Variant type";//$NON-NLS-1$
280 static final String ERROR_INVALID_INTERFACE_ADDRESS_MSG = "Invalid address received for Ole Interface.";//$NON-NLS-1$
281 static final String ERROR_APPLICATION_NOT_FOUND_MSG = "Unable to find Application.";//$NON-NLS-1$
282 static final String ERROR_ACTION_NOT_PERFORMED_MSG = "Action can not be performed.";//$NON-NLS-1$
285 public static void error (int code) {
288 public static void error (int code, int hresult) {
291 /* Illegal Arguments (non-fatal) */
292 case ERROR_INVALID_INTERFACE_ADDRESS :{
293 throw new IllegalArgumentException (ERROR_INVALID_INTERFACE_ADDRESS_MSG);
296 /* SWT Errors (non-fatal) */
297 case ERROR_CANNOT_CREATE_FILE : {
298 String msg = ERROR_CANNOT_CREATE_FILE_MSG;
299 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
300 throw new SWTException (code, msg);
302 case ERROR_CANNOT_CREATE_OBJECT : {
303 String msg = ERROR_CANNOT_CREATE_OBJECT_MSG;
304 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
305 throw new SWTException (code, msg);//$NON-NLS-1$
307 case ERROR_CANNOT_OPEN_FILE : {
308 String msg = ERROR_CANNOT_OPEN_FILE_MSG;
309 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
310 throw new SWTException (code, msg);
312 case ERROR_INTERFACE_NOT_FOUND : {
313 String msg = ERROR_INTERFACE_NOT_FOUND_MSG;
314 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
315 throw new SWTException (code, msg);
317 case ERROR_INVALID_CLASSID : {
318 String msg = ERROR_INVALID_CLASSID_MSG;
319 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
320 throw new SWTException (code, msg);
322 case ERROR_CANNOT_ACCESS_CLASSFACTORY : {
323 String msg = ERROR_CANNOT_ACCESS_CLASSFACTORY_MSG;
324 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
325 throw new SWTException (code, msg);
327 case ERROR_CANNOT_CREATE_LICENSED_OBJECT : {
328 String msg = ERROR_CANNOT_CREATE_LICENSED_OBJECT_MSG;
329 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
330 throw new SWTException (code, msg);
332 case ERROR_CANNOT_CHANGE_VARIANT_TYPE : {
333 String msg = ERROR_CANNOT_CHANGE_VARIANT_TYPE_MSG;
334 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
335 throw new SWTException (code, msg);
337 case ERROR_APPLICATION_NOT_FOUND : {
338 String msg = ERROR_APPLICATION_NOT_FOUND_MSG;
339 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
340 throw new SWTException (code, msg);
342 case ERROR_ACTION_NOT_PERFORMED : {
343 String msg = ERROR_ACTION_NOT_PERFORMED_MSG;
344 if (hresult != 0) msg += " result = "+hresult;//$NON-NLS-1$
345 throw new SWTException (code, msg);
348 /* OS Failure/Limit (fatal, may occur only on some platforms) */
349 case ERROR_OUT_OF_MEMORY : {
350 String msg = ERROR_ACTION_NOT_PERFORMED_MSG;
351 if (hresult != 0) msg += " result = "+hresult; //$NON-NLS-1$
352 throw new SWTError (code, msg);
356 /* Unknown/Undefined Error */
361 * Finds the OLE program id that is associated with an extension.
362 * The extension may or may not begin with a '.'. On platforms
363 * that do not support OLE, an empty string is returned.
365 * @param extension the program extension
366 * @return a string that is the OLE program id or an empty string
368 * @exception IllegalArgumentException <ul>
369 * <li>ERROR_NULL_ARGUMENT when extension is null</li>
372 public static String findProgramID (String extension) {
373 if (extension == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
374 if (extension.length () == 0) return ""; //$NON-NLS-1$
376 if (extension.charAt (0) != '.') extension = "." + extension; //$NON-NLS-1$
378 /* Use the character encoding for the default locale */
379 TCHAR extensionKey = new TCHAR(0, extension, true);
380 String result = getKeyValue(extensionKey);
381 if (result != null) {
382 // look for "<programID>\NotInsertable"
383 TCHAR notInsertableKey = new TCHAR(0, result+"\\NotInsertable", true); //$NON-NLS-1$
384 if (getKeyExists(notInsertableKey)) return ""; //$NON-NLS-1$
385 // look for "<programID>\Insertable"
386 TCHAR insertableKey = new TCHAR(0, result+"\\Insertable", true); //$NON-NLS-1$
387 if (getKeyExists(insertableKey)) return result;
388 // look for "<programID>\protocol\StdFileEditing\server"
389 TCHAR serverKey = new TCHAR(0, result+"\\protocol\\StdFileEditing\\server", true); //$NON-NLS-1$
390 if (getKeyExists(serverKey)) return result;
393 return ""; //$NON-NLS-1$
395 static String getKeyValue (TCHAR key) {
396 long [] phkResult = new long [1];
397 if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
400 String result = null;
401 int [] lpcbData = new int [1];
402 if (OS.RegQueryValueEx (phkResult [0], (TCHAR) null, 0, null, (TCHAR) null, lpcbData) == 0) {
403 int length = lpcbData [0] / TCHAR.sizeof;
407 /* Use the character encoding for the default locale */
408 TCHAR lpData = new TCHAR (0, length);
409 if (OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData) == 0) {
410 length = Math.max(0, lpData.length () - 1);
411 result = lpData.toString (0, length);
415 if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]);
418 private static boolean getKeyExists (TCHAR key) {
419 long [] phkResult = new long [1];
420 if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) {
423 if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]);
427 * Returns true if the specified file has an OLE Storage format.
429 * Note all empty files (regardless of extension) will return false.
431 * @param file the file to be checked
433 * @return true if this file has an OLE Storage format
435 public static boolean isOleFile(File file) {
436 if (file == null || !file.exists() || file.isDirectory())
439 return (COM.StgIsStorageFile((file.getAbsolutePath()+"\0").toCharArray()) == COM.S_OK);