首页 > 代码库 > C# using SendMessage, problem with WM_COPYDATA z

C# using SendMessage, problem with WM_COPYDATA z

The final missing piece depends on if you are using any processor, x86 or x64. The details using these different functions requires specific handling for the structure. Essentially if you are using the any processor or x64 you may have to use IntPtr or long, but for x86 you can use int for the dwData.

using System;
using System.Runtime.InteropServices;
 
namespace SendWindowMessage
{
    class Program
    {
        const int WM_COPYDATA = http://www.mamicode.com/0x004A;
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32.dll",CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hwnd, int msg,IntPtr wparam, IntPtr lparam);
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
        [StructLayout(LayoutKind.Sequential)]
        struct COPYDATASTRUCT
        {
            public uint dwData;
            public int cbData;
            public IntPtr lpData;
        }
        public static IntPtr IntPtrAlloc<T>(T param)
        {
            IntPtr retval = Marshal.AllocHGlobal(Marshal.SizeOf(param));
            Marshal.StructureToPtr(param, retval, false);
            return (retval);
        }
 
        public static void IntPtrFree(IntPtr preAllocated)
        {
            if (IntPtr.Zero == preAllocated) throw (new Exception("Go Home"));
            Marshal.FreeHGlobal(preAllocated); preAllocated = IntPtr.Zero;
        }
 
        static void Main(string[] args)
        {
 
            string message = "This is a test" ;
            IntPtr hWnd = FindWindow("Example", "" );
            if ( hWnd == IntPtr.Zero){
 
            }else{
 
                COPYDATASTRUCT cds ;
                cds.dwData = 1;
                cds.cbData = message.Length + 1;
                cds.lpData = Marshal.StringToHGlobalAnsi(message);
                IntPtr cdsBuffer = IntPtrAlloc ( cds ) ;
                PostMessage(hWnd, WM_COPYDATA, IntPtr.Zero, cdsBuffer);
                IntPtrFree(cds.lpData);
                IntPtrFree(cdsBuffer);
            }
        }
    }
}
#include <tchar.h>
#include <windows.h>
 
#define WND_CLASS_NAME _T ( "Example" )
 
LRESULT CALLBACK WndProc (  HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) ;
 
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
    WNDCLASSEX ex ;
    MSG msg ;
    ex.cbClsExtra = 0 ;
    ex.cbSize = sizeof ( WNDCLASSEX ) ;
    ex.cbWndExtra = 0 ;
    ex.hbrBackground = ( HBRUSH ) GetStockObject ( BLACK_BRUSH ) ;
    ex.hCursor = LoadCursor ( NULL, _T ( "IDC_ARROW" ) ) ;
    ex.hIcon = LoadIcon ( NULL, _T ( "IDI_APPLICATION" ) ) ;
    ex.hIconSm = LoadIcon ( NULL, _T( "IDI_APPLICATION" ) ) ;
    ex.hInstance = hInstance ;
    ex.lpfnWndProc = (WNDPROC )WndProc ;
    ex.lpszClassName = WND_CLASS_NAME ;
    ex.lpszMenuName = NULL ;
    ex.style = 0 ;
    if ( !RegisterClassEx ( &ex ) ){
        return 0 ;
    }
    HWND hWnd = CreateWindow ( WND_CLASS_NAME, _T ( "" ), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ) ;
    if ( !hWnd ){
        UnregisterClass ( WND_CLASS_NAME, hInstance ) ;
        return 0 ;
    }
 
    ShowWindow(hWnd, SW_HIDE ) ;
    UpdateWindow ( hWnd ) ;
    if ( !ChangeWindowMessageFilter ( WM_COPYDATA, MSGFLT_ADD ) ){
        MessageBox ( HWND_DESKTOP, _T("Failed to change message filter"),_T("STATUS"), MB_OK ) ;
    }
    while ( TRUE ) {
        UINT res = GetMessage ( &msg, hWnd, 0, 0 ) ;
        if ( res == 0 ){
            break ;
        }else if ( res <0){
 
        }else{
            DispatchMessage ( &msg ) ;
        }
    }
 
    return 0 ;
}
 
LRESULT CALLBACK WndProc (  HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
    PCOPYDATASTRUCT pcds;
    switch ( uMsg ){
        case WM_CREATE :
        break ;
        case WM_COPYDATA :
            pcds=(PCOPYDATASTRUCT)lParam;
            if ( pcds->cbData > 0 && pcds->cbData < 512 ){
                if ( pcds->dwData =http://www.mamicode.com/= 1 ){
                    ((CHAR*)pcds->lpData)[pcds->cbData-1] = \0 ;
                    MessageBoxA ( HWND_DESKTOP, (LPCSTR)pcds->lpData, "STATUS", MB_OK ) ;
                }else if ( pcds->dwData =http://www.mamicode.com/= -1 ){
                    ((CHAR*)pcds->lpData)[pcds->cbData-1]= L\0 ;
                    MessageBoxW ( HWND_DESKTOP, (LPCWSTR)pcds->lpData, L"STATUS", MB_OK ) ;
                }
            }
 
        break ;
        case WM_DESTROY :
            PostQuitMessage(0);
        default :
            return DefWindowProc ( hWnd, uMsg, wParam, lParam ) ;
    }
    return 0L ;
 
}