C# 操作内存案例

1 //C#读取内存例子 
  2
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Text;
  6 using System.Runtime.InteropServices;
  7 using System.Diagnostics;
  8 using System.Management;
  9
10 public class key
11     {
12         const uint PROCESS_ALL_ACCESS = 0x001F0FFF;
13         const uint KEYEVENTF_EXTENDEDKEY = 0x1;
14         const uint KEYEVENTF_KEYUP = 0x2;
15         private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
16         private readonly int MOUSEEVENTF_LEFTUP = 0x4;
17         const uint KBC_KEY_CMD = 0x64;
18         const uint KBC_KEY_DATA = 0x60;
19         //得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
20         [DllImport("user32.dll", CharSet = CharSet.Auto)]
21         public static extern IntPtr FindWindow(
22         string lpClassName, // pointer to class name
23         string lpWindowName // pointer to window name
24         );
25         [DllImport("user32.dll")]
26         private static extern int GetWindowThreadProcessId(IntPtr id, int pid);
27
28         [DllImport("kernel32.dll")]
29         private static extern void CloseHandle
30         (
31         uint hObject //Handle to object
32         );
33         //读取进程内存的函数
34         [DllImport("kernel32.dll")]
35         static extern bool ReadProcessMemory(uint hProcess, IntPtr lpBaseAddress,
36         IntPtr lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
37         //得到目标进程句柄的函数
38         [DllImport("kernel32.dll")]
39         public static extern uint OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
40         //鼠标事件声明
41         [DllImport("user32.dll")]
42         static extern bool setcursorpos(int x, int y);
43         [DllImport("user32.dll")]
44         static extern void mouse_event(mouseeventflag flags, int dx, int dy, uint data, UIntPtr extrainfo);
45         //键盘事件声明
46         [DllImport("user32.dll")]
47         static extern byte MapVirtualKey(byte wCode, int wMap);
48         [DllImport("user32.dll")]
49         static extern short GetKeyState(int nVirtKey);
50         [DllImport("user32.dll")]
51         static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
52         //键盘事件声明winio
53         [DllImport("winio.dll")]
54         public static extern bool InitializeWinIo();
55         [DllImport("winio.dll")]
56         public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize);
57         [DllImport("winio.dll")]
58         public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize);
59         [DllImport("winio.dll")]
60         public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle);
61         [DllImport("winio.dll")]
62         public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr);
63         [DllImport("winio.dll")]
64         public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal);
65         [DllImport("winio.dll")]
66         public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal);
67         [DllImport("winio.dll")]
68         public static extern void ShutdownWinIo();
69
70
71
72
73         /// <summary>
74         /// 获取进程pid
75         /// </summary>
76         /// <param name="name"></param>
77         /

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值