首页 > 其他 > 详细

C# using SendMessage, problem with WM_COPYDATA z

时间:2014-05-19 17:35:03      阅读:507      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣
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);
            }
        }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
#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] = \0 ;
                    MessageBoxA ( HWND_DESKTOP, (LPCSTR)pcds->lpData, "STATUS", MB_OK ) ;
                }else if ( pcds->dwData == -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 ;
 
}
bubuko.com,布布扣

 

C# using SendMessage, problem with WM_COPYDATA z,布布扣,bubuko.com

C# using SendMessage, problem with WM_COPYDATA z

原文:http://www.cnblogs.com/zeroone/p/3734877.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!