C++ 中的 GetProcessName

发布于 2024-09-14 08:03:12 字数 1744 浏览 5 评论 0原文

我有一个带有打击细节的功能。

typedef 部分

   typedef DWORD (WINAPI *GETMODULEFILENAMEEX)(HANDLE hProcess, HMODULE hModule, LPTSTR   lpBaseName,DWORD nSize); 

   typedef BOOL (WINAPI *PFNTERMINATEPROCESS)(HANDLE hProcess,UINT uExitCode);

/// GetProcessName 函数

void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize)
{

    HMODULE lib=LoadLibrary(TEXT("Psapi.dll"));
    GetModuleFileNameEx=(GETMODULEFILENAMEEX)GetProcAddress
    (lib,"GetModuleFileNameExW");
    _tcscpy_s(szProcessName, cchSize, TEXT("---"));


    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
    FALSE,PID);

   if (hProcess == NULL) {
      _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      return;
   }

   if (GetModuleFileNameEx(hProcess,(HMODULE)0, szProcessName, cchSize) 
       == 0) {
     if (!GetProcessImageFileName(hProcess, szProcessName, cchSize)) {
         _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      }
   }
   CloseHandle(hProcess);
}

我想在下面的函数中使用此函数

BOOL WINAPI Hook_TerminateProcess(HANDLE hProcess,UINT uExitCode) {
  BOOL nResult=false;
  TCHAR szProcessName[MAX_PATH];


 nResult = ((PFNTERMINATEPROCESS)(PROC) g_TerminateProcess)(hProcess,uExitCode);

 GetProcessName(HandleToULong(hProcess),szProcessName,MAX_PATH); //my question here


    MessageBox(0, szProcessName  ,TEXT("My MessageBox Info"),MB_OK | MB_ICONERROR);

   return(nResult);
}

当我调用函数 GetProcessName 时,这必须返回进程名称,但它???总是。 我直接通过PID调用这个函数,例如GetProcessName(2018,szProcessName,MAX_PATH);。 例如 2018 是一个 pid 并且它可以工作。 我不知道为什么 HandleToULong(hProcess) 不起作用。我的 hProcess 必须是一个句柄 现在肯定要输入我如何解决这个问题?

I have a function with blow detail.

typedef part

   typedef DWORD (WINAPI *GETMODULEFILENAMEEX)(HANDLE hProcess, HMODULE hModule, LPTSTR   lpBaseName,DWORD nSize); 

   typedef BOOL (WINAPI *PFNTERMINATEPROCESS)(HANDLE hProcess,UINT uExitCode);

/// GetProcessName function

void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize)
{

    HMODULE lib=LoadLibrary(TEXT("Psapi.dll"));
    GetModuleFileNameEx=(GETMODULEFILENAMEEX)GetProcAddress
    (lib,"GetModuleFileNameExW");
    _tcscpy_s(szProcessName, cchSize, TEXT("---"));


    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
    FALSE,PID);

   if (hProcess == NULL) {
      _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      return;
   }

   if (GetModuleFileNameEx(hProcess,(HMODULE)0, szProcessName, cchSize) 
       == 0) {
     if (!GetProcessImageFileName(hProcess, szProcessName, cchSize)) {
         _tcscpy_s(szProcessName, cchSize, TEXT("???"));
      }
   }
   CloseHandle(hProcess);
}

I want use this function in below function

BOOL WINAPI Hook_TerminateProcess(HANDLE hProcess,UINT uExitCode) {
  BOOL nResult=false;
  TCHAR szProcessName[MAX_PATH];


 nResult = ((PFNTERMINATEPROCESS)(PROC) g_TerminateProcess)(hProcess,uExitCode);

 GetProcessName(HandleToULong(hProcess),szProcessName,MAX_PATH); //my question here


    MessageBox(0, szProcessName  ,TEXT("My MessageBox Info"),MB_OK | MB_ICONERROR);

   return(nResult);
}

When I call function GetProcessName, this must return process name but it ??? str always.
I call this function directly by PID, for example GetProcessName(2018,szProcessName,MAX_PATH);.
2018 for example is a pid and it work.
I don't know why HandleToULong(hProcess) doesn't work. My hProcess must be a handle
type certainly now how I fix this problem?

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

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

发布评论

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

评论(4

幸福%小乖 2024-09-21 08:03:13

字符名称[MAX_PATH * 2] = "\0", *p;

GetModuleFileName(GetModuleHandle(NULL),名称,MAX_PATH);

p = 名称 + strlen(名称) - 1;

while (isalnum(*p) || ('.' == *p) || ('_' == *p))
p--;

p++;

std::cout << p << std::endl;

char name[MAX_PATH * 2 ] = "\0", *p;

GetModuleFileName(GetModuleHandle(NULL),name,MAX_PATH);

p = name + strlen(name) - 1;

while (isalnum(*p) || ('.' == *p) || ('_' == *p))
p--;

p++;

std::cout << p << std::endl;

も让我眼熟你 2024-09-21 08:03:13

您必须调用 GetProcessId 而不是 HandleToULong。您需要一个进程 ID,而不是转换为无符号长整型的句柄

You must call GetProcessId rather than HandleToULong. You need a process ID, not a handle-converted-to-an-unsigned-long

绿萝 2024-09-21 08:03:13
  1. 如何终止进程然后期望句柄仍然有效?因为如果执行任何清理,所有数据都会丢失(您没有显式复制句柄,因此可能会发生这种情况)
  2. 您的错误似乎源于检索 hProcess 的位置,在这种情况下,您应该检查 GetLastError 以了解其失败的原因
  1. How can you terminate the process then expect the handle to still be valid? cause if any clean up is performed, all data is lost(you don't explicitly copy the handle, so this can happen)
  2. your error seems to stem from where your retrieving hProcess, in which case you should check GetLastError to see why its failing
你丑哭了我 2024-09-21 08:03:13

在 Windows 中,进程 ID 与进程句柄不同。您将在 Hook_TerminateProcess 中获取进程句柄,并将其作为进程 ID 传递到 GetProcessName 中。这永远不会起作用。

您应该重构 GetProcessName 以获取句柄,然后使用获取进程 ID 的重载。进程 ID 重载会通过 OpenProcess 将其转换为句柄,并由 CloseHandle 进行清理。

重构后,您将有两种方法:

void GetProcessName(HANDLE hProcess, PTSTR szProcessName, size_t cchSize);
void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize);

In Windows, a process ID is different from a process handle. You are taking the process handle in Hook_TerminateProcess and passing it into GetProcessName as a process ID. This will never work.

You should refactor GetProcessName to take a handle and then have an overload that takes a process ID. The process ID overload does the OpenProcess work to convert it into a handle and the CloseHandle work to clean it up.

After the refactoring, you'll have two methods:

void GetProcessName(HANDLE hProcess, PTSTR szProcessName, size_t cchSize);
void GetProcessName(DWORD PID, PTSTR szProcessName, size_t cchSize);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文