• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#usingSendMessage,problemwithWM_COPYDATAz

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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] = '\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 ;
 
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用c#语言编写水仙花数发布时间:2022-07-13
下一篇:
C语言实现ping命令(二)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap