博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]C#获取窗口进程ID与句柄还有读写内存类
阅读量:6253 次
发布时间:2019-06-22

本文共 2317 字,大约阅读时间需要 7 分钟。

  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. }

转载地址:http://ytjsa.baihongyu.com/

你可能感兴趣的文章
Second glance in Go
查看>>
java 遍历arrayList的四种方法
查看>>
rm 命令(转)
查看>>
[禅悟人生]真知从实践中来
查看>>
Chrome 报 Resource interpreted as Script but transferred with MIME type text/plain 警告的解决办法...
查看>>
memcpy的使用方法总结
查看>>
关于C++ const 的全面总结
查看>>
[转载]如何破解Excel VBA密码
查看>>
【BZOJ】3757: 苹果树
查看>>
clang
查看>>
IOS成长之路-Nsstring中搜索方法rangeOfString
查看>>
递归函数的概念使用方法与实例
查看>>
RMAN_学习笔记4_RMAN Virtual Catalog虚拟恢复目录
查看>>
cf451C-Predict Outcome of the Game
查看>>
struct dev_t
查看>>
Java 原型模式
查看>>
【转】Android4.3 蓝牙BLE初步
查看>>
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
查看>>
hadoop2.0 和1.0的区别
查看>>
手机web——自适应网页设计(html/css控制) - 51CTO.COM
查看>>