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 = ;
cds.cbData = message.Length + ;
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 = ;
ex.cbSize = sizeof ( WNDCLASSEX ) ;
ex.cbWndExtra = ;
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 = ;
if ( !RegisterClassEx ( &ex ) ){
return ;
}
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 ;
} 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, , ) ;
if ( res == ){
break ;
}else if ( res <){ }else{
DispatchMessage ( &msg ) ;
}
} return ;
} 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 > && pcds->cbData < ){
if ( pcds->dwData == ){
((CHAR*)pcds->lpData)[pcds->cbData-] = '\0' ;
MessageBoxA ( HWND_DESKTOP, (LPCSTR)pcds->lpData, "STATUS", MB_OK ) ;
}else if ( pcds->dwData == - ){
((CHAR*)pcds->lpData)[pcds->cbData-]= L'\0' ;
MessageBoxW ( HWND_DESKTOP, (LPCWSTR)pcds->lpData, L"STATUS", MB_OK ) ;
}
} break ;
case WM_DESTROY :
PostQuitMessage();
default :
return DefWindowProc ( hWnd, uMsg, wParam, lParam ) ;
}
return 0L ; }

C# using SendMessage, problem with WM_COPYDATA z的更多相关文章

  1. 进程通信之一 使用WM_COPYDATA C++及C#实现 z

    原文地址:http://blog.csdn.net/morewindows/article/details/6804157 进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C ...

  2. WM_COPYDATA实现的不同进程间通信

    进程间通信,通过SendMessage向另一进程发送WM_COPYDATA消息,实现不同进程间的消息通信. 需求:已写好一个工具软件,想在不更改当前的软件开发的前提下,实现为后面新开发的软件提供数据推 ...

  3. 进程通信之一 使用WM_COPYDATA C++及C#实现(转)

    进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C#程序相互通信的二种实现方式.这样消息的接收端可以用C++实现,发送端可以用C++或C#实现.     发送WM_COPYD ...

  4. 使用 WM_COPYDATA 在进程间共享数据

    开发中有时需要进程间传递数据,比如对于只允许单实例运行的程序,当已有实例运行时,再次打开程序,可能需要向当前运行的实例传递信息进行特殊处理.对于传递少量数据的情况,最简单的就是用SendMessage ...

  5. ACM_小Z的A+B

    小Z的A+B Time Limit: 2000/1000ms (Java/Others) Problem Description: 小Z最喜欢A+B了,没事就研究研究,比如什么大整数A+B(就是100 ...

  6. Andrew Ng机器学习公开课笔记 -- 支持向量机

    网易公开课,第6,7,8课 notes,http://cs229.stanford.edu/notes/cs229-notes3.pdf SVM-支持向量机算法概述, 这篇讲的挺好,可以参考   先继 ...

  7. apache win openssl

    Rubayat Hasan Software Development, Music, Web Design, life, thoughts…   Home Portfolio Projects Con ...

  8. c#进程之间对象传递方法

    1. 起源 KV项目下载底层重构升级决定采用独立进程进行Media下载处理,以能做到模块复用之目的,因此涉及到了独立进程间的数据传递问题. 目前进程间数据传递,多用WM_COPYDATA.共享dll. ...

  9. Delphi跨进程访问DBGRID

    要想跨进程访问DBGRID,貌似只能用HOOK,写一个DLL想办法注入到目标进程.注入成功后,使DLL与目标进程在同一进程空间中(其内有一些细节问题,请参见代码),这时可以访问目标进程的VCL组件.并 ...

随机推荐

  1. having与where区别

    having后可以跟组函数如avg(sal)而where后不可以有, 如果条件不是必须使用组函数最好还是使用where

  2. resin access.log format配置详解

    The access log formatting variables follow the Apache variables:     %b result content length %D tim ...

  3. python学习笔记8(表达式和语句)

    一.print 和 import 信息 >>> print 'age:',22 # 用逗号隔开打印多个表达式 age: 22 import somemodule # 从模块导入函数 ...

  4. ExtJS4.2学习(七)EditorGrid可编辑表格(转)

    鸣谢地址:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-14/176.html ------------- ...

  5. Hibernate 缓存机制二(转)

    感谢:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html 一.why(为什么要用Hibernate缓存?) Hibernate是一个 ...

  6. 弹窗开关js

    // var guanbi = false; // $("#testbtn").click(function(){ // if(guanbi){ // $("#tan&q ...

  7. 原创新闻 11 个最佳 jQuery 滚动条插件

    通过jQuery滚动条插件,你可以换掉千篇一律的默认浏览器滚动条,让你的网站或web项目更具特色,更有吸引力.本文收集了11款非常漂亮.实用的jQuery滚动条插件,你可以轻松将它们应用在自己的网站中 ...

  8. pdb文件 小结

    .pdb文件,是VS生成的用于调试的符号文件(program database),保存着调试的信息.在VS的工程属性,C/C++,调试信息格式,设置/Zi,那么VS就会在构建项目时创建PDB文件. 在 ...

  9. 发现一个可以在线运行JS代码的网站

    平时可以在这里玩 http://jsbin.com/

  10. linux查看历史命令history

    在linux下,我们有可能需要查看最近执行过的命令(历史命令),我们可以进行如下操作: # 显示使用过的所有历史命令 $ history # 显示最近使用的5个命令 $ history 5 我们可以通 ...