首页 > 编程语言 >Reading or Writing to Another Processes Memory in C# 转载

Reading or Writing to Another Processes Memory in C# 转载

时间:2023-02-10 05:44:18浏览次数:51  
标签:IntPtr Processes C# Writing int address process byte out

https://www.cnblogs.com/zeroone/p/3766247.html

[Flags]
public enum ProcessAccessFlags : uint
{
    All = 0x001F0FFF,
    Terminate = 0x00000001,
    CreateThread = 0x00000002,
    VMOperation = 0x00000008,
    VMRead = 0x00000010,
    VMWrite = 0x00000020,
    DupHandle = 0x00000040,
    SetInformation = 0x00000200,
    QueryInformation = 0x00000400,
    Synchronize = 0x00100000
}
 
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
 
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
 
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);
 
[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hProcess);

复制代码
Reading from another processes Memory

复制代码
public static byte[] ReadMemory(Process process, int address, int numOfBytes, out int bytesRead)
{
    IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id);
 
    byte[] buffer = new byte[numOfBytes];
 
    ReadProcessMemory(hProc, new IntPtr(address), buffer, numOfBytes, out bytesRead);
    return buffer;
}

复制代码
Here is an example of a call to this function:

Process process = Process.GetProcessesByName("My Apps Name").FirstOrDefault();
int address = 0x02ED2910;

int bytesRead;
byte[] value = ReadMemory(process, address, 4, out bytesRead);
Writing to another processes memory

复制代码
public static bool WriteMemory(Process process, int address, long value, out int bytesWritten)
{
    IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id);
             
    byte[] val = BitConverter.GetBytes(value);
             
    bool worked = WriteProcessMemory(hProc, new IntPtr(address), val, (UInt32) val.LongLength, out bytesWritten);
 
    CloseHandle(hProc);
 
    return worked;
}
复制代码

Here is an example of a call to this function:

Process process = Process.GetProcessesByName("My Apps Name").FirstOrDefault();
int address = 0x02ED2910;

int bytesWritten;
bool worked = WriteMemory(process, address, value, out bytesWritten);

标签:IntPtr,Processes,C#,Writing,int,address,process,byte,out
From: https://www.cnblogs.com/dewxin/p/17107661.html

相关文章

  • 不用Blazor WebAssembly,开发在浏览器端编译和运行C#代码的网站
    本文中,我将会为大家分享一个如何用.NET技术开发“在浏览器端编译和运行C#代码的工具”,核心的技术就是用C#编写不依赖于Blazor框架的WebAssembly以及Roslyn技术。一、为什......
  • 代码生成Code generation to LLVM IR
    代码生成CodegenerationtoLLVMIR代码生成的准备工作在开始生成LLVMIR之前,还有一些准备工作要做。首先,给每个AST类添加一个虚函数Codegen(codegeneration),用于实现代......
  • LeetCode接雨水(/dp 单调栈 双指针)
    原题解题目给定n个非负整数表示每个宽度为1的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。约束题解解法一classSolution{public:inttra......
  • C#数据类型与数据库字段类型对应
    数据库C#程序intint32textstringbigintint64binarySystem.Byte[]bitBooleancharstringdatetimeSystem.D......
  • Kubernetes(k8s)控制器(四):ReplicaSet
    目录一.系统环境二.前言三.ReplicaSet概览四.ReplicaSet工作原理五.ReplicaSet使用场景六.创建ReplicaSet七.扩展replicaset副本数一.系统环境服务器版本docker软件......
  • 安装pytorch踩过的坑
    failedwithinitialfrozensolve 可能是由于没有这个版本的matplotlib(包名),可以用'condasearch包名'搜索一下,进一步确认问题下载包的速度慢condacreate-n环境......
  • Codeforces Round #851 (Div. 2) 题解
    CodeforcesRound#851(Div.2)题解A.OneandTwo取\(\log_2\),变成加号,前缀和枚举\(s[i]=\dfrac{s[n]}{2}\)。B.SumofTwoNumbers对于每一位,如果是偶数则平均......
  • [Typescript] Using type predicates
     import{expect,it}from"vitest";import{Equal,Expect}from"../helpers/type-utils";exportconstvalues=["a","b",undefined,"c",undefined];c......
  • cracking the System Design tech interview All In One
    crackingtheSystemDesigntechinterviewAllInOne破解系统设计技术面试SystemDesignhttps://github.com/donnemartin/system-design-primerhttps://github.c......
  • ACP云原生容器工程师-ACK概述
    ACK简介阿里云容器服务ACK,是阿里云针对云原生推出的拳头产品,基于原版Kubernetes进行适配和增强,简化集群的搭建和运维工作,整合阿里云虚拟化、存储、网络和安全能力,使得扩容......