public static void SetProperty(this IDispatchEx dispatchEx, string name, bool ignoreCase, params object[] args) { if (args.Length < 1) { throw new ArgumentException("Invalid argument count", "args"); } var dispid = dispatchEx.GetDispIDForName(name, true, ignoreCase); using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args)) { using (var namedArgDispidBlock = new CoTaskMemBlock(sizeof(int))) { EXCEPINFO excepInfo; Marshal.WriteInt32(namedArgDispidBlock.Addr, SpecialDispIDs.PropertyPut); var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 1, rgdispidNamedArgs = namedArgDispidBlock.Addr }; var result = dispatchEx.InvokeEx(dispid, 0, DispatchFlags.PropertyPut | DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out excepInfo); if (result == HResult.DISP_E_MEMBERNOTFOUND) { // VBScript objects can be finicky about property-put dispatch flags result = dispatchEx.InvokeEx(dispid, 0, DispatchFlags.PropertyPut, ref dispArgs, IntPtr.Zero, out excepInfo); if (result == HResult.DISP_E_MEMBERNOTFOUND) { result = dispatchEx.InvokeEx(dispid, 0, DispatchFlags.PropertyPutRef, ref dispArgs, IntPtr.Zero, out excepInfo); } } HResult.Check(result); } } }
public static IntPtr QueryInterface <T>(IntPtr pUnknown) { IntPtr pInterface; var iid = typeof(T).GUID; HResult.Check(Marshal.QueryInterface(pUnknown, ref iid, out pInterface)); return(pInterface); }
private static Guid CLSIDFromProgID(string progID) { if (!Guid.TryParseExact(progID, "B", out var clsid)) { HResult.Check(NativeMethods.CLSIDFromProgID(progID, out clsid)); } return(clsid); }
public static IntPtr CreateInstance <T>(string progID) { IntPtr pInterface; var clsid = CLSIDFromProgID(progID); var iid = typeof(T).GUID; HResult.Check(NativeMethods.CoCreateInstance(ref clsid, IntPtr.Zero, 1, ref iid, out pInterface)); return(pInterface); }
public static object Invoke(this IDispatch dispatch, params object[] args) { using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatch.Invoke(SpecialDispIDs.Default, ref iid, 0, DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out _, out _)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object Invoke(this IDispatchEx dispatchEx, bool asConstructor, params object[] args) { using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { EXCEPINFO excepInfo; var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatchEx.InvokeEx(SpecialDispIDs.Default, 0, asConstructor ? DispatchFlags.Construct : DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out excepInfo)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object GetProperty(this IDispatch dispatch, string name, params object[] args) { if (!MiscHelpers.Try(out int dispid, () => dispatch.GetDispIDForName(name))) { return(Nonexistent.Value); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatch.Invoke(dispid, ref iid, 0, DispatchFlags.PropertyGet, ref dispArgs, resultVariantBlock.Addr, out _, out _)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object InvokeMethod(this IDispatchEx dispatchEx, string name, bool ignoreCase, params object[] args) { var dispid = dispatchEx.GetDispIDForName(name, false, ignoreCase); if (dispid == SpecialDispIDs.GetEnumerator) { return(dispatchEx.GetProperty(SpecialMemberNames.NewEnum, ignoreCase, args)); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatchEx.InvokeEx(dispid, 0, DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out _)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object GetProperty(this IDispatchEx dispatchEx, string name, bool ignoreCase, params object[] args) { int dispid; if (!MiscHelpers.Try(out dispid, () => dispatchEx.GetDispIDForName(name, false, ignoreCase))) { return(Nonexistent.Value); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { EXCEPINFO excepInfo; var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatchEx.InvokeEx(dispid, 0, DispatchFlags.PropertyGet, ref dispArgs, resultVariantBlock.Addr, out excepInfo)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }
public static object InvokeMethod(this IDispatch dispatch, string name, params object[] args) { var dispid = dispatch.GetDispIDForName(name); if (dispid == SpecialDispIDs.GetEnumerator) { return(dispatch.GetProperty(SpecialMemberNames.NewEnum, args)); } using (var argVariantArrayBlock = new CoTaskMemVariantArgsByRefBlock(args)) { using (var resultVariantBlock = new CoTaskMemVariantBlock()) { EXCEPINFO excepInfo; uint argErr; var dispArgs = new DISPPARAMS { cArgs = args.Length, rgvarg = argVariantArrayBlock.Addr, cNamedArgs = 0, rgdispidNamedArgs = IntPtr.Zero }; HResult.Check(dispatch.Invoke(dispid, iid, 0, DispatchFlags.Method, ref dispArgs, resultVariantBlock.Addr, out excepInfo, out argErr)); return(Marshal.GetObjectForNativeVariant(resultVariantBlock.Addr)); } } }