C# using SendMessage, problem with WM_COPYDATA
Asked Answered
N

3

7

I've been spending a few days (or more) trying to get this to work.

The application at hand is FTPRush, and I know there is a cmd line application called rush_cmdline.exe which uses SendMessage to send requests to FTPRush.

From debugging the rush_cmdline.exe I can see lParam, wParam, Message and hWnd.

My code is as follows (using SendMessage, not SendMessageW):

[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("USER32.DLL", EntryPoint= "SendMessage")]
public static extern IntPtr SendMessage(int hWnd, int Msg, int wParam, IntPtr lParam);

And I've tried a another specification also:

[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);

The handle (hWnd) is not the problem, as this works:

int ftprush = FindWindow("TfmRush", null);
ShowWindow(ftprush, 8);

Which (I didn't paste the dllimport as it's not important here. Let me know if you wish to see it) brings the window to front. Also, I checked by debugging rush_cmdline.exe. So the handle is the same.

Two attempts which both fail (silently):

public const Int32 WM_COPYDATA = 0x4A;
string msg = "RushApp.FTP.Login('backup','',0); ";
// 1
byte[] array = Encoding.UTF8.GetBytes((string)msg);
int size = Marshal.SizeOf(array[0]) * array.Length + Marshal.SizeOf(array[0]);
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(array, 0, ptr, array.Length);
Marshal.WriteByte(ptr, size - 1, 0);
SendMessage(ftprush, WM_COPYDATA, 0, ptr);

// 2
public struct COPYDATASTRUCT
{
   public IntPtr dwData;
   public int cbData;
   [MarshalAs(UnmanagedType.LPStr)]
   public string lpData;
}

COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = msg;
cds.cbData = sarr.Length + 1;
SendMessage(ftprush, WM_COPYDATA, 0, ref cds);

I would expect at least the 2nd solution to work, as it matches up pretty well with this: perl example

Any enlightenment is GREATLY appreciated!

Thanks,

  • Frank

UPDATE:

string msg = "RushApp.FTP.Login('backup','',0);\0";
var cds = new COPYDATASTRUCT
{
            dwData = new IntPtr(3),
            cbData = msg.Length + 1,
            lpData = msg
};
IntPtr ftprush = FindWindow("TfmRush", null);
SendMessage(ftprush, WM_COPYDATA, IntPtr.Zero, ref cds);
Nigh answered 21/7, 2011 at 16:47 Comment(2)
Are you sure that FTP Rush is supports WM_COPYDATA? What does the FTPRush documentation say about that message? If FTPRush doesn't handle WM_COPYDATA it will just ignore the message and do nothing.Gown
@sh301: it does support WM_COPYDATA. It's the one used in the perl example, and it's the one used by rush_cmdline.exe.Nigh
G
11

My Definitions have

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);

public struct COPYDATASTRUCT {
  public int cbData;
  public IntPtr dwData;
  [MarshalAs(UnmanagedType.LPStr)] public string lpData;
}

var cds = new Win32.COPYDATASTRUCT {
                                           dwData = new IntPtr(3),
                                           cbData = str.Length + 1,
                                           lpData = str
                                         };
Win32.SendMessage(ftprush, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);

Of course, make sure that str is null terminated "\0"

Alternatively a definition given by PInvoke.NET is

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
//If you use '[Out] StringBuilder', initialize the string builder with proper length first.
Garett answered 21/7, 2011 at 17:0 Comment(9)
Thanks for the answer, Bob. Do you mean Win32.SendMessage(ftprush, Win32.WM_COPYDATA, IntPtr.Zero, ref cds); (ref)? I've updated my question. I had to add ref to get it to compile. Or am I missing something here?Nigh
Yes, I didn mean ref, I made a typo!Garett
I can not get it to work with FTPRush, but your approach aligns with what I'd expect, so I assume it's a stupid error on my part. Answer accepted. Do you by any chance have an example of SendMessage I can try out? (which application, which lParam)Nigh
I was using it to communicate between two of my own programs, one of the differences is that I set the 3rd parameter to the hwnd of window in the source application. I take it using the alternative with stringbuilder doesn't helpGarett
I'll have a look at ftp rush tonight and see if I can make an exampleGarett
Thanks for your patience. I've attempted to use pinvoke.net's overload for SendMessageW aswel (matching what rush_cmdline.exe uses).Nigh
I've had a go myself and can't get it to work. Does the Perl script you used actually work. There are issues with security and WM_COPYDATA being restricted under UAC that may be getting in the way.Garett
shouldn't cbData be (str.Length + 1) * sizeof(char)?Yogurt
@SebastianGodelet The string is marshalled as UnmanagedType.LPStr which is a null-terminated array of ANSI characters. These would be byte sized rather the 2 byte for Unicode.Garett
M
5

