美文网首页
获取系统的所有进程ID和路径

获取系统的所有进程ID和路径

作者: f675b1a02698 | 来源:发表于2017-09-14 16:14 被阅读0次

    效果图

    源码

    #define _WIN32_WINNT 0x0601

    #include

    #include

    #include

    int main(int argc, PCHAR argv[]){

    DWORD size = 1000 * sizeof(DWORD);

    DWORD lpidProcess = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

    PDWORD pid = lpidProcess;

    //DWORD lpidProcess[4000] = { 0 };

    DWORD num;

    if (!lpidProcess){

    printf("分配内存失败\n");

    }

    int a = 0;

    if (EnumProcesses(lpidProcess,size, &num)){

    printf("当前系统共有 %d 个进程:\n", num/4);

    for (a = 0; a< num;a+=4){

    printf("pid= %d", *(DWORD*)(lpidProcess + a));

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, *(DWORD*)(lpidProcess + a));

    if (hProcess){

    DWORD length = MAX_PATH;

    TCHAR ProcessName[MAX_PATH] = TEXT("<未知>");

    QueryFullProcessImageName(hProcess, 0, ProcessName, &length);

    printf(" lpExeName = %s\n", ProcessName);

    } else{

    printf("\n", GetLastError());

    }

    }

    } else{

    printf(" EnumProcess Error %d\n", GetLastError());

    }

    VirtualFree(lpidProcess, size, MEM_DECOMMIT);

    getchar();

    return 0;

    }

    相关文章

      网友评论

          本文标题:获取系统的所有进程ID和路径

          本文链接:https://www.haomeiwen.com/subject/dzwqsxtx.html