delphi获取进程用户名的问题

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,TLHelp32,Psapi, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

Type
  _TOKEN_USER = Record
    user:SID_AND_ATTRIBUTES;
  end;
  TTokenUser = ^_TOKEN_USER;






{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
TokenHandle : Thandle;
Tkp : TTokenPrivileges;
Zero : DWORD;
SnapShotHandle : Thandle;
Ft : Bool;
PEntry : TProcessEntry32;
ProHandle : Thandle ;
TokenInfo : TTokenUser;
CbTokenInfo : DWORD;
UserName ,DName: String;
CbUserName,CbDName: DWORD;
St :  SID_NAME_USE;

begin
  OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,TokenHandle);//提升权限
  LookUpPrivilegeValue(nil,'SeDebugPrivilege',Tkp.privileges[0].Luid);
  Tkp.PrivilegeCount := 1 ;
  Tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED ;
  AdjustTokenPrivileges(TokenHandle,False,Tkp,sizeof(Tkp),nil,Zero);
  SnapShotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//枚举进程
  PEntry.dwSize := Sizeof(TProcessEntry32);
  Ft := Process32First(SnapShotHandle,PEntry);
  while Ft do
    Begin
    ProHandle := OpenProcess(PROCESS_ALL_ACCESS,False,PEntry.th32ProcessID );
    OpenProcessToken(ProHandle,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY or TOKEN_READ, TokenHandle);//获取进程用户名
    GetTokenInformation(TokenHandle,TokenUser,@TokenInfo,Sizeof(TTokenUser),CbTokenInfo);
    SetLength(UserName,CbUserName);
    SetLength(DName,CbDName);
    LookupAccountSid(nil,TokenInfo.user.Sid ,Pchar(UserName),CbUserName,Pchar(DName),CbDName,St);
    UserName := PChar(UserName);
    Listbox1.Items.add(UserName);
    Ft := Process32Next(SnapShotHandle,PEntry);
    end;

end;

end.

请各位大虾给看看,代码中有什么不对的地方,俺初学编程,怎么也不能编译通过
最后编辑2007-05-18 19:37:19