4

Please guide me how to use this WndProc in Windows Forms application:

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == NativeCalls.APIAttach && (uint)lParam == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
    {
        // Get the current handle to the Skype window
        NativeCalls.HWND_BROADCAST = wParam;
        handled = true;
        return new IntPtr(1);
    }

    // Skype sends our program messages using WM_COPYDATA. the data is in lParam
    if (msg == NativeCalls.WM_COPYDATA && wParam == NativeCalls.HWND_BROADCAST)
    {
        COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
        StatusTextBox.AppendText(data.lpData + Environment.NewLine);

        // Check for connection
        if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
            ConnectButton.IsEnabled = false;

        // Check for calls
        IsCallInProgress(data.lpData);
        handled = true;
        return new IntPtr(1);
    }

    return IntPtr.Zero;
}

I have seen people use the above code in this way in WPF like

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);

    // Attach WndProc
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

4

The prototype for WndProc in C# is:

protected virtual void WndProc(ref Message m)

So, you need to override this procedure in your class, assumed that it's derived from Control.

protected override void WndProc(ref Message m)
{
    Boolean handled = false; m.Result = IntPtr.Zero;
    if (m.Msg == NativeCalls.APIAttach && (uint)m.Param == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
    {
        // Get the current handle to the Skype window
        NativeCalls.HWND_BROADCAST = m.WParam;
        handled = true;
        m.Result = new IntPtr(1);
    }

    // Skype sends our program messages using WM_COPYDATA. the data is in lParam
    if (m.Msg == NativeCalls.WM_COPYDATA && m.WParam == NativeCalls.HWND_BROADCAST)
    {
        COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
        StatusTextBox.AppendText(data.lpData + Environment.NewLine);

        // Check for connection
        if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
            ConnectButton.IsEnabled = false;

        // Check for calls
        IsCallInProgress(data.lpData);
        handled = true;
        m.Result = new IntPtr(1);
    }

    if (handled) DefWndProc(ref m); else base.WndProc(ref m);
}
Elvedin Hamzagic
  • 825
  • 1
  • 9
  • 22
  • thanks for your answer but i just do not understand why controls never come into second if block when i am debugging? can u tell me the reason. thanks – Thomas Apr 23 '14 at 12:39
  • Sorry, I edited my post. The problem was that `m.Result` is set to zero always at the bottom of the function. Setting it to zero at the beginning will solve this issue, and maybe also your problem. – Elvedin Hamzagic Apr 23 '14 at 15:10
  • still the same problem exist. – Thomas Apr 24 '14 at 08:19
  • Maybe you have some problem with the Skype, not `WndProc`. Can you put breakpoint and check for incoming messages? Do you receive normal window messages? – Elvedin Hamzagic Apr 24 '14 at 13:58
  • i guess no because i develop my application following a wpf based apps which also capture skype sound and save to file....that works great. my application is not working only. i guess i am missing something or there is some mistake in my code that is why problem occur. – Thomas Apr 25 '14 at 06:37
  • can u plzz download my apps from this url and check at your end if possible. here is the url and download the SkypeCallTest.zip file from this url... https://onedrive.live.com/#cid=C4A6F16F34D7540A&id=C4A6F16F34D7540A!126 – Thomas Apr 25 '14 at 06:39
4

You can use Application.AddMessageFilter Method.

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class TestMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        // Blocks all the messages relating to the left mouse button. 
        if (m.Msg >= 513 && m.Msg <= 515)
        {
            Console.WriteLine("Processing the messages : " + m.Msg);
            return true;
        }
        return false;
    }
}
tas
  • 472
  • 3
  • 6