views:

103

answers:

1

Could someone shed some light on why my WndProc method as implemented below isn't receiving any messages? If I put this class below in a WinForms application and pass in that application's handle, WndProc receives messages as I would expect. However, using the IntPtr returned from GetForegroundWindow() as I have below doesn't yield the same results. (FWIW, I have my code set up to execute GetForegroundWindow() when my application is hidden, so I'm certain that the IntPtr is referring to an outside application.) My goal here is to monitor certain events from outside applications.

public class MyNativeWindow : NativeWindow
{
    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern IntPtr GetForegroundWindow();

    public MyNativeWindow()
    {
        this.AssignHandle(GetForegroundWindow());
    }

    // Never called... I set a breakpoint
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
    }
}
+1  A: 

You won't recieve Messages of another process.

deerchao
Got any suggestions as to how I can receive messages of other processes?
Nick
The only way I can think of is to inject a dll into target process. Search for "dll injection"
deerchao
Sounds messy. ;) I'll check it out...
Nick