public unsafe void ITypeInfo_Invoke_Invoke_Success() { using var image = new Bitmap(16, 32); IPictureDisp picture = SubAxHost.GetIPictureDispFromPicture(image); IDispatch dispatch = (IDispatch)picture; ITypeInfo typeInfo; HRESULT hr = dispatch.GetTypeInfo(0, Kernel32.GetThreadLocale(), out typeInfo); Assert.Equal(HRESULT.S_OK, hr); var dispParams = new DISPPARAMS(); var varResult = new object[1]; var excepInfo = new EXCEPINFO(); uint argErr = 0; hr = typeInfo.Invoke( picture, (DispatchID)4, DISPATCH.PROPERTYGET, &dispParams, varResult, &excepInfo, &argErr ); Assert.Equal(HRESULT.DISP_E_MEMBERNOTFOUND, hr); Assert.Null(varResult[0]); Assert.Equal(0u, argErr); }
public unsafe void IDispatch_Invoke_Invoke_Success() { using var image = new Bitmap(16, 32); IPictureDisp picture = MockAxHost.GetIPictureDispFromPicture(image); IDispatch dispatch = (IDispatch)picture; Guid riid = Guid.Empty; var dispParams = new DISPPARAMS(); var varResult = new object[1]; var excepInfo = new EXCEPINFO(); uint argErr = 0; HRESULT hr = dispatch.Invoke( (DispatchID)4, &riid, Kernel32.GetThreadLocale(), DISPATCH.PROPERTYGET, &dispParams, varResult, &excepInfo, &argErr ); Assert.Equal(HRESULT.S_OK, hr); Assert.Equal(16, GdiHelper.HimetricToPixelY((int)varResult[0])); Assert.Equal(0u, argErr); }
public unsafe void IDispatch_Invoke_Invoke_Success() { using var image = new Bitmap(16, 32); IPictureDisp picture = SubAxHost.GetIPictureDispFromPicture(image); IDispatch dispatch = (IDispatch)picture; Guid riid = Guid.Empty; var dispParams = new DISPPARAMS(); var varResult = new object[1]; var excepInfo = new EXCEPINFO(); uint argErr = 0; HRESULT hr = dispatch.Invoke( (DispatchID)4, &riid, 0, DISPATCH.PROPERTYGET, &dispParams, varResult, &excepInfo, &argErr ); }
public virtual void OnScriptTerminate(object result, EXCEPINFO exceptionInfo) { }
/// <summary> /// Called when the script has completed execution. /// </summary> /// <param name="result"> The script result, or <c>null</c> if the script produced no result. /// </param> /// <param name="info"> The exception information generated when the script terminated, or /// <c>null</c> if no exception was generated. </param> void IActiveScriptSite.OnScriptTerminate(ref object result, ref EXCEPINFO info) { }
/// <summary> /// Invoke the COM member /// </summary> /// <param name="target">IDispatch object</param> /// <param name="dispId">Dispatch identifier that identifies the member</param> /// <param name="args">Arguments passed in</param> /// <param name="byRef">Boolean array that indicates by-Ref parameters</param> /// <param name="invokeKind">Invocation kind</param> /// <returns></returns> internal static object Invoke(IDispatch target, int dispId, object[] args, bool[] byRef, COM.INVOKEKIND invokeKind) { Diagnostics.Assert(target != null, "Caller makes sure an IDispatch object passed in."); Diagnostics.Assert(args == null || byRef == null || args.Length == byRef.Length, "If 'args' and 'byRef' are not null, then they should be one-on-one mapping."); int argCount = args != null ? args.Length : 0; int refCount = byRef != null?byRef.Count(c => c) : 0; IntPtr variantArgArray = IntPtr.Zero, dispIdArray = IntPtr.Zero, tmpVariants = IntPtr.Zero; try { // Package arguments if (argCount > 0) { variantArgArray = NewVariantArray(argCount); int refIndex = 0; for (int i = 0; i < argCount; i++) { // !! The arguments should be in REVERSED order!! int actualIndex = argCount - i - 1; IntPtr varArgPtr = variantArgArray + s_variantSize * actualIndex; // If need to pass by ref, create a by-ref variant if (byRef != null && byRef[i]) { // Allocate memory for temporary VARIANTs used in by-ref marshalling if (tmpVariants == IntPtr.Zero) { tmpVariants = NewVariantArray(refCount); } // Create a VARIANT that the by-ref VARIANT points to IntPtr tmpVarPtr = tmpVariants + s_variantSize * refIndex; Marshal.GetNativeVariantForObject(args[i], tmpVarPtr); // Create the by-ref VARIANT MakeByRefVariant(tmpVarPtr, varArgPtr); refIndex++; } else { Marshal.GetNativeVariantForObject(args[i], varArgPtr); } } } var paramArray = new COM.DISPPARAMS[1]; paramArray[0].rgvarg = variantArgArray; paramArray[0].cArgs = argCount; if (invokeKind == COM.INVOKEKIND.INVOKE_PROPERTYPUT || invokeKind == COM.INVOKEKIND.INVOKE_PROPERTYPUTREF) { // For property putters, the first DISPID argument needs to be DISPID_PROPERTYPUT dispIdArray = Marshal.AllocCoTaskMem(4); // Allocate 4 bytes to hold a 32-bit signed integer Marshal.WriteInt32(dispIdArray, DISPID_PROPERTYPUT); paramArray[0].cNamedArgs = 1; paramArray[0].rgdispidNamedArgs = dispIdArray; } else { // Otherwise, no named parameters are necessary since powershell parser doesn't support named parameter paramArray[0].cNamedArgs = 0; paramArray[0].rgdispidNamedArgs = IntPtr.Zero; } // Make the call EXCEPINFO info = default(EXCEPINFO); object result = null; try { // 'puArgErr' is set when IDispatch.Invoke fails with error code 'DISP_E_PARAMNOTFOUND' and 'DISP_E_TYPEMISMATCH'. // Appropriate exceptions will be thrown in such cases, but FullCLR doesn't use 'puArgErr' in the exception handling, so we also ignore it. uint puArgErrNotUsed = 0; target.Invoke(dispId, s_IID_NULL, LCID_DEFAULT, invokeKind, paramArray, out result, out info, out puArgErrNotUsed); } catch (Exception innerException) { // When 'IDispatch.Invoke' returns error code, CLR will raise exception based on internal HR-to-Exception mapping. // Description of the return code can be found at https://msdn.microsoft.com/library/windows/desktop/ms221479(v=vs.85).aspx // According to CoreCLR team (yzha), the exception needs to be wrapped as an inner exception of TargetInvocationException. string exceptionMsg = null; if (innerException.HResult == DISP_E_EXCEPTION) { // Invoke was successful but the actual underlying method failed. // In this case, we use EXCEPINFO to get additional error info. // Use EXCEPINFO.scode or EXCEPINFO.wCode as HR to construct the correct exception. int code = info.scode != 0 ? info.scode : info.wCode; innerException = Marshal.GetExceptionForHR(code, IntPtr.Zero) ?? innerException; // Get the richer error description if it's available. if (info.bstrDescription != IntPtr.Zero) { exceptionMsg = Marshal.PtrToStringBSTR(info.bstrDescription); Marshal.FreeBSTR(info.bstrDescription); } // Free the BSTRs if (info.bstrSource != IntPtr.Zero) { Marshal.FreeBSTR(info.bstrSource); } if (info.bstrHelpFile != IntPtr.Zero) { Marshal.FreeBSTR(info.bstrHelpFile); } } var outerException = exceptionMsg == null ? new TargetInvocationException(innerException) : new TargetInvocationException(exceptionMsg, innerException); throw outerException; } // Now back propagate the by-ref arguments if (refCount > 0) { for (int i = 0; i < argCount; i++) { // !! The arguments should be in REVERSED order!! int actualIndex = argCount - i - 1; // If need to pass by ref, back propagate if (byRef != null && byRef[i]) { args[i] = Marshal.GetObjectForNativeVariant(variantArgArray + s_variantSize * actualIndex); } } } return(result); } finally { // Free the variant argument array if (variantArgArray != IntPtr.Zero) { for (int i = 0; i < argCount; i++) { VariantClear(variantArgArray + s_variantSize * i); } Marshal.FreeCoTaskMem(variantArgArray); } // Free the dispId array if (dispIdArray != IntPtr.Zero) { Marshal.FreeCoTaskMem(dispIdArray); } // Free the temporary variants created when handling by-Ref arguments if (tmpVariants != IntPtr.Zero) { for (int i = 0; i < refCount; i++) { VariantClear(tmpVariants + s_variantSize * i); } Marshal.FreeCoTaskMem(tmpVariants); } } }
void IActiveScriptSite.OnScriptTerminate(ref object result, ref EXCEPINFO info) { }
public ScriptCodeException(EXCEPINFO excepInfo) : base(excepInfo.bstrDescription) => this.ExcepInfo = excepInfo;
public unsafe int AddError(char* pszPropName, EXCEPINFO* pExcepInfo) { IErrorLog_vtbl** @this = (IErrorLog_vtbl**)reference; IErrorLog_vtbl* vtbl = *@this; if (vtbl == null) throw new InvalidComObjectException(); Delegate genericDelegate = Marshal.GetDelegateForFunctionPointer(vtbl->method_3, typeof(delegate_3)); delegate_3 method = (delegate_3)genericDelegate; return method(@this, pszPropName, pExcepInfo); }