How can i hook windows messages in WPF?

I want to use WM_COPYDATA to send some data to my WPF application.

How can i do this

or is there another way for sending data to WPF application



Answer this question

How can i hook windows messages in WPF?

  • Amitt

    Do you want to set some data in clipboard if so you can use Clipboard.SetData

  • Andy Perkins

    Get the source and set the hook after window initialization:

    public Window1()
    {
    InitializeComponent();
    this.SourceInitialized += new EventHandler(OnSourceInitialized);
    }

    void OnSourceInitialized(object sender, EventArgs e)
    {
    HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
    source.AddHook(new HwndSourceHook(HandleMessages));
    }


  • RICH525234

    I tried to implement the suggestion above.
    But it seems I cannot get it to run.
    Using the following code in my main window (Window1) I receive an exception while starting the application.

    public Window1() {
    InitializeComponent();
    //Adding hook
    HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
    source.AddHook(new HwndSourceHook(HandleMessages));
    }

    private static IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
    //Handle messages here
    return System.IntPtr.Zero;
    }

    The exception states:

    Window1 could not be instantiated. System.Windows.Markup.XamlParseException at
    "Window1.xaml", Zeile 1, Position 9.

    With my Xaml file being unmodified since the project was setup as vs 2005 wpf application.

    Xaml-file:
    <Window x:Class="WPFHandlingWin32Msg.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPFHandlingWin32Msg" Height="300" Width="300"
    >
    <Grid>

    </Grid>
    </Window>

    When removing the line:
    "
    source.AddHook(new HwndSourceHook(HandleMessages));"
    The exception does not occur.

    Any suggestions greatly appreciated.



  • CSQ

    Should have thought of that.
    Thanks of the support.

  • Tej62007

    There's a number of ways to do this. You should be able to use the same methods in a WPF application as in .Net 2.0 or Win32 applications (TCP/IP, Pipes, shared memory/memory mapped files, etc.).

    If you want communicate with messages, you need to look at the HwndSource class in the WPF documentation. HwndSource is what's "wrapped" around a WPF Window and allows it to function in the (still) "native" Windows OS.

    To get to the HwndSource from a WPF Window do something like this:

    HwndSource source = (HwndSource)PresentationSource.FromVisual(MyWindow);

    To add an event handler for received Windows messages do something like this:

    private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam,IntPtr lParam,ref bool handled)
    {
    // Your code here...
    }

    private void Setup()
    {
    source.AddHook(HandleMessages)
    }

    Hope this helps,

    Tor.


  • How can i hook windows messages in WPF?