Beispiel #1
0
    def __call__(self, hwnd, keystrokeString, useAlternateMethod=False):
        needGetFocus = False
        sendToFront = False
        if hwnd is None:
            sendToFront = True
            hwnd = GetForegroundWindow()
            needGetFocus = True

        dwProcessId = DWORD()
        threadID = GetWindowThreadProcessId(hwnd, byref(dwProcessId))
        processID = dwProcessId.value
        ourThreadID = GetCurrentThreadId()

        # If not, attach our thread's 'input' to the foreground thread's
        if threadID != ourThreadID:
            AttachThreadInput(threadID, ourThreadID, True)

        if needGetFocus:
            hwnd = GetFocus()
        if not sendToFront:
            if GetGUIThreadInfo(0, byref(self.guiTreadInfo)):
                sendToFront = (self.guiTreadInfo.hwndFocus == hwnd)
            else:
                sendToFront = False
        if not hwnd:
            hwnd = None
        self.procHandle = OpenProcess(PROCESS_QUERY_INFORMATION, 0, processID)
        #self.WaitForInputProcessed()
        oldKeyboardState = PBYTE256()
        GetKeyboardState(byref(oldKeyboardState))

        keyData = ParseText(keystrokeString)
        if sendToFront and not useAlternateMethod:
            self.SendRawCodes1(keyData)
        else:
            self.SendRawCodes2(keyData, hwnd)

        SetKeyboardState(byref(oldKeyboardState))
        self.WaitForInputProcessed()
        if threadID != ourThreadID:
            AttachThreadInput(threadID, ourThreadID, False)
        if self.procHandle:
            CloseHandle(self.procHandle)
def GetWindowPid(hwnd):
    dwProcessId = DWORD()
    GetWindowThreadProcessId(hwnd, byref(dwProcessId))
    return dwProcessId.value
Beispiel #3
0
    def __call__(
        self,
        pathname='',
        arguments='',
        winState=0,
        waitForCompletion=False,
        priority=2,
        workingDir="",
        triggerEvent=False,
        disableWOW64=False,
        additionalSuffix="",
        disableParsingPathname=False,
        disableParsingArguments=False,
        disableParsingAdditionalSuffix=False,
        runAsAdmin=False,
    ):
        if eg.config.refreshEnv:
            eg.Environment.Refresh()
        returnValue = None
        pathname = expandvars(pathname)
        arguments = expandvars(arguments)
        workingDir = expandvars(workingDir)
        if not disableParsingPathname:
            pathname = eg.ParseString(pathname)
        if not disableParsingArguments:
            arguments = eg.ParseString(arguments)
        if not disableParsingAdditionalSuffix:
            additionalSuffix = eg.ParseString(additionalSuffix)
        if not workingDir:
            workingDir = dirname(abspath(pathname))
        processInformation = self.processInformation = SHELLEXECUTEINFO()
        processInformation.cbSize = sizeof(processInformation)
        processInformation.hwnd = 0
        processInformation.lpFile = pathname
        processInformation.lpParameters = arguments
        processInformation.lpDirectory = workingDir
        processInformation.nShow = WINSTATE_FLAGS[winState]
        processInformation.hInstApp = 0
        processInformation.fMask = SEE_MASK_NOCLOSEPROCESS
        if runAsAdmin:
            processInformation.lpVerb = "runas"
        disableWOW64 = disableWOW64 and IsWin64()
        if disableWOW64:
            prevVal = Wow64DisableWow64FsRedirection()
        activeThread = GetWindowThreadProcessId(GetForegroundWindow(), None)
        currentThread = GetCurrentThreadId()
        attached = AttachThreadInput(currentThread, activeThread, True)

        if not windll.shell32.ShellExecuteExW(byref(processInformation)):
            raise self.Exception(FormatError())

        if attached:
            AttachThreadInput(currentThread, activeThread, False)
        if disableWOW64:
            Wow64RevertWow64FsRedirection(prevVal)
        if priority != 2:
            try:
                SetPriorityClass(processInformation.hProcess,
                                 PRIORITY_FLAGS[priority])
                priorityClass = GetPriorityClass(processInformation.hProcess)
                if priorityClass != PRIORITY_FLAGS[priority]:
                    raise
            except:
                pid = windll.kernel32.GetProcessId(processInformation.hProcess)
                pi = SHELLEXECUTEINFO()
                pi.cbSize = sizeof(pi)
                pi.lpFile = r"C:\Windows\System32\wbem\wmic.exe"
                pi.lpParameters = (
                    "process where processid=%d CALL setpriority %d" %
                    (pid, PRIORITY_FLAGS[priority]))
                pi.lpVerb = "runas"
                if not windll.shell32.ShellExecuteExW(byref(pi)):
                    eg.PrintError(self.text.priorityIssue)
        suffix = "%s.%s" % (self.text.eventSuffix, splitext(
            split(pathname)[1])[0])
        if additionalSuffix != "":
            suffix = suffix + "." + additionalSuffix
        prefix = self.plugin.name.replace(' ', '')
        if waitForCompletion:
            WaitForSingleObject(processInformation.hProcess, INFINITE)
            exitCode = DWORD()
            if not GetExitCodeProcess(processInformation.hProcess,
                                      byref(exitCode)):
                raise self.Exception(FormatError())
            returnValue = exitCode.value
            if triggerEvent:
                eg.TriggerEvent(suffix, prefix=prefix)
            CloseHandle(processInformation.hProcess)
            return returnValue
        elif triggerEvent:
            te = self.TriggerEvent(processInformation, suffix, prefix)
            te.start()
        else:
            CloseHandle(processInformation.hProcess)