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 /
C# 操作内存案例
最新推荐文章于 2024-05-04 15:16:42 发布