码迷,腾讯三分彩 www.csbesbj.com
腾讯三分彩 > 系统相关 > 详细

中彩网|91233aa.com:遍历进程,获取当前进程下进程的路径.(获得全路径)

时间:2019-04-22 12:06:08      阅读:77      评论:0      收藏:0      [点我收藏+]

腾讯三分彩 www.csbesbj.com 标签:process   ret   argv   info   dev   驱动   logical   image   char s   

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#include <string>
#include <tchar.h>
#include <Psapi.h>
#pragma www.csbesbj.comment (lib,"Psapi.lib")
 
BOOL DosPathToNtPath(LPTSTR pszDosPath, LPTSTR pszNtPath)
{
    TCHAR            szDriveStr[500];
    TCHAR            szDrive[3];
    TCHAR            szDevName[100];
    INT                cchDevName;
    INT                i;
     
    //检查参数
    if(!pszDosPath || !pszNtPath )
        return FALSE;
 
    //获取本地磁盘字符串
    if(GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr))
    {
        for(i = 0; szDriveStr[i]; i += 4)
        {
            if(!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\")))
                continue;
 
            szDrive[0] = szDriveStr[i];
            szDrive[1] = szDriveStr[i + 1];
            szDrive[2] = '\0';
            if(!QueryDosDevice(szDrive, szDevName, 100))//查询 Dos 设备名
                return FALSE;
 
            cchDevName = lstrlen(szDevName);
            if(_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中
            {
                lstrcpy(pszNtPath, szDrive);//复制驱动器
                lstrcat(pszNtPath, pszDosPath + cchDevName);//复制路径
 
                return TRUE;
            }           
        }
    }
 
    lstrcpy(pszNtPath, pszDosPath);
     
    return FALSE;
}
//获取进程完整路径
BOOL GetProcessFullPath(DWORD dwPID, TCHAR pszFullPath[MAX_PATH])
{
    TCHAR        szImagePath[MAX_PATH];
    HANDLE        hProcess;
    if(!pszFullPath)
        return FALSE;
 
    pszFullPath[0] = '\0';
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID);
    if(!hProcess)
        return FALSE;
 
    if(!GetProcessImageFileName(hProcess, szImagePath, MAX_PATH))
    {
        CloseHandle(hProcess);
        return FALSE;
    }
 
    if(!DosPathToNtPath(szImagePath, pszFullPath))
    {
        CloseHandle(hProcess);
        return FALSE;
    }
 
    CloseHandle(hProcess);
 
    _tprintf(_T("%d,%s \r\n"),dwPID,pszFullPath);
    return TRUE;
}
int main(int argc, char* argv[])
{
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (INVALID_HANDLE_VALUE == hSnapshot)
    {
        return NULL;
    }
    PROCESSENTRY32 pe ={0};
    pe.dwSize = sizeof(PROCESSENTRY32); 
 
    BOOL fOk;
    for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
    {
        TCHAR szProcessName[MAX_PATH] = {0};
        GetProcessFullPath(pe.th32ProcessID, szProcessName);
    }
    return 0;
}

遍历进程,获取当前进程下进程的路径.(获得全路径)

标签:process   ret   argv   info   dev   驱动   logical   image   char s   

原文地址:https://www.cnblogs.com/iBinary/p/10749239.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
? 2014 腾讯三分彩 www.csbesbj.com 版权所有 京ICP备13008772号-2
迷上了代码!