使用VirtualFreeex时,我会获得错误代码0
因此,我正在尝试将内存分配给C#中的virtualAlloCex
,然后在此之后进行处理。
当我按 f1 时,它会分配内存,但不会对其进行分配。当我在virtualFreex
上添加“ getlastrorror”时,我会获得错误代码0。有修复吗?我已经尝试了一切,我不确定我在做什么错。
DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle,
uint dwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint
dwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
int dwSize, int dwFreeType);
public const int
PAGE_READWRITE = 0x40,
PROCESS_VM_OPERATION = 0x0008,
PROCESS_VM_READ = 0x0010,
MEM_COMMIT = 0x00001000,
MEM_DECOMMIT = 0x00004000,
MEM_RELEASE = 0x00008000,
PROCESS_VM_WRITE = 0x0020;
IntPtr whWnd = FindWindow(null, "FIFA 22");
uint procID;
GetWindowThreadProcessId(whWnd, out procID);
IntPtr hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, procID);
IntPtr SHIT = VirtualAllocEx(hProcess, (IntPtr)0xED68000, 1000,MEM_COMMIT, PAGE_READWRITE);
System.Threading.Thread.Sleep(2000);
bool crao = VirtualFreeEx(hProcess, SHIT, 0, MEM_RELEASE);
if (!crao)
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show("The last Win32 Error was: " + error);
}
So, I'm trying to allocate memory to a process with VirtualAllocEx
in C# and then deallocate it, shortly after.
When I press F1, it does allocate memory, but it doesn't deallocate it. I get error code 0 when I've added "getlasterror" on VirtualFreEx
. Any fix? I've tried everything, I m not sure what I am doing wrong.
DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle,
uint dwProcessId);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint
dwProcessId);
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
int dwSize, int dwFreeType);
public const int
PAGE_READWRITE = 0x40,
PROCESS_VM_OPERATION = 0x0008,
PROCESS_VM_READ = 0x0010,
MEM_COMMIT = 0x00001000,
MEM_DECOMMIT = 0x00004000,
MEM_RELEASE = 0x00008000,
PROCESS_VM_WRITE = 0x0020;
IntPtr whWnd = FindWindow(null, "FIFA 22");
uint procID;
GetWindowThreadProcessId(whWnd, out procID);
IntPtr hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, procID);
IntPtr SHIT = VirtualAllocEx(hProcess, (IntPtr)0xED68000, 1000,MEM_COMMIT, PAGE_READWRITE);
System.Threading.Thread.Sleep(2000);
bool crao = VirtualFreeEx(hProcess, SHIT, 0, MEM_RELEASE);
if (!crao)
{
int error = Marshal.GetLastWin32Error();
MessageBox.Show("The last Win32 Error was: " + error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是有效的代码。
This is the code that's working.