从C+发送通知+;DLL到.NET应用程序 我正在编写一个C++ DLL,需要通知客户端应用程序。在C++(MFC)中,我可以在DLL内部登记一个客户端窗口句柄,然后在需要通知客户端时调用POST消息。当客户端是C#应用程序时,我该怎么办?

从C+发送通知+;DLL到.NET应用程序 我正在编写一个C++ DLL,需要通知客户端应用程序。在C++(MFC)中,我可以在DLL内部登记一个客户端窗口句柄,然后在需要通知客户端时调用POST消息。当客户端是C#应用程序时,我该怎么办?,c#,c++,C#,C++,PostMessage只是告诉windows向另一个应用程序的主应用程序循环发送消息。如果客户端是C#app,那么几乎可以肯定您也会做同样的事情,因此更恰当的问题是,如何读取发送到C#中主应用程序循环的消息 PostMessage只是告诉windows向另一个应用程序的主应用程序循环发送消息。如果客户端是C#app,那么几乎可以肯定您也会做同样的事情,因此更恰当的问题是,如何读取发送到C#中主应用程序循环的消息 您可以在C#窗口中重写WndProc方法来处理此特定消息 protected ove

PostMessage只是告诉windows向另一个应用程序的主应用程序循环发送消息。如果客户端是C#app,那么几乎可以肯定您也会做同样的事情,因此更恰当的问题是,如何读取发送到C#中主应用程序循环的消息

PostMessage只是告诉windows向另一个应用程序的主应用程序循环发送消息。如果客户端是C#app,那么几乎可以肯定您也会做同样的事情,因此更恰当的问题是,如何读取发送到C#中主应用程序循环的消息

您可以在C#窗口中重写WndProc方法来处理此特定消息

protected override void WndProc(ref Message m)
{
    if (m.Msg = YOUR_MESSAGE)
    {
        // handle the notification
    }
    else
    {
        base.WndProc(ref m);
    }
}

您可以在C#窗口中重写WndProc方法来处理此特定消息

protected override void WndProc(ref Message m)
{
    if (m.Msg = YOUR_MESSAGE)
    {
        // handle the notification
    }
    else
    {
        base.WndProc(ref m);
    }
}

如果您想实现穷人的发布-订阅模式,回调是一种可行的方法。这里有一些很好的信息。

如果你想实现一个穷人的发布-订阅模式,回调是最好的选择。中有一些很好的信息。

可以通过将消息发布到windows句柄来实现。在dotnet类中,创建一个可以截获消息的虚拟窗口,然后触发消息

这里有一些代码,你只需要填写我使用WMMMySeMess的地方,引用一个正确的Windows消息,现在在你的C++ DLL中你可以向它发布一条消息。注意,我确信有更好的/其他的方法来做你想做的事情,但这可能也会起作用

//Dummy window classes.  Because we don't have access to the wndproc method of a form, we create
//dummy forms and expose the method to the SystemHotKeyHook class as an event.

