delphi – GetModuleFileNameEx()没有得到各种系统进程的路径

我创建了这个函数来获取各种网络进程的路径,比如svchost,Firefox等.这是代码:
function GetProcessPath(var pId:Integer):String;
var
    Handle: THandle;

begin
    Result := '';
    try
        Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,pID);
        if Handle <> 0 then
        begin
            try
               SetLength(Result,MAX_PATH);
               if GetModuleFileNameEx(Handle,PChar(Result),MAX_PATH) > 0 then
                   SetLength(Result,StrLen(PChar(Result)))
               else
                  Result := '';
            finally
                CloseHandle(Handle);
        end;
    end;

    except
       on E:Exception do
           ShowMessage(E.ClassName + ':' + E.Message);
    end;
end;

我的问题是我没有得到所有进程的路径.它适用于获取Firefox和其他类似用户级进程的路径.但对于像alg,Svchost这样的进程,我无法通过这种方法获得路径.我的猜测是我必须使用一些不同的API.我该如何解决这个问题?

我使用的是Windows XP,32位.

解决方法

您需要设置调试权限.以下是它的完成方式:
function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;
var
  hToken: THandle;
  TokenPriv: TOKEN_PRIVILEGES;
  PrevTokenPriv: TOKEN_PRIVILEGES;
  ReturnLength: Cardinal;
begin
  Result := True;

  // Only for Windows NT/2000/XP and later.
  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then Exit;

  Result := False;

  // Obtain the processes token
  if OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken) then
  begin
    try
      // Get the locally unique identifier (LUID) .
      if LookupPrivilegeValue(nil,PChar(sPrivilege),TokenPriv.Privileges[0].Luid) then
      begin
        TokenPriv.PrivilegeCount := 1; // One privilege to set

        case bEnabled of
          True: TokenPriv.Privileges[0].Attributes  := SE_PRIVILEGE_ENABLED;
          False: TokenPriv.Privileges[0].Attributes := 0;
        end;

        ReturnLength := 0; // Replaces a var parameter
        PrevTokenPriv := TokenPriv;

        // Enable or disable the privilege

        AdjustTokenPrivileges(hToken,TokenPriv,SizeOf(PrevTokenPriv),PrevTokenPriv,ReturnLength);
      end;
    finally
      CloseHandle(hToken);
    end;
  end;
end;

NtSetPrivilege('SeDebugPrivilege',TRUE); // Call this on form create

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...