Winform 如何向其他进程微信延时发送消息息

博客访问: 506408
博文数量: 96
博客积分: 2023
博客等级: 上尉
技术积分: 1738
注册时间:
分类: 嵌入式 22:04:09
&&& 我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可)。下面的发送和接收端同时演示了系统消息和自定义的消息。消息统一采用4位16进制的数。
1.系统消息使用的是0x0100(WM_KEYDOWN);0x0是系统自定义的消息,其中0x0000为WM_NULL,0x0400为WM_USER。0x0400以后的数值我们可以作为自定义的消息值。(见附录)
2.自定义消息Message,代码如下:
class Message
&&&&&&&&public const int USER = 0x0400;
&&&&&&&&public const int WM_TEST = USER + 101;
&&&&&&&&public const int WM_MSG = USER + 102;
3.接收端和发送端
接收端Form1:
public partial class Form1 : Form
&&&&&&&&public Form1()
&&&&&&&&&&&&InitializeComponent();
&&&&&&&&[DllImport("User32.dll", EntryPoint = "SendMessage")]
&&&&&&&&private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
&&&&&&&&//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
&&&&&&&&//private static extern int StartSendMessage(IntPtr hWnd);
&&&&&&&&//自己触发自定义消息
&&&&&&&&private void button1_Click(object sender, EventArgs e)
&&&&&&&&&&&&SendMessage(this.Handle, Message.WM_TEST, 100, 200);
&&&&&&&&private void button2_Click(object sender, EventArgs e)
&&&&&&&&&&&&//StartSendMessage(this.Handle);
&&&&&&&&//响应和处理自定义消息
&&&&&&&&protected override void DefWndProc(ref System.Windows.Forms.Message m)
&&&&&&&&&&&&string message;
&&&&&&&&&&&&switch (m.Msg)
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&case Message.WM_TEST://处理消息
&&&&&&&&&&&&&&&&&&&&message = string.Format("收到从应用程序发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
&&&&&&&&&&&&&&&&&&&&MessageBox.Show(message);
&&&&&&&&&&&&&&&&&&&&break;
&&&&&&&&&&&&&&&&//case Message.WM_MSG:
&&&&&&&&&&&&&&&&// message = string.Format("收到从DLL发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
&&&&&&&&&&&&&&&&// MessageBox.Show(message);
&&&&&&&&&&&&&&&&//
&&&&&&&&&&&&&&&&default:
&&&&&&&&&&&&&&&&&&&&base.DefWndProc(ref m);
&&&&&&&&&&&&&&&&&&&&break;
&&&&&&&&&&&&}
&&&&&&&&private void Form1_KeyDown(object sender, KeyEventArgs e)
&&&&&&&&&&&&this.label1.Text = e.KeyValue.ToString();
发送端Form2:button1启动接收端,button2关闭接收端,button3传递系统消息和自定义的消息。
public partial class Form1 : Form
&&&&&&&&[DllImport("User32.dll", EntryPoint = "SendMessage")]
&&&&&&&&private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
&&&&&&&&//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
&&&&&&&&//private static extern int StartSendMessage(IntPtr hWnd);
&&&&&&&&//internal string local = "this is a local internal";
&&&&&&&&public Form1()
&&&&&&&&&&&&InitializeComponent();
&&&&&&&&//ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
&&&&&&&&ProcessStartInfo startInfo = new ProcessStartInfo();
&&&&&&&&Process pro = new Process();
&&&&&&&&private void Form1_Load(object sender, EventArgs e)
&&&&&&&&&&&&startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
&&&&&&&&&&&&pro.StartInfo = startInfo;
&&&&&&&&private void button1_Click(object sender, EventArgs e)
&&&&&&&&&&&&pro.Start();
&&&&&&&&private void button2_Click(object sender, EventArgs e)
&&&&&&&&&&&&pro.Kill();
&&&&&&&&private void button3_Click(object sender, EventArgs e)
&&&&&&&&&&&&IntPtr hWnd = pro.MainWindowHandle;
&&&&&&&&&&&&int data = Convert.ToInt32(this.textBox1.Text);
&&&&&&&&&&&&SendMessage(hWnd, 0x0100, data, 0);
&&&&&&&&&&&&SendMessage(hWnd, Message.WM_TEST, 300, 300);
4.界面显示:
点击button1:
左侧窗体为发送端,右侧窗体为接收端。
左侧窗体在文本框中输入数值,然后点击button2后,左右窗体显示如下:
4.关于消息驱动
现在VS上已经由事件驱动取代了消息驱动,不过像消息驱动还是在进程间通信常用的。此外本来试着通过控制台应用程序向窗体发送消息,没有发送成功(如果有高人解决了,麻烦告诉me一下下哈)。不过一般消息发送与处理都是窗口之间进行的。
5.附录:系统消息
6.链接资料网站
发送和接收消息:
SendMessage和PostMessage函数:
阅读(26971) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~
请登录后评论。2添加评论分享收藏感谢收起博客分类:
WinFrom的按钮中没有ToolTip的属性设置,如何让按钮可以显示提示信息呢,可以参考如下代码:
public Form1()
InitializeComponent();
InitButtonTooltip();
private void InitButtonTooltip()
ToolTip ttSetp1 = new System.Windows.Forms.ToolTip();
ttSetp1.SetToolTip(btnStep1, "第一步:将上次数据保存到历史数据表,并清空当前数据表。");
结果如下:
浏览: 416977 次
来自: 昆明
三五天的没有,一周入门的视频教程我知道http://www.s ...
[b][/b][flash=200,200][flash=20 ...
我们https://yunba.io/也是基于MQTT协议实现 ...
GoEasyweb 推送基于websocket 和pollin ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'向另一个进程窗口发消息?
[问题点数:20分,结帖人xianglitian]
本版专家分:5
结帖率 64.38%
CSDN今日推荐
本版专家分:18386
本版专家分:18386
本版专家分:709
本版专家分:0
本版专家分:311
本版专家分:311
本版专家分:45
本版专家分:147
本版专家分:3260
本版专家分:19126
2003年6月 C/C++大版内专家分月排行榜第一2002年5月 C/C++大版内专家分月排行榜第一
2003年9月 C/C++大版内专家分月排行榜第二2002年6月 C/C++大版内专家分月排行榜第二2002年4月 C/C++大版内专家分月排行榜第二
2007年3月 C/C++大版内专家分月排行榜第三2007年2月 C/C++大版内专家分月排行榜第三2007年1月 C/C++大版内专家分月排行榜第三2003年7月 C/C++大版内专家分月排行榜第三
本版专家分:6023
本版专家分:1014
匿名用户不能发表回复!|
其他相关推荐I'm making an injected .dll written in C++, and I want to communicate with a C# app using named pipes.
Now, I am using the built in System.IO.Pipe .net classes in the C# app, and I'm using the regular functions in C++.
I don't have much experience in C++ (Read: This is my first C++ code..), tho I'm experienced in C#.
It seems that the connection with the server and the client is working, the only problem is the messaged aren't being send. I tried making the .dll the server, the C# app the server, making the pipe direction InOut (duplex) but none seems to work.
When I tried to make the .dll the server, which sends messages to the C# app, the code I used was like this:
DWORD ServerCreate() // function to create the server and wait till it successfully creates it to return.
hPipe = CreateNamedPipe(pipename,//The unique pipe name. This string must have the following form:
\\.\pipe\pipename
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT, //write and read and return right away
PIPE_UNLIMITED_INSTANCES,//The maximum number of instances that can be created for this pipe
4096 , // output time-out
4096 , // input time-out
0,//client time-out
if(hPipe== INVALID_HANDLE_VALUE)
return 1;//failed
return 0;//success
void SendMsg(string msg)
WriteFile(hPipe,msg.c_str(), msg.length()+1, &cbWritten,NULL);
void ProccesingPipeInstance()
while(ServerCreate() == 1)//if failed
Sleep(1000);
//if created success, wait to connect
ConnectNamedPipe(hPipe, NULL);
SendMsg("HI!");
if( ConnectNamedPipe(hPipe, NULL)==0)
if(GetLastError()==ERROR_NO_DATA)
DebugPrintA("previous closed,ERROR_NO_DATA");
DisconnectNamedPipe(hPipe);
ConnectNamedPipe(hPipe, NULL);
Sleep(1000);
And the C# cliend like this:
static void Main(string[] args)
Console.WriteLine("Hello!");
using (var pipe = new NamedPipeClientStream(".", "HyprPipe", PipeDirection.In))
Console.WriteLine("Created Client!");
Console.Write("Connecting to pipe server in the .dll ...");
pipe.Connect();
Console.WriteLine("DONE!");
using (var pr = new StreamReader(pipe))
while ((t = pr.ReadLine()) != null)
Console.WriteLine("Message: {0}",t);
I see that the C# client connected to the .dll, but it won't receive any message..
I tried doing it the other way around, as I said before, and trying to make the C# send messages to the .dll, which would show them in a message box.
The .dll was injected and connected to the C# server, but when It received a message it just crashed the application it was injected to.
Please help me, or guide me on how to use named pipes between C++ and C# app
解决方案 A few things.
1- Are you using the same pipe name
C++ : "\\.\pipe\HyperPipe"
: "HyperPipe"
2- I think on the C# side it might be better to use ReadToEnd(), I have only used named pipes in C++, but I assume that ReadToEnd() will read a message since you are using message
based pipes.
3- On the C++ side, you are trying to use non-blocking pipes and polling the pipe for connection and data etc. I would suggest one of three things.
a - Use a blocking pipe on a separate thread or
b - Use non blocking pipes using Overlapped IO
c - Use non blocking pipes using IOCompletion ports
The first option would be the easiest and for what it sounds like you are doing, it will scale fine. Here is a link to a simple sample to (a)
4- Make sure that your encodings match on both sides. For example, if your C++ code is compiled for Unicode, you must read the Pipe stream on the C# side using Unicode encoding.
Some thing like the following.
using (StreamReader rdr = new StreamReader(pipe, System.Text.Encoding.Unicode))
System.Console.WriteLine(rdr.ReadToEnd());
Update: Since I had not worked with this in C# I thought I would write a small test. Just using blocking pipes no threading or anything, just to confirm the basics work, here is the very rough test code.
C++ Server
#include &tchar.h&
#include &windows.h&
int _tmain(int argc, _TCHAR* argv[])
HANDLE hPipe = ::CreateNamedPipe(_T("\\\\.\\pipe\\HyperPipe"),
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
PIPE_UNLIMITED_INSTANCES,
ConnectNamedPipe(hPipe, NULL);
LPTSTR data = _T("Hello");
DWORD bytesWritten = 0;
WriteFile(hPipe, data, _tcslen(data) * sizeof(TCHAR), &bytesWritten, NULL);
CloseHandle(hPipe);
using System.T
using System.IO;
using System.IO.P
namespace CSPipe
class Program
static void Main(string[] args)
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "HyperPipe", PipeDirection.InOut);
pipe.Connect();
using (StreamReader rdr = new StreamReader(pipe, Encoding.Unicode))
System.Console.WriteLine(rdr.ReadToEnd());
Console.ReadKey();
本文地址: &
我在做一个注入的.dll文件C ++编写的,我想使用命名管道一个C#应用程序进行通信。
现在,我使用的是内置在System.IO.Pipe .NET中的C#应用程序类,而我使用C ++的常规功能。
我没有用C多少经验++(阅读:这是我的第一个C ++代码..),尽管我在C#中我经历了
这似乎与服务器和客户端的连接工作时,唯一的问题是在传递消息不被发送。我试图使.dll文件服务器上,C#应用程序服务器,使管方向的InOut(双面),但没有一个似乎工作。
当我试图使为.dll的服务器,将消息发送到C#应用程序,我使用的代码是这样的:
DWORD ServerCreate()/ /函数来创建服务器,等到它成功地创建它返回。
{ hPipe = CreateNamedPipe时(pipename,//唯一的管道名此字符串必须有以下形式:\\.\pipe\pipename
PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,//写入和读出并立即返回 PIPE_UNLIMITED_INSTANCES,//可为此管 4096中创建的实例的最大数目,//输出超时 4096,//输入超时 0,//客户端超时 NULL);
如果(hPipe == INVALID_HANDLE_VALUE) {返回1; //失败} ,否则返回0; //成功}
无效SENDMSG(string信息) { DWORD cbW
WriteFile的(hPipe,msg.c_str(),msg.length()+ 1,&安培; cbWritten,NULL); }
无效ProccesingPipeInstance() {,而(ServerCreate()== 1)//如果失败 {睡眠( 1000); }
//如果创建成功,等待连接 ConnectNamedPipe(hPipe,NULL); 为(;;) { SENDMSG(“HI!”); 如果(ConnectNamedPipe(hPipe,NULL)== 0)如果(GetLastError函数()== ERROR_NO_DATA) { DebugPrintA(“以前关闭,ERROR_NO_DATA”);
DisconnectNamedPipe(hPipe);
ConnectNamedPipe(hPipe,NULL); } 睡眠(1000);
和C#的cliend是这样的:
静态无效的主要(字串[] args) { Console.WriteLine(“你好!”);
使用 { Console.WriteLine(“创建的客户端!”)(VAR管=新NamedPipeClientStream(“HyprPipe”,PipeDirection.In)“。”);
Console.Write(“连接到管道服务器在该.dll ......”);
pipe.Connect();
Console.WriteLine(“DONE!”);
使用(VAR PR =新的StreamReader(管道)) {串吨; ,而((T = pr.ReadLine())!= NULL) { Console.WriteLine(“消息:{0}”,T); } } } }
我看到C#客户端连接到.dll文件,但它不会收到任何消息.. 我想围绕做它的其他方式,如我之前说的,并试图让C#发送消息给。 DLL中,这将让他们在一个消息框。 中的.dll文件注入,并连接到C#的服务器,但是,当它接收到的消息,它只是坠毁注入到应用程序。
请帮我或指导我如何使用C的命名管道++和C#应用程序
解决方案 有几件事情。
- 您是否使用相同的管道名称
C ++:“\\.\pipe\HyperPipe”
C# “HyperPipe”
- 我想在C#方面,它可能是更好地使用ReadToEnd的(),我只用在一个名为C ++的管道,但我认为ReadToEnd的()将读取的消息,因为你正在使用的消息为基础的管道。
3在C ++的一面,你要使用非阻塞管道和轮询连接和数据等管我建议的三件事之一。
- 使用重叠IO $ b。使用非阻塞管道 - 在一个单独的线程或B使用管道堵塞$ BC
- 使用的IoCompletion端口使用非阻塞管道
第一个选择是最简单,它所听起来像你正在做的,它将规模罚款。这里是一个链接到一个简单的样品(一)
4-确保您的编码上都匹配两侧。例如,如果你的C ++代码编译为Unicode的,你必须阅读使用Unicode编码C#侧的管道流。 像下面这样的一些东西。
使用(StreamReader的RDR =新的StreamReader(管道,System.Text.Encoding。 Unicode)的) {的System.Console.WriteLine(rdr.ReadToEnd()); }
更新:既然我已经不是这个工作在C#中,我想我会写一个小测试。只需使用阻塞的管道没有线程或任何东西,只是为了确认的基础工作,这里是非常粗略的测试代码。
C ++服务器
的#include<&TCHAR.H GT; &#包括LT; WINDOWS.H>
INT _tmain(INT ARGC,_TCHAR *的argv []) { HANDLE hPipe = :: CreateNamedPipe时(_T(“\\\\.\ \pipe\\HyperPipe“), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL);
ConnectNamedPipe(hPipe,NULL);
LPTSTR数据= _T(“你好”);
DWORD bytesWritten = 0;
WriteFile的(hPipe,数据,_tcslen(数据)*的sizeof(TCHAR),放大器; bytesWritten,NULL);
CloseHandle的(hPipe); 返回0; }
使用系统; 使用System.T :使用System.IO;使用System.IO.Pipes ;
命名空间CSPipe
{类节目 {静态无效的主要(字串[] args) { NamedPipeClientStream管=新NamedPipeClientStream(“。”,“HyperPipe”,PipeDirection.InOut);
pipe.Connect();使用(StreamReader的RDR =新的StreamReader(管道,Encoding.Unicode)) {的System.Console.WriteLine (rdr.ReadToEnd()); }
Console.ReadKey(); } } }
本文地址: &
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起
(window.slotbydup = window.slotbydup || []).push({
id: '5828425',
container: s,
size: '300,250',
display: 'inlay-fix'

我要回帖

更多关于 电脑发送消息哪个键盘 的文章

 

随机推荐