使用VirtualFreeex时,我会获得错误代码0

发布于 2025-01-25 22:17:43 字数 1742 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-02-01 22:17:43

这是有效的代码。

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

    [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", SetLastError = true)]
    public static extern bool VirtualFreeEx(IntPtr hProcess, UIntPtr lpAddress,
      uint dwSize, int dwFreeType);
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern int MessageBoxA(
    IntPtr hWnd, string lpText, string lpCaption, uint uType);

  public const int
    PAGE_READWRITE = 0x40,
    PROCESS_VM_OPERATION = 0x0008,
    PROCESS_VM_READ = 0x0010,
    PROCESS_VM_WRITE = 0x0020,
    MEM_COMMIT = 0x00001000,
    MEM_RESERVE = 0x00002000,
    MEM_DECOMMIT = 0x00004000,
     MEM_RELEASE = 0x00008000;
 IntPtr whWnd = FindWindow(null, "yourapp");
                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)0x13FFF0000, 1000,MEM_COMMIT, PAGE_READWRITE);



System.Threading.Thread.Sleep(2300);
bool crao =  VirtualFreeEx(hprocess, codecavebase, 0, MEM_RELEASE);

            if (!crao)
            {
                int error = Marshal.GetLastWin32Error();

                MessageBox.Show("The last Win32 Error was: " + error);
            }

This is the code that's working.

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

    [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", SetLastError = true)]
    public static extern bool VirtualFreeEx(IntPtr hProcess, UIntPtr lpAddress,
      uint dwSize, int dwFreeType);
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern int MessageBoxA(
    IntPtr hWnd, string lpText, string lpCaption, uint uType);

  public const int
    PAGE_READWRITE = 0x40,
    PROCESS_VM_OPERATION = 0x0008,
    PROCESS_VM_READ = 0x0010,
    PROCESS_VM_WRITE = 0x0020,
    MEM_COMMIT = 0x00001000,
    MEM_RESERVE = 0x00002000,
    MEM_DECOMMIT = 0x00004000,
     MEM_RELEASE = 0x00008000;
 IntPtr whWnd = FindWindow(null, "yourapp");
                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)0x13FFF0000, 1000,MEM_COMMIT, PAGE_READWRITE);



System.Threading.Thread.Sleep(2300);
bool crao =  VirtualFreeEx(hprocess, codecavebase, 0, MEM_RELEASE);

            if (!crao)
            {
                int error = Marshal.GetLastWin32Error();

                MessageBox.Show("The last Win32 Error was: " + error);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文