Trying to open a .exe with the CreateProcess windows api method with python

34 views Asked by At

I have to open a .exe in the background, with my script when i try to execute it works but does not open in backgroud it just opens normally, but if i run the same script with the notepad.exe for example it works fine in te backgroud.

someone can help me? am i doing something wrong acessing the windows api method? this is my script:

class STARTUPINFO(ctypes.Structure):
        _fields_ = [
            ("cb", wintypes.DWORD),
            ("lpReserved", wintypes.LPWSTR),
            ("lpDesktop", wintypes.LPWSTR),
            ("lpTitle", wintypes.LPWSTR),
            ("dwX", wintypes.DWORD),
            ("dwY", wintypes.DWORD),
            ("dwXSize", wintypes.DWORD),
            ("dwYSize", wintypes.DWORD),
            ("dwXCountChars", wintypes.DWORD),
            ("dwYCountChars", wintypes.DWORD),
            ("dwFillAttribute", wintypes.DWORD),
            ("dwFlags", wintypes.DWORD),
            ("wShowWindow", wintypes.WORD),
            ("cbReserved2", wintypes.WORD),
            ("lpReserved2", wintypes.LPBYTE),
            ("hStdInput", wintypes.HANDLE),
            ("hStdOutput", wintypes.HANDLE),
            ("hStdError", wintypes.HANDLE),
        ]
    class PROCESS_INFORMATION(ctypes.Structure):
        _fields_ = [
            ("hProcess", wintypes.HANDLE),
            ("hThread", wintypes.HANDLE),
            ("dwProcessId", wintypes.DWORD),
            ("dwThreadId", wintypes.DWORD),
        ]
    caminho_programa = "C:\SCI\VSUC\SUCESSOR.EXE"
    kernelbase  = ctypes.WinDLL('kernelbase.dll')
    CreateProcess = kernelbase.CreateProcessW

    CreateProcess.restype = wintypes.BOOL 

    application_name = None  # Caminho do executável
    command_line = caminho_programa  # Linha de comando para o executável (None se não houver)
    process_attributes = None  # Atributos do processo (None para usar os padrões)
    thread_attributes = None  # Atributos da thread (None para usar os padrões)
    inherit_handles = False  # Indica se os manipuladores devem ser herdados pelo novo processo
    creation_flags = win32con.CREATE_NO_WINDOW  # Sinalizadores de criação do processo (CREATE_NO_WINDOW)
    environment = None  # Variáveis de ambiente para o novo processo (None para usar as do processo pai)
    current_directory = None  # Diretório de trabalho para o novo processo (None para usar o diretório do processo pai)

    startupinfo = STARTUPINFO()
    startupinfo.cb = ctypes.sizeof(startupinfo)
    startupinfo.dwFlags = win32process.STARTF_USESHOWWINDOW
    startupinfo.wShowWindow = win32con.SW_HIDE

    process_information = PROCESS_INFORMATION()

    success = CreateProcess(
        application_name, command_line, process_attributes, thread_attributes,
        inherit_handles, creation_flags, environment, current_directory,
        ctypes.byref(startupinfo), ctypes.byref(process_information)
    )

I tried open a .exe with the windows api i background but it opens in the screen

0

There are 0 answers