惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
博客园_首页
云风的 BLOG
云风的 BLOG
月光博客
月光博客
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
D
Docker
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
博客园 - 叶小钗
Martin Fowler
Martin Fowler
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - LT

用vs code写代码,spyder看变量,只能这样了 Chrome禁用software_reporter_tool 【转载】Scarbee Pre-Bass 贝司的使用教程 罗兰管弦乐音色表【中英文对照】 ----转载 博文阅读密码验证 - 博客园 火狐无法启动,如何恢复数据、书签、密码 【转】C#写的170行俄罗斯方块 一个好用的C#抓图类 【转】WinSocket编程——原始套接字 【转】SHarpPcap(winpcap基于c#封转的库)网络嗅探程序核心 [心得] socket 多路复用误区 UDP 异步 收发 服务端例子 【转载】 UDPClient 的奇特现象,实在搞不懂 [转载]TCP打开端口数和打开连接数限制系列文章 UDP错误10054:远程主机强迫关闭的解决方法---转载 应用程式上屏蔽flash控件的右键菜单并获得左键消息_c# AJAX分段下载/读取HTML内容(有效节省带宽加快运行速度) 使用Fiddler2录制HTTP操作脚本 Subversion for Windows 安装配置
[转]C#获取窗口进程ID与句柄还有读写内存类
LT · 2012-02-20 · via 博客园 - LT
  1. using System;
  2. using System.Text;

  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;

  5. namespace PlantsVsZombiesTool
  6. {
  7.     
  8.     public abstract class Helper
  9.     {
  10.         [DllImportAttribute("kernel32.dll", EntryPoint = "ReadProcessMemory")]
  11.         public static extern bool ReadProcessMemory
  12.             (
  13.                 IntPtr hProcess,
  14.                 IntPtr lpBaseAddress,
  15.                 IntPtr lpBuffer,
  16.                 int nSize,
  17.                 IntPtr lpNumberOfBytesRead
  18.             );

  19.         [DllImportAttribute("kernel32.dll", EntryPoint = "OpenProcess")]
  20.         public static extern IntPtr OpenProcess
  21.             (
  22.                 int dwDesiredAccess,
  23.                 bool bInheritHandle,
  24.                 int dwProcessId
  25.             );

  26.         [DllImport("kernel32.dll")]
  27.         private static extern void CloseHandle
  28.             (
  29.                 IntPtr hObject
  30.             );

  31.         //写内存
  32.         [DllImportAttribute("kernel32.dll", EntryPoint = "WriteProcessMemory")]
  33.         public static extern bool WriteProcessMemory
  34.             (
  35.                 IntPtr hProcess,
  36.                 IntPtr lpBaseAddress,
  37.                 int[] lpBuffer,
  38.                 int nSize,
  39.                 IntPtr lpNumberOfBytesWritten
  40.             );

  41.         //获取窗体的进程标识ID
  42.         public static int GetPid(string windowTitle)
  43.         {
  44.             int rs = 0;
  45.             Process[] arrayProcess = Process.GetProcesses();
  46.             foreach (Process p in arrayProcess)
  47.             {
  48.                 if (p.MainWindowTitle.IndexOf(windowTitle) != -1)
  49.                 {
  50.                     rs = p.Id;
  51.                     break;
  52.                 }
  53.             }

  54.             return rs;
  55.         }

  56.         //根据进程名获取PID
  57.         public static int GetPidByProcessName(string processName)
  58.         {
  59.             Process[] arrayProcess = Process.GetProcessesByName(processName);

  60.             foreach (Process p in arrayProcess)
  61.             {
  62.                 return p.Id;
  63.             }
  64.             return 0;
  65.         }

  66.         //根据窗体标题查找窗口句柄(支持模糊匹配)
  67.         public static IntPtr FindWindow(string title)
  68.         {
  69.             Process[] ps = Process.GetProcesses();
  70.             foreach (Process p in ps)
  71.             {
  72.                 if (p.MainWindowTitle.IndexOf(title) != -1)
  73.                 {
  74.                     return p.MainWindowHandle;
  75.                 }
  76.             }
  77.             return IntPtr.Zero;
  78.         }

  79.         //读取内存中的值
  80.         public static int ReadMemoryValue(int baseAddress,string processName)
  81.         {
  82.             try
  83.             {
  84.                 byte[] buffer = new byte[4];
  85.                 IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //获取缓冲区地址
  86.                 IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName));
  87.                 ReadProcessMemory(hProcess, (IntPtr)baseAddress, byteAddress, 4, IntPtr.Zero); //将制定内存中的值读入缓冲区
  88.                 CloseHandle(hProcess);
  89.                 return Marshal.ReadInt32(byteAddress);
  90.             }
  91.             catch
  92.             {
  93.                 return 0;
  94.             }
  95.         }

  96.         //将值写入指定内存地址中
  97.         public static void WriteMemoryValue(int baseAddress, string processName, int value)
  98.         {
  99.             IntPtr hProcess = OpenProcess(0x1F0FFF, false, GetPidByProcessName(processName)); //0x1F0FFF 最高权限
  100.             WriteProcessMemory(hProcess, (IntPtr)baseAddress, new int[] { value }, 4, IntPtr.Zero);
  101.             CloseHandle(hProcess);
  102.         }
  103.     }
  104. }

posted on 2012-02-20 12:44  LT  阅读(1694)  评论()    收藏  举报