/// <summary>
/// Inherits from System.Windows.Form.NativeWindow. Provides an Event for Message handling
/// </summary>
private class NativeWindowWithEvent : System.Windows.Forms.NativeWindow
{
    public event MessageEventHandler ProcessMessage;
    protected override void WndProc(ref Message m)
    {
        //Intercept the message you are looking for...
        if (m.Msg == (int)WM_MYMESSAGE)
        {
            //Fire event which is consumed by your class
            if (ProcessMessage != null)
            {
                bool Handled = false;
                ProcessMessage(this, ref m, ref Handled);
                if (!Handled)
                {
                    base.WndProc(ref m);
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }
        else
        {
            base.WndProc(ref m);
        }
    }
}
/// <summary>
/// Inherits from NativeWindowWithEvent and automatic creates/destroys of a dummy window
/// </summary>
private class DummyWindowWithEvent : NativeWindowWithEvent, IDisposable
{
    public DummyWindowWithEvent()
    {
        CreateParams parms = new CreateParams();
        this.CreateHandle(parms);
    }
    public void Dispose()
    {
        if (this.Handle != (IntPtr)0)
        {
            this.DestroyHandle();
        }
    }
}
//虚拟窗口类。因为我们不能访问表单的wndproc方法,所以我们创建
//dummy将该方法作为事件形成并公开给SystemHotKeyHook类。
/// 
///从System.Windows.Form.NativeWindow继承。提供用于消息处理的事件
/// 
私有类NativeWindowWithEvent:System.Windows.Forms.NativeWindow
{
公共事件消息EventHandler ProcessMessage;
受保护的覆盖无效WndProc(参考消息m)
{
//拦截您正在查找的邮件。。。
如果(m.Msg==(int)WM_MYMESSAGE)
{
//你的班级消耗的火灾事件
if(ProcessMessage!=null)
{
bool=false;
ProcessMessage(此,ref m,ref已处理);
如果(!已处理)
{
基准WndProc(参考m);
}
}
其他的
{
基准WndProc(参考m);
}
}
其他的
{
基准WndProc(参考m);
}
}
}
/// 
///从NativeWindowWithEvent继承并自动创建/销毁虚拟窗口
/// 
私有类DummyWindowWithEvent:NativeWindowWithEvent,IDisposable
{
公共DummyWindowWithEvent()
{
CreateParams parms=新的CreateParams();
这个.CreateHandle(parms);
}
公共空间处置()
{
if(this.Handle!=(IntPtr)0)
{
这个.handle();
}
}
}
类来拦截消息:

// <summary>
/// System hotkey interceptor
/// </summary>
public class MessageIntercept: IDisposable
{
    private delegate void MessageEventHandler(object Sender, ref System.Windows.Forms.Message msg, ref bool Handled);

    //Window for WM_MYMESSAGE Interceptor
    private DummyWindowWithEvent frmDummyReceiver_m;

    /// <summary>
    /// Default constructor
    /// </summary>
    public MessageIntercept()
    {
        this.frmDummyReceiver_m = new DummyWindowWithEvent();
        this.frmDummyReceiver_m.ProcessMessage += new MessageEventHandler(this.InterceptMessage);
    }

    private void InterceptMessage(object Sender, ref System.Windows.Forms.Message msg, ref bool Handled)
    {
        //Do something based on criteria of the message
        if ((msg.Msg == (int)WM_MYMESSAGE) && 
            (msg.WParam == (IntPtr)xyz))
        {
            Handled = true;
            System.Diagnostics.Debug.WriteLine("Message intercepted.");
        }
    }
}
//
///系统热键拦截器
/// 
公共类MessageIntercept:IDisposable
{
私有委托void MessageEventHandler(对象发送者,ref System.Windows.Forms.Message msg,ref bool Handled);
//WM_MYMESSAGE拦截器窗口
具有事件frmDummyReceiver\m的私有DummyWindow;
/// 
///默认构造函数
/// 
公共消息拦截()
{
this.frmDummyReceiver_m=新的DummyWindowWithEvent();
this.frmDummyReceiver\u m.ProcessMessage+=新的MessageEventHandler(this.InterceptMessage);
}
私有消息(对象发送方,ref System.Windows.Forms.Message msg,ref bool Handled)
{
//根据消息的标准执行某些操作
如果((msg.msg==(int)WM_MYMESSAGE)&&
(msg.WParam==(IntPtr)xyz))
{
已处理=正确;
System.Diagnostics.Debug.WriteLine(“消息被截获”);
}
}
}

可以通过将消息发布到windows句柄来实现。在dotnet类中,创建一个可以截获消息的虚拟窗口,然后触发消息

这里有一些代码,你只需要填写我使用WMMMySeMess的地方,引用一个正确的Windows消息,现在在你的C++ DLL中你可以向它发布一条消息。注意,我确信有更好的/其他的方法来做你想做的事情,但这可能也会起作用

//Dummy window classes.  Because we don't have access to the wndproc method of a form, we create
//dummy forms and expose the method to the SystemHotKeyHook class as an event.

/// <summary>
/// Inherits from System.Windows.Form.NativeWindow. Provides an Event for Message handling
/// </summary>
private class NativeWindowWithEvent : System.Windows.Forms.NativeWindow
{
    public event MessageEventHandler ProcessMessage;
    protected override void WndProc(ref Message m)
    {
        //Intercept the message you are looking for...
        if (m.Msg == (int)WM_MYMESSAGE)
        {
            //Fire event which is consumed by your class
            if (ProcessMessage != null)
            {
                bool Handled = false;
                ProcessMessage(this, ref m, ref Handled);
                if (!Handled)
                {
                    base.WndProc(ref m);
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }
        else
        {
            base.WndProc(ref m);
        }
    }
}
/// <summary>
/// Inherits from NativeWindowWithEvent and automatic creates/destroys of a dummy window
/// </summary>
private class DummyWindowWithEvent : NativeWindowWithEvent, IDisposable
{
    public DummyWindowWithEvent()
    {
        CreateParams parms = new CreateParams();
        this.CreateHandle(parms);
    }
    public void Dispose()
    {
        if (this.Handle != (IntPtr)0)
        {
            this.DestroyHandle();
        }
    }
}
//虚拟窗口类。因为我们不能访问表单的wndproc方法,所以我们创建
//dummy将该方法作为事件形成并公开给SystemHotKeyHook类。
/// 
///从System.Windows.Form.NativeWindow继承。提供用于消息处理的事件
/// 
私有类NativeWindowWithEvent:System.Windows.Forms.NativeWindow
{
公共事件消息EventHandler ProcessMessage;
受保护的覆盖无效WndProc(参考消息m)
{
//拦截您正在查找的邮件。。。
如果(m.Msg==(int)WM_MYMESSAGE)
{
//你的班级消耗的火灾事件
if(ProcessMessage!=null)
{
bool=false;
ProcessMessage(此,ref m,ref已处理);
如果(!已处理)
{
基准WndProc(参考m);
}
}
其他的
{
基准WndProc(参考m);
}
}
其他的
{
基准WndProc(参考m);
}
}
}
/// 
///从NativeWindowWithEvent继承并自动创建/销毁虚拟窗口
/// 
私有类DummyWindowWithEvent:NativeWindowWithEvent,IDisposable
{
公众假人