1 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 82 public final class OleAutomation { 83 private IDispatch objIDispatch; 84 private String 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 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 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 getHelpFile(int dispId) { 143 if (objITypeInfo == null) return null; 144 String [] file = new String [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 getDocumentation(int dispId) { 150 if (objITypeInfo == null) return null; 151 String [] doc = new String [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 [] 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 getName(int dispId) { 246 if (objITypeInfo == null) return null; 247 String [] name = new String [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 [] getNames(int dispId, int maxSize) { 253 if (objITypeInfo == null) return new String [0]; 254 String [] names = new String [maxSize]; 255 int[] count = new int[1]; 256 int rc = objITypeInfo.GetNames(dispId, names, maxSize, count); 257 if (rc == OLE.S_OK) { 258 String [] newNames = new String [count[0]]; 259 System.arraycopy(names, 0, newNames, 0, count[0]); 260 return newNames; 261 } 262 return new String [0]; 263 } 264 274 public int[] getIDsOfNames(String [] 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 287 public String getLastError() { 288 289 return exceptionDescription; 290 291 } 292 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 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 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 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 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 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 if (objIDispatch == null) return COM.E_FAIL; 404 405 DISPPARAMS pDispParams = new DISPPARAMS(); 407 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 (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 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 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 manageExcepinfo(result, excepInfo); 457 458 return result; 459 } 460 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 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 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"; return; 532 } 533 534 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 (buffer); 541 } else { 542 exceptionDescription = "OLE Automation Error Exception "; if (excepInfo.wCode != 0){ 544 exceptionDescription += "code = "+excepInfo.wCode; } else if (excepInfo.scode != 0){ 546 exceptionDescription += "code = "+excepInfo.scode; } 548 } 549 } else { 550 exceptionDescription = "OLE Automation Error HResult : " + hResult; } 552 553 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 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 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 |