python能否通过句柄得到外部程序的路径?

发布于 2022-09-11 23:41:36 字数 92 浏览 26 评论 0

c++通过函数
GetWindowThreadProcessId CreateToolhelp32Snapshot 可以查到exe程序的路径,python如何查到?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信仰 2022-09-18 23:41:36

def _win32_check_running_pid(self, pid):

    class PROCESSENTRY32(ctypes.Structure):
        _fields_ = [("dwSize", ctypes.c_ulong),
                    ("cntUsage", ctypes.c_ulong),
                    ("th32ProcessID", ctypes.c_ulong),
                    ("th32DefaultHeapID", ctypes.c_ulong),
                    ("th32ModuleID", ctypes.c_ulong),
                    ("cntThreads", ctypes.c_ulong),
                    ("th32ParentProcessID", ctypes.c_ulong),
                    ("pcPriClassBase", ctypes.c_ulong),
                    ("dwFlags", ctypes.c_ulong),
                    ("szExeFile", ctypes.c_char * 260)]# win64,这行代码如何改?
    CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot
    Process32First = ctypes.windll.kernel32.Process32First
    Process32Next = ctypes.windll.kernel32.Process32Next
    CloseHandle = ctypes.windll.kernel32.CloseHandle
    TH32CS_SNAPPROCESS = 0x00000002  # win32 magic number
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    pe32 = PROCESSENTRY32()
    pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)
    result = False
    if not Process32First(hProcessSnap, ctypes.byref(pe32)):
        _log.debug("Failed getting first process.")
        CloseHandle(hProcessSnap)
        return result
    while True:
        if pe32.th32ProcessID == pid:
            result = True
            break
        if not Process32Next(hProcessSnap, ctypes.byref(pe32)):
            break
    CloseHandle(hProcessSnap)
    return result
 网上找到一段代码,返回是bytes,转换总是乱码,怀疑是32位程序转换在win64出错
遮了一弯 2022-09-18 23:41:36
#遍历process获取程序名
def win32_get_path(pid):

    class PROCESSENTRY32(ctypes.Structure):
        _fields_ = [("dwSize", ctypes.c_ulong),
                    ("cntUsage", ctypes.c_ulong),
                    ("th32ProcessID", ctypes.c_ulong),
                    ("th32DefaultHeapID", ctypes.c_void_p),
                    ("th32ModuleID", ctypes.c_ulong),
                    ("cntThreads", ctypes.c_ulong),
                    ("th32ParentProcessID", ctypes.c_ulong),
                    ("pcPriClassBase", ctypes.c_ulong),
                    ("dwFlags", ctypes.c_ulong),
                    ("szExeFile", ctypes.c_char * 260)]

    kernel32 = ctypes.windll.kernel32
    hProcessSnap = kernel32.CreateToolhelp32Snapshot(0x00000002, 0)
    pe32 = PROCESSENTRY32()
    pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)
    ret = kernel32.Process32First(hProcessSnap, ctypes.byref(pe32))
    while ret:
        if pe32.th32ProcessID == pid:
            kernel32.CloseHandle(hProcessSnap)
            #如果要获取全路径 还要遍历module 
            return pe32.szExeFile.decode("gbk")

        ret = kernel32.Process32Next(hProcessSnap, ctypes.byref(pe32))

    kernel32.CloseHandle(hProcessSnap)

    ERROR_NO_MORE_FILES = 18
    err = kernel32.GetLastError()
    if err is not ERROR_NO_MORE_FILES:
        print("error %d" % err)

    return None
 
 #通过psapi获取全路径
def win32_get_path2(pid):

    def invalid_handle(handle):
        return handle == 0 or handle == -1

    kernel32 = ctypes.windll.kernel32
    psapi = ctypes.windll.psapi
    h_Process = kernel32.OpenProcess(0x0400, 0, pid)
    if invalid_handle(h_Process):
        print("error %d" % kernel32.GetLastError())
        return None
    path = (ctypes.c_char * 260)()
    psapi.GetModuleFileNameExA(h_Process, 0, path, 260)
    kernel32.CloseHandle(h_Process)
    return path.value.decode("gbk")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文