J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
Joined: Jul 2004
|
RE: how to catch the MessengerPlus_DisplayToast in VB.NET or C#?
It took me a while to figure out how to do this with C#, but with help from MSND and of course with raceprouk's code, it finally worked!
Here it is:
code: // For the dll import
using System.Runtime.InteropServices;
// Filter function delegate
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
// Hook Types
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
// The CWPSTRUCT structure
public struct CWPSTRUCT
{
public IntPtr lParam;
public IntPtr wParam;
public uint message;
public IntPtr hwnd;
}
// the MSG structure
struct MSG
{
public Int32 hwnd;
public Int32 message;
public Int32 wParam;
public Int32 lParam;
public Int32 time;
public Point pt;
}
// Win32: SetWindowsHookEx()
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code,
HookProc func,
IntPtr hInstance,
int threadID);
// Win32: UnhookWindowsHookEx()
[DllImport("user32.dll")]
protected static extern int UnhookWindowsHookEx(IntPtr hhook);
// Win32: CallNextHookEx()
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook,
int code, IntPtr wParam, IntPtr lParam);
// Win32: RegisterWindowMessage()
[DllImport("user32.dll")]
protected static extern uint RegisterWindowMessage(string lpString);
// Variable declares
IntPtr cwp;
IntPtr gmp;
uint msgID;
public void Initialize()
{
HookProc hCallWndProc = new HookProc(CallWndProc);
cwp = SetWindowsHookEx(HookType.WH_CALLWNDPROC, hCallWndProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
HookProc hGetMsgProc = new HookProc(GetMsgProc);
gmp = SetWindowsHookEx(HookType.WH_GETMESSAGE, hGetMsgProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
msgID = RegisterWindowMessage("MessengerPlus_DisplayToast");
}
int CallWndProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0)
{
CWPSTRUCT msg = (CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPSTRUCT));
if (msg.message == msgID)
{
// Do something here
}
}
return CallNextHookEx(cwp, code, wParam, lParam);
}
int GetMsgProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0)
{
MSG msg = (MSG)Marshal.PtrToStructure(lParam, typeof(MSG));
if (msg.message == msgID)
{
// Do something here
}
}
return CallNextHookEx(cwp, code, wParam, lParam);
}
void Uninitialize()
{
UnhookWindowsHookEx(cwp);
UnhookWindowsHookEx(gmp);
}
edit: forgot to include the "using" statement
This post was edited on 06-06-2006 at 03:21 PM by J-Thread.
|
|