public int Exec(ref Guid guidCommandGroup, uint commandID, uint commandExecOpt, IntPtr variantIn, IntPtr variantOut) { // Cache the variants, they'll be needed if the command chain // goes back into another IOleCommandTarget (see CommandTargetToOleShim) CommandResult result; try { _variantStacks?.Push(variantIn, variantOut, false); var inputArg = TranslateInputArg(ref guidCommandGroup, commandID, variantIn); object outputArg = null; result = _commandTarget.Invoke(guidCommandGroup, (int)commandID, inputArg, ref outputArg); if (outputArg != null && variantOut != IntPtr.Zero) { Marshal.GetNativeVariantForObject(outputArg, variantOut); } } catch (Exception) { // Some ICommandTarget object is throwing an exception, which will propagate out as a failed hr, which // isn't particularly easy to track down. Put a bit of exception information in the activity log. //ActivityLog.LogInformation("R Tools", exc.ToString()); //string logPath = ActivityLog.LogFilePath; // This actually flushes the activity log buffer throw; } finally { _variantStacks?.Pop(); } return(OleCommand.MakeOleResult(result)); }
public CommandStatus Status(Guid group, int id) { OLECMD[] oleCmd = new OLECMD[1]; oleCmd[0].cmdID = (uint)id; oleCmd[0].cmdf = 0; int oleStatus = OleTarget.QueryStatus(ref group, 1, oleCmd, IntPtr.Zero); return(OleCommand.MakeCommandStatus(oleStatus, oleCmd[0].cmdf)); }
public CommandResult Invoke(Guid group, int id, object inputArg, ref object outputArg) { IntPtr variantIn = IntPtr.Zero; IntPtr variantOut = IntPtr.Zero; bool allocateVariants = false; var commandStatus = Status(group, id); if ((commandStatus & CommandStatus.SupportedAndEnabled) != CommandStatus.SupportedAndEnabled) { return(CommandResult.NotSupported); } if (!_variantStacks.IsEmpty) { _variantStacks.Peek(out variantIn, out variantOut, out allocateVariants); } else { Debug.Fail("Not expecting to use OleToCommandTargetShim without a prior CommandTargetToOleShim"); } if (allocateVariants) { // I have to allocate my own variants if (inputArg != null) { variantIn = Marshal.AllocCoTaskMem(16); Marshal.GetNativeVariantForObject(inputArg, variantIn); } if (outputArg != null) { variantOut = Marshal.AllocCoTaskMem(16); Marshal.GetNativeVariantForObject(outputArg, variantOut); } } int oleResult = 0; try { oleResult = OleTarget.Exec(ref group, (uint)id, 0, variantIn, variantOut); if (oleResult >= 0 && (variantOut != IntPtr.Zero)) { outputArg = Marshal.GetObjectForNativeVariant(variantOut); } } finally { if (allocateVariants) { if (variantIn != IntPtr.Zero) { NativeMethods.VariantClear(variantIn); Marshal.FreeCoTaskMem(variantIn); } if (variantOut != IntPtr.Zero) { NativeMethods.VariantClear(variantOut); Marshal.FreeCoTaskMem(variantOut); } } } return(OleCommand.MakeCommandResult(oleResult)); }
public int QueryStatus(ref Guid guidCommandGroup, uint commandCount, OLECMD[] commandArray, IntPtr commandText) { var status = _commandTarget.Status(guidCommandGroup, (int)commandArray[0].cmdID); return(OleCommand.MakeOleCommandStatus(status, commandArray)); }