C# 向指定的进程发送消息
public static class ProcessExtensions
{
// Messages
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_CHAR = 0x105;
const int WM_SYSKEYDOWN = 0x104;
const int WM_SYSKEYUP = 0x105; private static class Win32
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); [return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostThreadMessage(uint threadId, uint msg, IntPtr wParam, IntPtr lParam); public delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
} //Sends a message to the first enumerated window in the first enumerated thread with at least one window, and returns the handle of that window through the hwnd output parameter if such a window was enumerated. If a window was enumerated, the return value is the return value of the SendMessage call, otherwise the return value is zero.
public static IntPtr SendMessage(this Process p, out IntPtr hwnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
{
hwnd = p.WindowHandles().FirstOrDefault();
if (hwnd != IntPtr.Zero)
return Win32.SendMessage(hwnd, msg, wParam, lParam);
else
return IntPtr.Zero;
} public static void SendKey(this Process p, Keys key)
{
IntPtr hwnd = p.WindowHandles().FirstOrDefault();//IntPtr.Zero; //this will receive a non-zero value if SendMessage was called successfully
//uint msg = 0xC000; //The message you want to send
IntPtr wParam =new IntPtr( Convert.ToInt32(key));// IntPtr.Zero; //The wParam value to pass to SendMessage
IntPtr lParam = IntPtr.Zero; //The lParam value to pass to SendMessage
Win32.SendMessage(hwnd, WM_KEYDOWN, wParam, lParam);
Win32.SendMessage(hwnd, WM_KEYUP, wParam, lParam);
//SendMessage(hwndMessageWasSentTo, WM_KEYDOWN, Convert.ToInt32(key), 0);
//SendMessage(hwndMessageWasSentTo, WM_KEYUP, Convert.ToInt32(key), 0);
} //Posts a message to the first enumerated window in the first enumerated thread with at least one window, and returns the handle of that window through the hwnd output parameter if such a window was enumerated. If a window was enumerated, the return value is the return value of the PostMessage call, otherwise the return value is false.
public static bool PostMessage(this Process p, out IntPtr hwnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
{
hwnd = p.WindowHandles().FirstOrDefault();
if (hwnd != IntPtr.Zero)
return Win32.PostMessage(hwnd, msg, wParam, lParam);
else
return false;
} //Posts a thread message to the first enumerated thread (when ensureTargetThreadHasWindow is false), or posts a thread message to the first enumerated thread with a window, unless no windows are found in which case the call fails. If an appropriate thread was found, the return value is the return value of PostThreadMessage call, otherwise the return value is false.
public static bool PostThreadMessage(this Process p, UInt32 msg, IntPtr wParam, IntPtr lParam, bool ensureTargetThreadHasWindow = true)
{
uint targetThreadId = 0;
if (ensureTargetThreadHasWindow)
{
IntPtr hwnd = p.WindowHandles().FirstOrDefault();
uint processId = 0;
if (hwnd != IntPtr.Zero)
targetThreadId = Win32.GetWindowThreadProcessId(hwnd, out processId);
}
else
{
targetThreadId = (uint)p.Threads[].Id;
}
if (targetThreadId != 0)
return Win32.PostThreadMessage(targetThreadId, msg, wParam, lParam);
else
return false;
} public static IEnumerable<IntPtr> WindowHandles(this Process process)
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in process.Threads)
Win32.EnumThreadWindows((uint)thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
return handles;
}
}
C# 向指定的进程发送消息的更多相关文章
- 【C#】无损转换Image为Icon 【C#】组件发布:MessageTip,轻快型消息提示窗 【C#】给无窗口的进程发送消息 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体 【手记】调用Process.EnterDebugMode引发异常:并非所有引用的特权或组都分配给呼叫方 【C#】DataRowState演变备忘
[C#]无损转换Image为Icon 如题,市面上常见的方法是: var handle = bmp.GetHicon(); //得到图标句柄 return Icon.FromHandle(handle ...
- smbcontrol - 向smbd或nmbd进程发送消息
总览smbcontrol [ -i ] smbcontrol [ 目标 ] [ 消息类型 ] [ 参数 ] 描述这个工具是是Samba组件的一部分. smbcontrol是个很小的程序,用它可以向系统 ...
- 【C#】给无窗口的进程发送消息
注:本文适用.net2.0+的winform程序 一个winform程序,我希望它不能多开(但是如何防多开不是本文要讲的),那么在用户启动第二个实例的时候,作为第二个实例来说,大概可以有这么几种做法: ...
- 跨进程发送消息数据(发送WM_COPYDATA消息,够简单的)
1 //1.发送窗体 2 procedure TForm2.Button1Click(Sender: TObject); 3 var 4 h: HWND; 5 Size: Integer; 6 Cop ...
- Android为TV端助力:EventBus跨进程发送消息
单一app内的用法 如果你在单一app内进行多进程开发,那么只需要做以下三步: Step 1 在gradle文件中加入下面的依赖: dependencies { compile 'xiaofe ...
- java集成WebSocket向指定用户发送消息
一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通 ...
- SSM框架下使用websocket实现后端发送消息至前端
本篇文章本人是根据实际项目需求进行书写的第一版,里面有些内容对大家或许没有用,但是核心代码本人已对其做了红色标注.文章讲解我将从maven坐标.HTML页面.js文件及后端代码一起书写. 一.mave ...
- socket 服务器向指定的客户端发消息
一.需求 需求如题. 当多个客户端连接服务器时,服务器如何给指定的客户端发送消息. 二.解决方案 核心思想: 在服务器端,需保存不同客户端的socket列表及客户端相关信息. socket含有发送方和 ...
- Spring Boot+Socket实现与html页面的长连接,客户端给服务器端发消息,服务器给客户端轮询发送消息,附案例源码
功能介绍 客户端给所有在线用户发送消息 客户端给指定在线用户发送消息 服务器给客户端发送消息(轮询方式) 项目搭建 项目结构图 pom.xml <?xml version="1.0&q ...
随机推荐
- Maven 入门指南
为什么要用 Maven? Maven 主要帮助用户完成以下 3 个方面的工作: 生命周期管理,便捷的构建过程: 依赖管理,方便引入所需依赖 Jar 包: 仓库管理,提供统一管理所有 Jar 包的工具: ...
- solr4.7配置(ik-analyzer)
环境: windows server 2003 sp2 x86 tomcat8.0 solr-4.7.2 IK Analyzer 2012FF_hf1 ————————————华丽的分割线—————— ...
- thinkphp生成的验证码不显示问题解决
在调用验证码之前加上 ob_clean(); 不显示验证码的代码: public function verify(){ $verify = new \Think\Verify(); $verify-& ...
- Python3爬虫:利用Fidder抓取手机APP的数据
1.什么是Fiddler? Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,ht ...
- NLP常用信息资源
ACL Anthology,囊括了ACL,EMNLP,CL等NLP领域重要会议和期刊的论文.http://www.aclweb.org/anthology-new/ LDC: The Linguist ...
- Neo4j 查询某标签节点个数语句 删除某标签全部节点语句
查询:MATCH (n:标签名) RETURN count(n) 删除:MATCH (n:标签名) DELETE n
- MYSQL的服务不见了
多日不用MYSQL数据库,进入突然要用,进入服务列表突然发现...MYSQL服务不见了..不是于是乎..找到命令: F:\www\MySQLServer\bin>mysqld.exe -in ...
- Unable to create new web application
When I try to create a new web application, it just shows message as 'This should'nt take too long ...
- [RxJS] Build your own RxJS
JavaScript has multiple APIs that use callback functions that all do nearly the same thing with slig ...
- mac 安装 RabbitMQ
https://blog.csdn.net/u010046908/article/details/54773323