Pet*_*ger 7 .net c# c++ mfc windows-messages
我正在编写一个C#应用程序,它需要拦截另一个应用程序发出的Window Messages.编写我正在监控的应用程序的公司给我发了一些示例代码,但它是用C++编写的,我真的不知道.
在C++示例代码中,我得到了他们使用以下代码:
UINT uMsg = RegisterWindowMessage(SHOCK_MESSAGE_BROADCAST);
ON_REGISTERED_MESSAGE(WM_SHOCK_BROADCAST_MESSAGE, OnShockStatusMessage)
LRESULT OnShockStatusMessage(WPARAM wParam, LPARAM lParam);
Run Code Online (Sandbox Code Playgroud)
据我所知,这将从Windows中检索我们想要侦听的特定消息的Id.然后我们要求C++在拦截与Id匹配的消息时调用OnShockStatusMessage.
经过一番研究后,我在C#中整理了以下内容
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
private IntPtr _hWnd; // APS-50 class reference
private List<IntPtr> _windowsMessages = new List<IntPtr>(); // APS-50 messages
private const string _className = "www.AuPix.com/SHOCK/MessageWindowClass";
// Windows Messages events
private const string _messageBroadcast = "www.AuPix.com/SHOCK/BROADCAST";
private const string _messageCallEvents = "www.AuPix.com/SHOCK/CallEvents";
private const string _messageRegistrationEvents = "www.AuPix.com/SHOCK/RegistrationEvents";
private const string _messageActions = "www.AuPix.com/SHOCK/Actions";
private void DemoProblem()
{
// Find hidden window handle
_hWnd = FindWindow(_className, null);
// Register for events
_windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageActions ) ) );
_windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageBroadcast ) ) );
_windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageCallEvents ) ) );
_windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageRegistrationEvents ) ) );
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// Are they registered Windows Messages for the APS-50 application?
foreach (IntPtr message in _windowsMessages)
{
if ((IntPtr)m.Msg == message)
{
Debug.WriteLine("Message from specified application found!");
}
}
// Are they coming from the APS-50 application?
if ( m.HWnd == shock.WindowsHandle)
{
Debug.WriteLine("Message from specified application found!");
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这应该做同样的基本事情,因为它:
但是,在我覆盖WndProc()方法时,我的检查都没有拦截任何特定消息或来自我正在监视的应用程序的任何消息.
如果我通过它调试所有消息的Debug.WriteLine,我可以看到它正在监视它们.但是它永远不会过滤掉我想要的消息.
通过运行用C++编写的示例监视应用程序,我可以看到正在发送和拾取窗口消息 - 这只是我的C#实现不做同样的事情.
结果我还需要向其他应用程序发送一条PostMessage,要求它向我的应用程序发送窗口消息。
PostMessage((int)_hWnd, _windowsMessages[0], SHOCK_REQUEST_ACTIVE_CALLINFO, (int)_thisHandle);
PostMessage((int)_hWnd, _windowsMessages[0], SHOCK_REQUEST_ALL_REGISTRATIONINFO, (int)_thisHandle);
PostMessage((int)_hWnd, _windowsMessages[0], SHOCK_REQUEST_CALL_EVENTS, (int)_thisHandle);
PostMessage((int)_hWnd, _windowsMessages[0], SHOCK_REQUEST_REGISTRATION_EVENTS, (int)_thisHandle);
Run Code Online (Sandbox Code Playgroud)
代码不是很漂亮,但足以证明它有效,这就是我现在所需要的:)