Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C# Helper file for using WM_DATACOPY in Win32 API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class MessageHelper
{
[DllImport("User32.dll")]
private static extern int RegisterWindowMessage(string lpString);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(int hWnd);
public const int WM_USER = 0x400;
public const int WM_COPYDATA = 0x4A;
//Used for WM_COPYDATA for string messages
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public bool bringAppToFront(int hWnd)
{
return SetForegroundWindow(hWnd);
}
public int sendWindowsStringMessage(int hWnd, int wParam, string msg)
{
int result = 0;
if (hWnd > 0)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(msg);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = msg;
cds.cbData = len + 1;
result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
}
return result;
}
public int sendWindowsMessage(int hWnd, int Msg, int wParam, int lParam)
{
int result = 0;
if (hWnd > 0)
{
result = SendMessage(hWnd, Msg, wParam, lParam);
}
return result;
}
public int getWindowId(string className, string windowName)
{
return FindWindow(className, windowName);
}
}

Djamana commented Oct 15, 2015

When debugging SendMessage(WM_COPYDATA...

Well Microsoft Spy++ might really come in handy.
You may find it here:

c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx_amd64.exe
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\spyxx.exe
  1. Test that it's working on the target process(window) [ctrl+f,Windows].
  2. Second set message filter on WM_COPYDATA.
    ... and
  3. 'View\Always on top' is also really handy.

Happy C++'ing - especially in C# that API can be real 'fun'. ;)

Ah yes and instead

        COPYDATASTRUCT cds;
        cds.dwData = (IntPtr)100;
        cds.lpData = msg; ...

That'll be more nice

        COPYDATASTRUCT cds = new COPYDATASTRUCT {
                    dwData = (IntPtr)100,
                    lpData = msg,...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment