首页 > 系统相关 >C# 写入内存

C# 写入内存

时间:2024-04-25 16:13:55浏览次数:27  
标签:IntPtr processName C# 写入 int static 内存 public hProcess

public class Helper
{
	[DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
	public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, IntPtr lpNumberOfBytesRead);

	[DllImport("kernel32.dll", EntryPoint = "OpenProcess")]
	public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

	[DllImport("kernel32.dll")]
	private static extern void CloseHandle(IntPtr hObject);

	[DllImport("kernel32.dll", EntryPoint = "WriteProcessMemory")]
	public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, int[] lpBuffer, int nSize, IntPtr lpNumberOfBytesWritten);

	public static int ReadMemoryValue(int baseAddress, string processName)
	{
		try
		{
			byte[] buffer = new byte[4];
			IntPtr byteAddress = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); //获取缓冲区地址
			IntPtr hProcess = OpenProcess(0x1f0fff, false, GetPidByProcessName(processName));
			ReadProcessMemory(hProcess, (IntPtr)baseAddress, byteAddress, 4, IntPtr.Zero); //将制定内存中的值读入缓冲
			CloseHandle(hProcess);
			return Marshal.ReadInt32(byteAddress);
		}
		catch
		{
			return 0;
		}
	}

	public static void WriteMemory(int baseAddress, string processName, int value)
	{
		var hProcess = OpenProcess(0x1f0fff, false, GetPidByProcessName(processName));
		WriteProcessMemory(hProcess, (IntPtr)baseAddress, new int[] { value }, 4, IntPtr.Zero);
		CloseHandle(hProcess);
	}


	public static void KillProcessByPid(int pid)
	{
		var process = Process.GetProcessById(pid);
		process?.Kill();
	}

	public static void KillProcessByWinTitle(string winTitle)
	{
		var processes = Process.GetProcesses();
		var process = processes.FirstOrDefault(z => z.MainWindowTitle.Contains(winTitle));
		process?.Kill();
	}

	public static void KillProcessByProcessName(string processName)
	{
		var processes = Process.GetProcessesByName(processName);
		processes.FirstOrDefault()?.Kill();
	}

	public static int GetPidByWinTitle(string winTitle)
	{
		var processes = Process.GetProcesses();
		return processes.FirstOrDefault(z => z.MainWindowTitle.Contains(winTitle))?.Id ?? 0;
	}

	public static int GetPidByProcessName(string processName)
	{
		var processes = Process.GetProcessesByName(processName);
		return processes.FirstOrDefault()?.Id ?? 0;
	}
}

标签:IntPtr,processName,C#,写入,int,static,内存,public,hProcess
From: https://www.cnblogs.com/fires/p/18157908

相关文章

  • echart 常用属性
    echart常用属性基础属性title左上角标题legend每一项的列表xAxis:x轴上的数据yAxis:y轴上的数据提示框tooltip:{trigger:'axis'},demo地址:https://echarts.apache.org/v4/examples/zh/editor.html?c=line-stack文字转动斜着摆放axisLabel.rota......
  • el-select @change事件获取整个对象
    <el-form-itemlabel="使用人"prop="useBy"><el-selectref="useByRef"v-model="form.useBy"filterableplaceholder="请输入员工名称"style="width:100%"no-match-text="请输入员工名称&qu......
  • LFI to RCE [NewStarCtf]Include
    记录一个没见过的RCE类型题目。先看源码:点击查看代码<?phperror_reporting(0);if(isset($_GET['file'])){$file=$_GET['file'];if(preg_match('/flag|log|session|filter|input|data/i',$file)){die(&#......
  • 如何使用 SFDX CLI 拉取存储在 Public 文件夹的邮件模版(How to retrieve an email tem
    SELECTDeveloperName,FolderName,IsActiveFROMEmailTemplateSELECTDeveloperName,Folder.DeveloperName,IsActiveFROMEmailTemplate使用下面的命令可以正确获取到public文件夹下的邮件模版sfdxforce:source:retrieve-mEmailTemplate:unfiled\$public\/My_te......
  • C语言实现封装特性
    注:本文原型摘自《架构整洁之道》第5章面向对象编程,经过作者加工修改1.封装特性封装是面向对象编程的基本思想之一,通过采用封装特性,我们可以把一组相关联的数据和函数圈起来,使圈外面的代码只能看见部分函数,数据则完全不可见。然而由于C++编译器必须知道每个类实例的大小,因此要......
  • Avalonia UI 中 Styles 与 ControlTheme 的区别
    目录目录介绍使用方式全局主题(GlobalTheme)局部主题(LocalTheme)控件主题(ControlTheme)问题描述问题分析问题1区别问题2重写Template用Styles还是ControlTheme?使用ControlTheme最佳实践总结Style&ControlTheme的特性独立性继承性......
  • CA-410 色彩分析仪
    CA-310升级型号,快速、高分辨率,支持HDR、广色域和OLED等新型显示测量的新一代色彩分析仪1、CA-410在0.03cd/m2的暗场测量非常稳定,但CA-310就忽高忽低了。2、CA-410在测量Gamma速度时,比CA-310快1.5倍3、CA-410非常便携4、CA-410带自动调零功能5、由于XYZ滤色片的精度提升,CA......
  • Django admin static files errors All In One
    DjangoadminstaticfileserrorsAllInOne404errorshttp://127.0.0.1:8000/admin/demos$pythonmanage.pyrunserver#admin#123456#admin@django.comhttp://127.0.0.1:8000/admin/login/?next=/admin/(......
  • Tomcat生命周期管理
    一、生命周期统一接口Lifecycle:在自定义Mini-Tomcat之动态资源访问实现中已分析tomcat的两个功能,通过server.xml配置可以进行设置。下面看看Tomcat的实际的server.xml配置:从配置结构看,Tomcat内部架构中各个核心组件是包含于被包含的关系,往下层层......
  • ubuntu 20安装Visual Studio Code后出现——root权限登录——转载
    VisualStudioCode报错:YouaretryingtostartVisualStudioCodeasasuperuserwhichisn‘t......快乐小炮灰已于2023-11-2519:46:51修改阅读量2.1k收藏10点赞数4分类专栏:linuxC++开发工具IDE文章标签:vscodeide编辑器版权linuxC++开发工具同时被2个......