'I get errorcode 0 when I use VirtualFreeEx

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);
}


Solution 1:[1]

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);
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lepis