151
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 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 * Enzo Petrelli
|
|
12 *******************************************************************************/
|
|
13 module ole.Snippet81;
|
|
14
|
|
15 /*
|
|
16 * OLE and ActiveX example snippet: browse the typelibinfo for a program id (win32 only)
|
|
17 * NOTE: This snippet uses internal SWT packages that are
|
|
18 * subject to change without notice.
|
|
19 *
|
|
20 * For a list of all SWT example snippets see
|
|
21 * http://www.eclipse.org/swt/snippets/
|
|
22 */
|
|
23
|
|
24 bug
|
|
25 {
|
|
26 pragma(lib, "DD-dwt-dbg.lib");
|
|
27 }
|
|
28 else
|
|
29 {
|
|
30 pragma(lib, "DD-dwt.lib");
|
|
31 }
|
|
32 pragma(lib, "advapi32.lib");
|
|
33 pragma(lib, "comctl32.lib");
|
|
34 pragma(lib, "comdlg32.lib");
|
|
35 pragma(lib, "gdi32.lib");
|
|
36 pragma(lib, "gdiplus.lib");
|
|
37 pragma(lib, "glu32.lib");
|
|
38 pragma(lib, "imm32.lib");
|
|
39 pragma(lib, "kernel32.lib");
|
|
40 pragma(lib, "msimg32.lib");
|
|
41 pragma(lib, "ole32.lib");
|
|
42 pragma(lib, "oleacc.lib");
|
|
43 pragma(lib, "oleaut32.lib");
|
|
44 pragma(lib, "olepro32.lib");
|
|
45 pragma(lib, "opengl32.lib");
|
|
46 pragma(lib, "shlwapi.lib");
|
|
47 pragma(lib, "user32.lib");
|
|
48 pragma(lib, "usp10.lib");
|
|
49 pragma(lib, "uxtheme.lib");
|
|
50
|
|
51 import dwt.DWT;
|
|
52 import dwt.widgets.Display;
|
|
53 import dwt.widgets.Shell;
|
|
54
|
|
55 import dwt.dwthelper.utils; // String
|
|
56 import dwt.internal.ole.win32.OAIDL;
|
|
57 import dwt.ole.win32.OLE;
|
|
58 import dwt.ole.win32.OleAutomation;
|
|
59 import dwt.ole.win32.OleControlSite;
|
|
60 import dwt.ole.win32.OleFrame;
|
|
61 import dwt.ole.win32.OleFunctionDescription;
|
|
62 import dwt.ole.win32.OlePropertyDescription;
|
|
63
|
|
64 import std.cstream;
|
|
65 import std.path;
|
|
66 import std.stream;
|
|
67
|
|
68 alias char[] string;
|
|
69
|
|
70 int main(string[] asArg)
|
|
71 {
|
|
72 int iRes = 0;
|
|
73
|
|
74 try
|
|
75 {
|
|
76 // String progID = "Shell.Explorer";
|
|
77 String progID = "Excel.Application";
|
|
78
|
|
79 Display oDisplay = new Display();
|
|
80 Shell oShell = new Shell(oDisplay);
|
|
81
|
|
82 OleFrame frame = new OleFrame(oShell, DWT.NONE);
|
|
83 OleControlSite oOleSite = null;
|
|
84 OleAutomation oOleAutoObj = null;
|
|
85 try
|
|
86 {
|
|
87 oOleSite = new OleControlSite(frame, DWT.NONE, progID);
|
|
88 } catch (Exception oExc)
|
|
89 {
|
|
90 dout.writefln("Exception %s creating OleControlSite on type library for %s", oExc.toString(), progID);
|
|
91 return 1;
|
|
92 }
|
|
93 try
|
|
94 {
|
|
95 oOleAutoObj = new OleAutomation(oOleSite);
|
|
96 } catch (Exception oExc)
|
|
97 {
|
|
98 dout.writefln("Exception %s OleAutomation on type library for %s", oExc.toString(), progID);
|
|
99 return 1;
|
|
100 }
|
|
101
|
|
102 dout.writefln("TypeLibrary for: '%s'", progID);
|
|
103 printTypeInfo(oOleAutoObj, dout);
|
|
104
|
|
105 oShell.dispose();
|
|
106 oDisplay.dispose();
|
|
107 } catch (Exception oExc)
|
|
108 {
|
|
109 dout.writefln("Exception %s in main()", oExc.toString());
|
|
110 iRes = 1;
|
|
111 }
|
|
112
|
|
113 return iRes;
|
|
114 }
|
|
115
|
|
116 private static void printTypeInfo(OleAutomation oOleAutoObj, OutputStream oOut)
|
|
117 {
|
|
118 dwt.internal.ole.win32.OAIDL.TYPEATTR * pTypeAttr = oOleAutoObj.getTypeInfoAttributes();
|
|
119 if (pTypeAttr !is null)
|
|
120 {
|
|
121 if (pTypeAttr.cFuncs > 0)
|
|
122 {
|
|
123 oOut.writefln("Functions :");
|
|
124 for (int iIdx = 0; iIdx < pTypeAttr.cFuncs; ++iIdx)
|
|
125 {
|
|
126 OleFunctionDescription oData = oOleAutoObj.getFunctionDescription(iIdx);
|
|
127 string sArgList = "";
|
|
128 int iFirstOptionalArgIndex = oData.args.length - oData.optionalArgCount;
|
|
129 for (int iArg = 0; iArg < oData.args.length; ++iArg)
|
|
130 {
|
|
131 sArgList ~= "[";
|
|
132 if (iArg >= iFirstOptionalArgIndex)
|
|
133 sArgList ~= "optional, ";
|
|
134 sArgList ~= getDirection(oData.args[iArg].flags) ~ "] " ~
|
|
135 getTypeName(oData.args[iArg].type) ~ " " ~
|
|
136 oData.args[iArg].name
|
|
137 ;
|
|
138 if (iArg < oData.args.length - 1)
|
|
139 sArgList ~= ", ";
|
|
140 }
|
|
141 oOut.writefln("%s (id = %d (0x%08X))", getInvokeKind(oData.invokeKind), oData.id, oData.id);
|
|
142 oOut.writefln("\tSignature : %s %s(%s)", getTypeName(oData.returnType), oData.name, sArgList);
|
|
143 oOut.writefln("\tDescription: %s", oData.documentation);
|
|
144 oOut.writefln("\tHelp File : %s", oData.helpFile);
|
|
145 oOut.writefln();
|
|
146 }
|
|
147 }
|
|
148
|
|
149 if (pTypeAttr.cVars > 0)
|
|
150 {
|
|
151 oOut.writefln("\n\nVariables :");
|
|
152 for (int iIdx = 0; iIdx < pTypeAttr.cVars; ++iIdx)
|
|
153 {
|
|
154 OlePropertyDescription oData = oOleAutoObj.getPropertyDescription(iIdx);
|
|
155 oOut.writefln("PROPERTY (id = %d (0x%08X)", oData.id, oData.id);
|
|
156 oOut.writefln("\tName : %s", oData.name);
|
|
157 oOut.writefln("\tType : %s", getTypeName(oData.type));
|
|
158 oOut.writefln();
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162 }
|
|
163 private static string getTypeName(int iType)
|
|
164 {
|
|
165 int iBase = iType & ~OLE.VT_BYREF;
|
|
166 String sDsc = null;
|
|
167 switch (iBase)
|
|
168 {
|
|
169 case OLE.VT_BOOL : sDsc = "boolean"; break;
|
|
170 case OLE.VT_R4 : sDsc = "float"; break;
|
|
171 case OLE.VT_R8 : sDsc = "double"; break;
|
|
172 case OLE.VT_I4 : sDsc = "int"; break;
|
|
173 case OLE.VT_DISPATCH : sDsc = "IDispatch"; break;
|
|
174 case OLE.VT_UNKNOWN : sDsc = "IUnknown"; break;
|
|
175 case OLE.VT_I2 : sDsc = "short"; break;
|
|
176 case OLE.VT_BSTR : sDsc = "String"; break;
|
|
177 case OLE.VT_VARIANT : sDsc = "Variant"; break;
|
|
178 case OLE.VT_CY : sDsc = "Currency"; break;
|
|
179 case OLE.VT_DATE : sDsc = "Date"; break;
|
|
180 case OLE.VT_UI1 : sDsc = "unsigned char"; break;
|
|
181 case OLE.VT_UI4 : sDsc = "unsigned int"; break;
|
|
182 case OLE.VT_USERDEFINED : sDsc = "UserDefined"; break;
|
|
183 case OLE.VT_HRESULT : sDsc = "HRESULT"; break;
|
|
184 case OLE.VT_VOID : sDsc = "void"; break;
|
|
185
|
|
186 case OLE.VT_UI2 : sDsc = "unsigned short"; break;
|
|
187 case OLE.VT_UINT : sDsc = "unsigned int"; break;
|
|
188 case OLE.VT_PTR : sDsc = "void *"; break;
|
|
189 default: break;
|
|
190 }
|
|
191 if (sDsc !is null)
|
|
192 {
|
|
193 if ((iType & OLE.VT_BYREF) == OLE.VT_BYREF)
|
|
194 return sDsc ~ " *";
|
|
195 return sDsc;
|
|
196 }
|
|
197 return std.string.format("unknown %d (0x%04X)", iType, iType);
|
|
198 }
|
|
199 private static string getDirection(int bDirection)
|
|
200 {
|
|
201 string sDirString = "";
|
|
202 bool bComma = false;
|
|
203 if ((bDirection & OLE.IDLFLAG_FIN) != 0)
|
|
204 {
|
|
205 sDirString ~= "in";
|
|
206 bComma = true;
|
|
207 }
|
|
208 if ((bDirection & OLE.IDLFLAG_FOUT) != 0)
|
|
209 {
|
|
210 if (bComma) sDirString ~= ", ";
|
|
211 sDirString ~= "out";
|
|
212 bComma = true;
|
|
213 }
|
|
214 if ((bDirection & OLE.IDLFLAG_FLCID) != 0)
|
|
215 {
|
|
216 if (bComma) sDirString ~= ", ";
|
|
217 sDirString ~= "lcid";
|
|
218 bComma = true;
|
|
219 }
|
|
220 if ((bDirection & OLE.IDLFLAG_FRETVAL) != 0)
|
|
221 {
|
|
222 if (bComma) sDirString ~= ", ";
|
|
223 sDirString ~= "retval";
|
|
224 }
|
|
225 return sDirString;
|
|
226 }
|
|
227 private static string getInvokeKind(int iInvKind) {
|
|
228 switch (iInvKind)
|
|
229 {
|
|
230 case OLE.INVOKE_FUNC : return "METHOD";
|
|
231 case OLE.INVOKE_PROPERTYGET : return "PROPERTY GET";
|
|
232 case OLE.INVOKE_PROPERTYPUT : return "PROPERTY PUT";
|
|
233 case OLE.INVOKE_PROPERTYPUTREF : return "PROPERTY PUT BY REF";
|
|
234 default: break;
|
|
235 }
|
|
236 return std.string.format("unknown %d", iInvKind);
|
|
237 }
|