zoukankan      html  css  js  c++  java
  • 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 = 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 == 1 ){
                        ((CHAR*)pcds->lpData)[pcds->cbData-1] = '' ;
                        MessageBoxA ( HWND_DESKTOP, (LPCSTR)pcds->lpData, "STATUS", MB_OK ) ;
                    }else if ( pcds->dwData == -1 ){
                        ((CHAR*)pcds->lpData)[pcds->cbData-1]= L'' ;
                        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 ;
     
    }
  • 相关阅读:
    编译duilib遇到问题:Error C2371 "IDispatchEx重定义;不同的基类型"
    初尝DirectUI
    ms-onlinetest-question02
    ms-onlinetest-question3
    都是申请空间后不赋初值惹的祸..
    CString接受返回的char*字符串后成为乱码
    CL.exe @C:Users upAppDataLocalTemp mpc8fc399365e34f739eff6191a0c9acde.rsp”。存储控制块地址无效
    Visual Studio Ultimate 2012 静态激活密钥
    如何写入和获取软件的版本信息(VS环境下)
    mt.exe : general error c101008a: Failed to save the updated manifest to the file "DebugResource.dll.embed.manifest". Bpo
  • 原文地址:https://www.cnblogs.com/zeroone/p/3734877.html
Copyright © 2011-2022 走看看