Skip to content

windows.winproxy.error.WinproxyError: VirtualAllocEx: [WinError 6] Invalid handle #93

Open

Description

@wkingnet

environment: windows10, python 3.15

I have some assembly code that I need to execute within another process using PythonForWindows.

import windows.native_exec.simple_x86 as x86
import windows.winobject.process

def run_asm(code, pid: int = 0, handle: int = 0, name: str = "", ppid: int = 0) -> None:
    _wp = windows.winobject.process.WinProcess(pid=pid, handle=handle, name=name, ppid=ppid)
    _addr = _wp.virtual_alloc(len(code.get_code()))
    _wp.write_memory(_addr, code.get_code())
    _wp.create_thread(_addr, 0).wait()
    _wp.virtual_free(_addr)


code = x86.MultipleInstr()
code += x86.Pushad()
code += x86.Mov("EBP", "ESP")
code += x86.Mov("ESI", esi)
code += x86.Push(x86.mem("[esi+0x4]"))
code += x86.Lea("EAX", x86.mem("[esi+0x8]"))
code += x86.Push("EAX")
code += x86.Mov("EBX", addr.ess['call_unpack_name'])
code += x86.Call("EBX")
code += x86.Mov("esp", "ebp")
code += x86.Popad()
code += x86.Ret()
run_asm(code, pid=self.pid)

But after I upgraded Python to 3.15, the code stopped working correctly. Here is an error message.

File "E:\code\call\func.py", line 696, in decode_name
run_asm(code, pid=self.pid)
~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "E:\code\call\func.py", line 73, in run_asm
_addr = _wp.virtual_alloc(len(code.get_code()))
File "E:\code\call.venv\Lib\site-packages\windows\winobject\process.py", line 1116, in virtual_alloc
return winproxy.VirtualAllocEx(self.handle, lpAddress=addr, dwSize=size, flAllocationType=type, flProtect=prot)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\code\call.venv\Lib\site-packages\windows\winproxy\apis\kernel32.py", line 277, in VirtualAllocEx
return VirtualAllocEx.ctypes_function(hProcess, lpAddress, dwSize, flAllocationType, flProtect)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\code\call.venv\Lib\site-packages\windows\winproxy\apiproxy.py", line 99, in perform_call
return self._cprototyped(*args)
~~~~~~~~~~~~~~~~~^^^^^^^
File "E:\code\call.venv\Lib\site-packages\windows\winproxy\error.py", line 49, in fail_on_zero
raise WinproxyError(func_name)
windows.winproxy.error.WinproxyError: VirtualAllocEx: [WinError 6] Invalid handle

Activity

  1. hakril commented on Nov 2, 2026

    @hakril
    Owner

    Hi,

    Thank you for the issue.
    From what I understand, it seems the error is in the default handle value you are trying to pass to the WinProcess constructor.
    The default (non-set) value for handle should be None and not 0 (https://github.com/hakril/PythonForWindows/blob/master/windows/winobject/process.py#L1061).

    Passing 0 will make WinProces use this handle for VirtualAllocEx which will results in Invalid handle.

    Example:

    >>> windows.WinProcess(pid=windows.current_process.pid).virtual_alloc(0x1000)
    2323953614848
    >>> windows.WinProcess(pid=windows.current_process.pid, handle=None).virtual_alloc(0x1000)
    2324023869440
    >>> windows.WinProcess(pid=windows.current_process.pid, handle=0).virtual_alloc(0x1000)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "c:\users\cleme\documents\projets\pythonforwindows\windows\winobject\process.py", line 1116, in virtual_alloc
        return winproxy.VirtualAllocEx(self.handle, lpAddress=addr, dwSize=size, flAllocationType=type, flProtect=prot)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "c:\users\cleme\documents\projets\pythonforwindows\windows\winproxy\apis\kernel32.py", line 277, in VirtualAllocEx
        return VirtualAllocEx.ctypes_function(hProcess, lpAddress, dwSize, flAllocationType, flProtect)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "c:\users\cleme\documents\projets\pythonforwindows\windows\winproxy\apiproxy.py", line 99, in perform_call
        return self._cprototyped(*args)
               ^^^^^^^^^^^^^^^^^^^^^^^^
      File "c:\users\cleme\documents\projets\pythonforwindows\windows\winproxy\error.py", line 49, in fail_on_zero
        raise WinproxyError(func_name)
    windows.winproxy.error.WinproxyError: VirtualAllocEx: [WinError 6] The handle is invalid.
    

    Hope this help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions