天灵灵,地灵灵,但愿这个一定灵!!!python调用win32api,启动应用程序窗口

这个是逼到没办法,C#那一套,一点基本没有。

还好,网上找到例程,可以指定帐户启动进程,但愿可以摆脱WIN SERVICE启动产生的SESSION 0 隔离问题。

因为这个问题,以SERVICE启动的进程的程序,没办法打开正常的程序窗口。

from ctypes import *
from ctypes.wintypes import *

INVALID_HANDLE_VALUE = -1
CREATE_UNICODE_ENVIRONMENT = 0x00000400

CData = Array.__base__
LPBYTE = POINTER(BYTE)

class PROCESS_INFORMATION(Structure):
    '''http://msdn.microsoft.com/en-us/library/ms684873'''
    _fields_ = [
        ('hProcess',    HANDLE),
        ('hThread',     HANDLE),
        ('dwProcessId', DWORD),
        ('dwThreadId',  DWORD),
    ]
LPPROCESS_INFORMATION = POINTER(PROCESS_INFORMATION)


class STARTUPINFOW(Structure):
    'http://msdn.microsoft.com/en-us/library/ms686331'
    _fields_ = [
        ('cb',              DWORD),
        ('lpReserved',      LPWSTR),
        ('lpDesktop',       LPWSTR),
        ('lpTitle',         LPWSTR),
        ('dwX',             DWORD),
        ('dwY',             DWORD),
        ('dwXSize',         DWORD),
        ('dwYSize',         DWORD),
        ('dwXCountChars',   DWORD),
        ('dwYCountChars',   DWORD),
        ('dwFillAttribute', DWORD),
        ('dwFlags',         DWORD),
        ('wShowWindow',     WORD),
        ('cbReserved2',     WORD),
        ('lpReserved2',     LPBYTE),
        ('hStdInput',       HANDLE),
        ('hStdOutput',      HANDLE),
        ('hStdError',       HANDLE),
    ]
LPSTARTUPINFOW = POINTER(STARTUPINFOW)


# http://msdn.microsoft.com/en-us/library/ms682431
windll.advapi32.CreateProcessWithLogonW.restype = BOOL
windll.advapi32.CreateProcessWithLogonW.argtypes = [
    LPCWSTR, # lpUsername
    LPCWSTR, # lpDomain
    LPCWSTR, # lpPassword
    DWORD,   # dwLogonFlags
    LPCWSTR, # lpApplicationName
    LPWSTR,  # lpCommandLine (inout)
    DWORD,   # dwCreationFlags
    LPCWSTR, # lpEnvironment  (force Unicode)
    LPCWSTR, # lpCurrentDirectory
    LPSTARTUPINFOW, # lpStartupInfo
    LPPROCESS_INFORMATION, # lpProcessInfo (out)
]
def CreateProcessWithLogonW(
    lpUsername=None, 
    lpDomain=None, 
    lpPassword=None, 
    dwLogonFlags=0, 
    lpApplicationName=None, 
    lpCommandLine=None,
    dwCreationFlags=0, 
    lpEnvironment=None, 
    lpCurrentDirectory=None,
    startupInfo=None
):
    if (lpCommandLine is not None and 
        not isinstance(lpCommandLine, CData)
    ):
        lpCommandLine = create_unicode_buffer(lpCommandLine)
    dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT               
    if startupInfo is None:
        startupInfo = STARTUPINFOW(sizeof(STARTUPINFOW))
    processInformation = PROCESS_INFORMATION(
        INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE)
    success = windll.advapi32.CreateProcessWithLogonW(
        lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName,
        lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, 
        byref(startupInfo), byref(processInformation))
    if not success:
        raise WinError()
    return processInformation


if __name__ == '__main__':
    pi = CreateProcessWithLogonW(
        "wahaha", ".", "wahaha", 0, None, 
        "C:\\Windows\\notepad.exe")
    print(pi.dwProcessId)

天灵灵,地灵灵,但愿这个一定灵!!!python调用win32api,启动应用程序窗口

上一篇:《Python游戏编程快速上手》一1.2 启动IDLE


下一篇:双网口相机平台搭建C++(大恒水星)