Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在NativeWindow中处理WM_GETOBJECT返回IOleCommandTarget_C#_Com_Wndproc_Native Methods_Nativewindow - Fatal编程技术网

C# 在NativeWindow中处理WM_GETOBJECT返回IOleCommandTarget

C# 在NativeWindow中处理WM_GETOBJECT返回IOleCommandTarget,c#,com,wndproc,native-methods,nativewindow,C#,Com,Wndproc,Native Methods,Nativewindow,我试图从面板控件句柄检索IOleCommandTarget引用,以便对其调用IOleCommandTarget.Exec() 为了能够返回IOleCommandTarget引用,我创建了一个CommandTargetWindow类,该类实现了NativeWindow和IOleCommandTarget,我正在重写WndProc以截获AccessibleObjectFromWindow()发送的WM_GETOBJECT消息: 问题是,正如上面的评论一样,我如何通过消息传回IOleCommandTa

我试图从面板控件句柄检索IOleCommandTarget引用,以便对其调用IOleCommandTarget.Exec()

为了能够返回IOleCommandTarget引用,我创建了一个CommandTargetWindow类,该类实现了NativeWindow和IOleCommandTarget,我正在重写WndProc以截获AccessibleObjectFromWindow()发送的WM_GETOBJECT消息:

问题是,正如上面的评论一样,我如何通过消息传回IOleCommandTarget?

为了实现自动化,我们在其他地方也做了类似的事情,实现了接口irawlementprovidersimple(而不是IOleCommandTarget),并使用定义的静态方法AutomationInteropProvider.ReturnRawElementProvider():


有什么想法吗?

事实证明我需要使用COM方法LresultFromObject,我在NativeMethods.cs中定义为

[DllImport("oleacc.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr LresultFromObject(ref Guid refiid, IntPtr wParam, IntPtr pAcc);
现在在我的WndProc中,我调用LresultFromObject以返回m中的IOleCommandTarget句柄。结果:

[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);
}
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);
    }
}
[PermissionSetAttribute(SecurityAction.Demand, Unrestricted = true)]
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if ((m.Msg == NativeMethods.WM_GETOBJECT) && (m.LParam.ToInt32() == AutomationInteropProvider.RootObjectId))
    {
        m.Result = AutomationInteropProvider.ReturnRawElementProvider(
            Handle, m.WParam, m.LParam, (IRawElementProviderSimple)this);

        return;
    }
    base.WndProc(ref m);
}
[DllImport("oleacc.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr LresultFromObject(ref Guid refiid, IntPtr wParam, IntPtr pAcc);
[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);
}