Win 查看系统进程中是否已存在某个程序

来源:互联网 发布:更新软件下载 编辑:程序博客网 时间:2024/06/07 21:32
/*2017-12-12  创建人:Ruo_Xiao代码已测试,可以直接使用。需加头文件 #include "tlhelp32.h"*/

实例代码:

/*功能:查看系统进程列表中是否有指定的进程名字;输入:进程名字的宽字符;输出:返回该进程的数量。*/DWORD GetProcessidFromName(LPCTSTR strWProcessName){    PROCESSENTRY32 pe;    pe.dwSize = sizeof(PROCESSENTRY32);    DWORD id = 0;    int iProcessId = 0;    //给系统内所有的进程拍个快照    //返回快照对象的句柄    HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);    //遍历进程快照,轮流显示每个进程的信息    if(!::Process32First(hSnapshot,&pe))      {        return 0;    }    char strProcessName[1000];    sprintf_s(strProcessName,sizeof(char)*1000,"%ws", strWProcessName );    while(true)    {        char strTest[1000];        if(!::Process32Next(hSnapshot,&pe))        {            break;        }        sprintf_s(strTest,sizeof(char)*1000, "%ws", pe.szExeFile);        if(strcmp(strTest,strProcessName)==0)        {            id = pe.th32ProcessID;            iProcessId++;        }    }    ::CloseHandle(hSnapshot);    return iProcessId;}

一、CreateToolhelp32Snapshot
MSDN:Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes.
翻译:对指定的进程拍照,包括此进程生成的堆、模块和线程。
函数原型如下:

HANDLE WINAPI CreateToolhelp32Snapshot(  _In_ DWORD dwFlags,  _In_ DWORD th32ProcessID);Members:dwFlags      这里选择“TH32CS_SNAPPROCESS”,代表快照对象中包含系统中所有的进程。th32ProcessID      指定将要快照的进程的ID。如果该参数为0表示快照当前进程。该参数只有在设置了TH32CS_SNAPHEAPLIST或者TH32CS_SNAPMODULE后才有效,在其他情况下该参数被忽略,所有的进程都会被快照。返回值    调用成功,返回快照对象的句柄,调用失败,返回INVALID_HANDLE_VALUE 。    删除快照对象则使用CloseHandle()。

CreateToolhelp32Snapshot MSDN

二、ROCESSENTRY32
MSDN:Describes an entry from a list of the processes residing in the system address space when a snapshot was taken.
翻译:用来存储快照中常驻系统地址空间的进程的信息。

结构体如下:

typedef struct tagPROCESSENTRY32 {  DWORD     dwSize;  DWORD     cntUsage;  DWORD     th32ProcessID;  ULONG_PTR th32DefaultHeapID;  DWORD     th32ModuleID;  DWORD     cntThreads;  DWORD     th32ParentProcessID;  LONG      pcPriClassBase;  DWORD     dwFlags;  TCHAR     szExeFile[MAX_PATH];} PROCESSENTRY32, *PPROCESSENTRY32;Members:dwSize    The size of the structure, in bytes. Before calling the Process32First function, set this member to sizeof(PROCESSENTRY32). If you do not initialize dwSize, Process32First fails.    翻译:代表此结构体大小(byte),调用Process32First函数之前,先调用sizeof(PROCESSENTRY32),否则Process32First运行失败。cntUsageThis member is no longer used and is always set to zero.    翻译:此成员不再使用,总是设置为0。th32ProcessIDThe process identifier.    翻译:进程的ID号。th32DefaultHeapIDThis member is no longer used and is always set to zero.    翻译:此成员不再使用,总是设置为0。th32ModuleIDThis member is no longer used and is always set to zero.    翻译:此成员不再使用,总是设置为0。cntThreadsThe number of execution threads started by the process.    翻译:被此进程开始的执行线程的ID。th32ParentProcessIDThe identifier of the process that created this process (its parent process).    翻译:父进程的ID。pcPriClassBaseThe base priority of any threads created by this process.    翻译:被此进程创建的任何线程的基础优先级。dwFlagsThis member is no longer used, and is always set to zero.    翻译:此成员不再使用,总是设置为0。szExeFileThe name of the executable file for the process. To retrieve the full path to the executable file, call the Module32First function and check the szExePath member of the MODULEENTRY32 structure that is returned. However, if the calling process is a 32-bit process, you must call the QueryFullProcessImageName function to retrieve the full path of the executable file for a 64-bit process.    翻译:此进程的exe文件的名字。可以通过调用Module32First来检索包含完整路径的进程的名字,存储在返回的MODULEENTRY32结构体中的szExePath 成员中。若检索的进程是32位,则必须调用QueryFullProcessImageName来检索64位的进程的可执行文件的完整路径。

ROCESSENTRY32 MSDN

三、Process32First
MSDN:Retrieves information about the first process encountered in a system snapshot.
翻译:获取指定的系统快照对象中的第一个进程的信息。

函数原型如下:

BOOL WINAPI Process32First(  _In_    HANDLE           hSnapshot,  _Inout_ LPPROCESSENTRY32 lppe);Members:hSnapshot    CreateToolHelp32Snapshot返回的快照对象句柄。lppe    一个指向PROCESSENTRY32的指针。该结构体包含了进程的信息,例如:进程的可执行文件的名字、进程的的ID和进程的父进程的ID。返回值    如果进程列表的第一个进程信息已经copy到LPPROCESSENTRY32中 ,则返回TRUE,否则返回FALSE。如果没有进程存在或者快照对象不包含进程信息,GetLastError返回ERROR_NO_MORE_FILES(18L)。 注意    调用之前必须设置PROCESSENTRY32的大小(字节单位)。    如若继续获取同一个快照对象的其他进程信息,需用Process32Next函数。

Process32First MSDN

四、Process32Next
MSDN:Retrieves information about the next process recorded in a system snapshot.
翻译:搜索记录在系统快照对象中下一个进程的信息。

函数原型如下:

BOOL WINAPI Process32Next(  _In_  HANDLE           hSnapshot,  _Out_ LPPROCESSENTRY32 lppe);Members:hSnapshot    CreateToolHelp32Snapshot返回的快照对象句柄。lppe    一个指向PROCESSENTRY32的指针。返回值    如果进程列表的下一个进程信息已经copy到LPPROCESSENTRY32中 ,则返回TRUE,否则返回FALSE。如果没有进程存在或者快照对象不包含进程信息,GetLastError返回ERROR_NO_MORE_FILES(18L)。注意    若获取快照对象中第一个进程的信息,用Process32First函数。

Process32Next MSDN

原创粉丝点击