救命啊!怎么会有这种问题?!(100分)

  • 主题发起人 主题发起人 zbird
  • 开始时间 开始时间
Z

zbird

Unregistered / Unconfirmed
GUEST, unregistred user!
一个关于列举进程的程序<br>我只是简单的改了一下代码怎么就不能用了?<br>已经满久没解决了,我已经开受不了 了。<br>//------------------------------------------------<br>//原程序(正确) &nbsp;//我只贴出部分重要的,相信大家应当看得懂<br>//------------------------------------------------<br>TYPE &nbsp; TProcessInfo = CLASS(TObject)<br>&nbsp; &nbsp; &nbsp; &nbsp;PRIVATE<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fID &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fName &nbsp; &nbsp;: String;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fFullPath: String;<br>&nbsp; &nbsp; &nbsp; &nbsp;PUBLIC<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CONSTRUCTOR Create(ProcessID : DWORD);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PROPERTY ID: DWORD READ FID;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PROPERTY Name: STRING READ FName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PROPERTY FullPath: STRING READ FFullPath;<br>&nbsp; &nbsp; &nbsp; &nbsp;END;<br><br>PROCEDURE TProcesses.ListFill;<br>CONST MaxProcesses = 1024;<br>VAR IDBuffer: ARRAY[0..MaxProcesses] OF DWORD;<br>&nbsp; &nbsp; Res &nbsp; &nbsp; : BOOL;<br>&nbsp; &nbsp; cbNeeded: DWORD;<br>&nbsp; &nbsp; i &nbsp; &nbsp; &nbsp; : INTEGER;<br>BEGIN<br>&nbsp; Res := EnumProcesses(IDBuffer,SizeOf(IDBuffer),cbNeeded);//我出错的地方[:(]<br>&nbsp; IF (Res) THEN BEGIN<br>&nbsp; &nbsp; FOR i:=0 TO (cbNeeded DIV SizeOf(DWORD))-1 DO BEGIN<br>&nbsp; &nbsp; &nbsp; FProcessList.Add(TProcessInfo.Create(IDBuffer));<br>&nbsp; &nbsp; END;<br>&nbsp; END ELSE BEGIN<br>&nbsp; &nbsp; { GetLastError - error handling }<br>&nbsp; END;<br>END{ ProcessListFill };<br><br>CONSTRUCTOR TProcessInfo.Create(ProcessID: DWORD);<br>VAR hProcess: THandle;<br>&nbsp; &nbsp; hMod &nbsp; &nbsp;: hModule;<br>&nbsp; &nbsp; cbNeeded: DWORD;<br>&nbsp; &nbsp; szProcessName: ARRAY[0..1024] OF CHAR;<br>BEGIN<br>&nbsp; INHERITED Create;<br>&nbsp; FID := ProcessID;<br><br>&nbsp; hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FALSE, processID );<br><br>&nbsp; { Get the process name. }<br><br>&nbsp; szProcessName := 'unknown';<br>&nbsp; IF (hProcess&lt;&gt;0) THEN BEGIN<br>&nbsp; &nbsp; IF(EnumProcessModules(hProcess,hMod,sizeof(hMod),cbNeeded)) THEN BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp;GetModuleBaseName(hProcess,hMod,szProcessName,sizeof(szProcessName));<br>&nbsp; &nbsp; &nbsp; &nbsp;fName := StrPas(szProcessName);<br>&nbsp; &nbsp; &nbsp; &nbsp;GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));<br>&nbsp; &nbsp; &nbsp; &nbsp;fFullPath := StrPas(szProcessName);<br>&nbsp; &nbsp; END;<br>&nbsp; END;<br>&nbsp; CloseHandle(hProcess);<br><br>END{ Create };<br><br>//----------------------------------------------<br>//我该为 //因为测试用,所以代码比较乱(包含系些无用变量定义)<br>//-----------------------------------------------<br>PROCEDURE tform1.ListFill;<br>CONST <br>&nbsp; MaxProcesses = 1024;<br>VAR<br>&nbsp; IDBuffer: ARRAY[0..MaxProcesses] OF DWORD;<br>&nbsp; Res &nbsp; &nbsp; : BOOL;<br>&nbsp; cbNeeded: DWORD;<br>&nbsp; i &nbsp; &nbsp; &nbsp; : INTEGER;<br>&nbsp; hProcess: THandle;<br>&nbsp; ProcessID: DWORD;<br>&nbsp; szProcessName: ARRAY[0..1024] OF CHAR;<br>BEGIN<br>&nbsp; Res := EnumProcesses(IDBuffer,SizeOf(IDBuffer),cbNeeded);//提示类型错误(大意为:数组和PDword不能通用)[:(]<br>&nbsp; IF (Res) THEN BEGIN<br>&nbsp; &nbsp; FOR i:=0 TO (cbNeeded DIV SizeOf(DWORD))-1 DO<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; processID:=IDBuffer;<br>&nbsp; &nbsp; &nbsp; hProcess := OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FALSE, processID );<br>&nbsp; &nbsp; &nbsp; szProcessName := 'unknown';<br><br>&nbsp; &nbsp; &nbsp; IF(EnumProcessModules(hProcess,hMod,sizeof(hMod),cbNeeded)) THEN<br>&nbsp; &nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; GetModuleFileNameEx(hProcess,hMod,szProcessName,sizeof(szProcessName));<br>&nbsp; &nbsp; &nbsp; &nbsp; fFullPath := StrPas(szProcessName);<br>&nbsp; &nbsp; &nbsp; END;<br>&nbsp; &nbsp; END;<br>&nbsp; END<br>&nbsp; ELSE<br>&nbsp; BEGIN<br>&nbsp; &nbsp; { GetLastError - error handling }<br>&nbsp; END;<br>END{ ProcessListFill };<br>帮帮忙吧,谢谢<br>[:)][:D][8D][:(][:(!][^][?][?][^][:(!][:(][8D][:D][:)]
 
<br>EnumProcesses(@IDBuffer,SizeOf(IDBuffer),cbNeeded);//我出错的地方<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
 
原程序和你改的一样啊!!(出错的那一行)。<br>要改成<br>EnumProcesses(@IDBuffer,SizeOf(IDBuffer),cbNeeded);<br>你是不是用NT家族的windows(NT/K/XP)?<br>列举进程我比较喜欢用Native API(本机API)。有兴趣的话我可以把一段源代码放在这儿。<br>其实PSAPI.dll要调用Native API取得那些信息。<br>(Borland 的winapi group 已经改名为 native api [:D][:D][:D] )<br>//----------------<br>CIONO1
 
不好意思。我发帖字的时候没看到jsxjd写的东西。[:)]。
 
多人接受答案了。
 
后退
顶部