[转]Python之调用windows api操作

分享者:KernelA
邮件:wangaibo168@163.com
主页:http://www.du52.com
1.调用windows api列出当前所有进程
2.调用Windows API锁定计算机
3.调用windows api函数ShellExecute
4.调用windows api GetVersionEx获取当前系统版本

 

(一)Python调用windows api列出当前所有进程

import ctypes;
__metaclass__ = type;

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_long),
        ("dwFlags",ctypes.c_ulong),
        ("szExeFile",ctypes.c_char*260)
    ]

kernel32 = ctypes.windll.LoadLibrary("kernel32.dll");
pHandle = kernel32.CreateToolhelp32Snapshot(0x2,0x0);

import sys;

if pHandle==-1:
    sys.exit();

proc = PROCESSENTRY32();
proc.dwSize = ctypes.sizeof(proc);

while kernel32.Process32Next(pHandle,ctypes.byref(proc)):
    print("ProcessName : %s - ProcessID : %d"%(ctypes.string_at(proc.szExeFile),proc.th32ProcessID));

kernel32.CloseHandle(pHandle);

(二) 调用Windows API锁定计算机

import ctypes;
dll = ctypes.WinDLL('user32.dll');
dll.LockWorkStation();

(三)调用windows api函数ShellExecute

import ctypes;

handler = None;
operator = "open";
fpath = r"C:\Python27\python.exe";
param = None;
dirpath = None;
ncmd = 1;

shell32 = ctypes.windll.LoadLibrary("shell32.dll");
shell32.ShellExecuteA(handler,operator,fpath,param,dirpath,ncmd);

 

(四)调用windows api GetVersionEx获取当前系统版本

"通过调用Window API判断当前系统版本"
# 演示通过ctypes调用windows api函数.
# 作者已经知道python32能够实现相同功能
# 语句末尾加分号,纯属个人习惯
# 仅作部分版本判断,更详细的版本判断推荐系统OSVERSIONINFOEX结构体

import ctypes;

class OSINFO(ctypes.Structure):
    _fields_ = [
        ("dwOSVersionInfoSize",ctypes.c_long),
        ("dwMajorVersion",ctypes.c_long),
        ("dwMinorVersion",ctypes.c_long),
        ("dwBuildNumber",ctypes.c_long),
        ("dwPlatformId",ctypes.c_long),
        ("szCSDVersion",ctypes.c_char*128)
    ];

def GetSystemVersionString():
    kernel32 = ctypes.windll.LoadLibrary("kernel32.dll");
    os = OSINFO();
    os.dwOSVersionInfoSize = ctypes.sizeof(os);
    if kernel32.GetVersionExA(ctypes.byref(os))==0:
        return "Null Version";
    if os.dwPlatformId==1: #windows 95/98/me
        if os.dwMajorVersion==4 and os.dwMinorVersion==0:
            verStr = "windows 95";
        elif os.dwMajorVersion==4 and os.dwMinorVersion==10:
            verStr = "windows 98";
        elif os.dwMajorVersion==4 and os.dwMinorVersion==90:
            verStr = "windows me";
        else:
            verStr = "unknown version";
    elif os.dwPlatformId==2: #windows vista/server 2008/server 2003/xp/2000/nt
        if os.dwMajorVersion==4 and os.dwMinorVersion==0:
            verStr = "windows nt 4.0";
        elif os.dwMajorVersion==5 and os.dwMinorVersion==0:
            verStr = "windows 2000";
        elif os.dwMajorVersion==5 and os.dwMinorVersion==1:
            verStr = "windows xp";
        elif os.dwMajorVersion==5 and os.dwMinorVersion==2:
            verStr = "windows 2003";
        elif os.dwMajorVersion==6 and os.dwMinorVersion==0:
            verStr = "windows vista"; # or 2008
        elif os.dwMajorVersion>=0:
            verStr = "windows 7";
        else:
            verStr = "unknown version";
    else:
        return "unknown Version";
    return verStr+" build"+str(os.dwBuildNumber)+" "+ctypes.string_at(os.szCSDVersion);

if __name__ == "__main__":
    print(GetSystemVersionString());

 

 

 

 

 

 

posted @ 2013-07-14 10:55  开心星  阅读(1790)  评论(0)    收藏  举报