Between the 2 answers above I cobbled together a working example. Bryce Wagner's class works, so I added a method to use SendMessageTimeout to send the data. it's a static method, so you just call it to send data. This isn't really my work, just gluing together and sharing back.

    [StructLayout(LayoutKind.Sequential)]
    public struct CopyData: IDisposable {
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, ref CopyData target,
                                                SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);

        [Flags]
        enum SendMessageTimeoutFlags: uint {
            SMTO_NORMAL             = 0x0,
            SMTO_BLOCK              = 0x1,
            SMTO_ABORTIFHUNG        = 0x2,
            SMTO_NOTIMEOUTIFNOTHUNG = 0x8
        }
        const uint WM_COPYDATA = 0x4A;

        public IntPtr dwData;
        public int cbData;
        public IntPtr lpData;

        public void Dispose() {
            if (lpData != IntPtr.Zero) {
                Marshal.FreeCoTaskMem(lpData);
                lpData = IntPtr.Zero;
                cbData = 0;
            }
        }
        public string AsAnsiString {
            get { return Marshal.PtrToStringAnsi(lpData, cbData); }
        }
        public string AsUnicodeString {
            get { return Marshal.PtrToStringUni(lpData); }
        }
        public static CopyData CreateForString(int dwData, string value, bool Unicode = false) {
            var result = new CopyData();
            result.dwData = (IntPtr) dwData;
            result.lpData = Unicode ? Marshal.StringToCoTaskMemUni(value) : Marshal.StringToCoTaskMemAnsi(value);
            result.cbData = value.Length + 1;
            return result;
        }

        public static UIntPtr Send(IntPtr targetHandle, int dwData, string value, uint timeoutMs = 1000, bool Unicode = false) {
            var cds = CopyData.CreateForString(dwData, value, Unicode);
            UIntPtr result;
            SendMessageTimeout(targetHandle, WM_COPYDATA, IntPtr.Zero, ref cds, SendMessageTimeoutFlags.SMTO_NORMAL, timeoutMs, out result);
            cds.Dispose();
            return result;
        }
    }

To use it:

CopyData.Send(targetHandle, 1234, "This is a test");

That uses the default 1 second timeout.

Misbehavior answered 25/1, 2015 at 2:44 Comment(1)
Thank you so much for this example! It safed me days of researhing!Homophile
P
2

The order of arguments in the COPYDATASTRUCT are critically important, and Bob Vale's answer has them in the wrong order. http://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx It should be in this order:

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
    public IntPtr dwData;
    public int cbData;
    public IntPtr lpData;
}

I haven't gotten the MarshalAs(UnmanagedType.LPStr)] public string lpData to work either. I've only gotten it to work by doing the marshalling myself:

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT : IDisposable
{
    public IntPtr dwData;
    public int cbData;
    public IntPtr lpData;

    /// <summary>
    /// Only dispose COPYDATASTRUCT if you were the one who allocated it
    /// </summary>
    public void Dispose()
    {
        if (lpData != IntPtr.Zero)
        {
            Marshal.FreeCoTaskMem(lpData);
            lpData = IntPtr.Zero;
            cbData = 0;
        }
    }
    public string AsAnsiString { get { return Marshal.PtrToStringAnsi(lpData, cbData); } }
    public string AsUnicodeString { get { return Marshal.PtrToStringUni(lpData); } }
    public static COPYDATASTRUCT CreateForString(int dwData, string value, bool Unicode = false)
    {
        var result = new COPYDATASTRUCT();
        result.dwData = (IntPtr)dwData;
        result.lpData = Unicode ? Marshal.StringToCoTaskMemUni(value) : Marshal.StringToCoTaskMemAnsi(value);
        result.cbData = value.Length + 1;
        return result;
    }
}
Prelacy answered 11/7, 2014 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.