Return an IOleCommandTarget from processing WM_GETOBJECT in a NativeWindow
I am trying to retrieve an IOleCommandTarget reference from a panel control handle, so that I can call IOleCommandTarget.Exec() on it.
NativeMethods.IOleCommandTarget target = null;
if (GetObjectFromHandle<NativeMethods.IOleCommandTarget>(panel.Handle, out target))
{
Guid guidCmdGroup = commandID.Guid;
handled = (target.Exec(ref guidCmdGroup, commandID.ID, 0, null, 0) == NativeMethods.S_OK);
}
private static bool GetObjectFromHandle<T>(IntPtr hwnd, out T value)
{
Guid guid = typeof(T).GUID;
object obj = null;
var hr = NativeMethods.AccessibleObjectFromWindow(hwnd, 0, ref guid, ref obj);
if (hr == NativeMethods.S_OK)
{
value = (T)obj;
return true;
}
value = default(T);
return false;
}
In my NativeMethods.cs:
[ComImport]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("B722BCCB-4E68-101B-A2BC-00AA00404770")]
public interface IOleCommandTarget
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryStatus(ref Guid pguidCmdGroup, int cCmds, [In, Out] NativeMethods.OLECMD prgCmds, [In, Out] IntPtr pCmdText);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int Exec(ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt, [In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn, int pvaOut);
}
[DllImport("oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
IntPtr hwnd,
uint id,
ref Guid iid,
[In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object ppvObject);
To be able to return an IOleCommandTarget reference, I created a CommandTargetWindow class that implements NativeWindow and IOleCommandTarget, and I am overriding WndProc to intercept the WM_GETOBJECT message that AccessibleObjectFromWindow() sends:
public sealed class CommandTargetWindow : NativeWindow,
NativeMethods.IOleCommandTarget,
IDisposable
{
private IWin32Window _parent;
public CommandTargetWindow(IWin32Window parent)
{
_parent = parent;
base.AssignHandle(parent.Handle);
}
[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == NativeMethods.WM_GETOBJECT)
{
//How do I pass back an IOleCommandTarget through the message?
}
base.WndProc(ref m);
}
}
The question is, as in the comment above, how do I pass back an IOleCommandTarget through the message?
We do something similar elsewhere for automation purposes, by implementing interface IRawElementProviderSimple (instead of IOleCommandTarget) and using the defined static method AutomationInteropProvider.ReturnRawElementProvider():
[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if ((m.Msg == NativeMethods.WM_GETOBJECT) &&开发者_JS百科; (m.LParam.ToInt32() == AutomationInteropProvider.RootObjectId))
{
m.Result = AutomationInteropProvider.ReturnRawElementProvider(
Handle, m.WParam, m.LParam, (IRawElementProviderSimple)this);
return;
}
base.WndProc(ref m);
}
Any ideas?
It turns out I needed to use COM method LresultFromObject which I defined in NativeMethods.cs as
[DllImport("oleacc.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr LresultFromObject(ref Guid refiid, IntPtr wParam, IntPtr pAcc);
Now in my WndProc, I call LresultFromObject to return the IOleCommandTarget handle in m.Result:
[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == (int)NativeMethods.WindowMessage.GETOBJECT)
{
if (m.LParam.ToInt32() == AutomationInteropProvider.RootObjectId)
{
m.Result = AutomationInteropProvider.ReturnRawElementProvider(
Handle, m.WParam, m.LParam, (IRawElementProviderSimple)this);
return;
}
else if (m.LParam == (IntPtr)NativeMethods.OBJID_NATIVEOM)
{
IntPtr handle = Marshal.GetComInterfaceForObject(this, typeof(NativeMethods.IOleCommandTarget));
Guid unknownGuid = typeof(NativeMethods.IUnknown).GUID;
m.Result = NativeMethods.LresultFromObject(ref unknownGuid, m.WParam, handle);
return;
}
}
base.WndProc(ref m);
}
精彩评论