介绍一下进程控制的api(200分)

  • 主题发起人 主题发起人 ioi2000
  • 开始时间 开始时间
I

ioi2000

Unregistered / Unconfirmed
GUEST, unregistred user!
介绍一下进程控制的api<br>(例如:启动、关闭、获得内存和cpu使用情况)<br>再多问一下如何取得当前CPU使用情况
 
createprocess:创建一个新进程<br>exitproscess:终止一个进程<br>getprocessworkingsetsize:了解一个应用程序在运行过程中实际向它交付了多大容量的内存<br>terminateprocess:结束一个进程<br>GlobalMemoryStatus:获取内存信息<br>至于cpu占用情况,从注册表中可得到HKEY_DYN_DATA/PerfStats/StartStat/KERNEL/CPUUsage""
 
CreateToolhelp32Snapshot()<br>Process32First()<br>Process32Next()<br>CloseHandle<br>CreateProcess<br>CreateRemoteThread<br>DuplicateHandle<br>GetCurrentProcess<br>GetCurrentProcessId<br>GetExitCodeProcess<br>GetPriorityClass<br>ReadProcessMemory<br>SetPriorityClass<br>TerminateProcess<br>VirtualProtectEx<br>WriteProcessMemory <br>Thread32First<br>Thread32Next<br>Module32First<br>Module32Next<br>具体的用法请参考msdn或windows sdk,引用时要uses TlHelp32
 
那么getprocessworkingsetsize中的最大和最小内存使用是什么意思阿,介绍一下拉
 
就是限制进程的最大和最小内存使用。<br>看帮助说明。<br><br>The GetProcessWorkingSetSize function obtains the minimum and maximum working set sizes of a specified process. <br><br>The "working set" of a process is the set of memory pages currently visible to the process in physical RAM memory. These pages are resident and available for an application to use without triggering a page fault. The size of a process' working set is specified in bytes. The minimum and maximum working set sizes affect the virtual memory paging behavior of a process.<br><br>BOOL GetProcessWorkingSetSize(<br><br>&nbsp; &nbsp; HANDLE hProcess, // open handle to the process of interest<br>&nbsp; &nbsp; LPDWORD lpMinimumWorkingSetSize, // points to variable to receive minimum working set size<br>&nbsp; &nbsp; LPDWORD lpMaximumWorkingSetSize // points to variable to receive maximum working set size<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>hProcess<br><br>An open handle to the process whose working set sizes will be obtained. The handle must have PROCESS_QUERY_INFORMATION access rights. For more information, see Process Objects. <br><br>lpMinimumWorkingSetSize<br><br>Points to a variable that receives the minimum working set size of the specified process. The virtual memory manager attempts to keep at least this much memory resident in the process whenever the process is active.<br><br>lpMaximumWorkingSetSize<br><br>Points to a variable that receives the maximum working set size of the specified process. The virtual memory manager attempts to keep no more than this much memory resident in the process whenever the process is active when memory is in short supply.<br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is nonzero.<br>If the function fails, the return value is zero. To get extended error information, call GetLastError.
 
结束进程与终止进程有什么不同,<br>如果我自己的程序启动另外一个程序,那个程序自己结束了,我还需不需要结束或终止这个进程
 
前面别人说的没错。我只是想加一点。。。<br>如果你用的是WINNT family (winnt、win2k、winxp),<br>而且想得到进程的信息(像 Win2k 的任务管理 那样)就可以用Native API(本机API)。<br>有一本书关于Native API 名为 Windows NT/2000 本机API 可以看一下<br>http://bingle.sitenova.net/teach/NTNativeAPI.cab<br>用ReadProcessMemory也可以得到进程信息。。。<br>告诉我你用什么windows。。<br>
 
too too easy……<br>winnt,xp,2000一个函数嫁异行代码搞定,<br>温酒吧的要复杂一点!!<br>可惜不想公开源码,有什么好交换的没有??(有点技术价值的)
 
以上是对进程物理内存使用情况而言,cpu使用情况则到处都有源码可以抄袭!<br>前面两个就不说了……
 
好多函数啊,API函数一共多少条?
 
最后nt,window2000,xp下面物理内存的使用绝对不是用的ReadProcessMemory<br>getprocessworkingsetsize两个函数……<br>真的是很简单的,不过我找资料也找了很久哟:)<br>温酒吧的则是自己搞定的,稍微复杂一点
 
如果对 NativeAPI感兴趣的话我可以把源代码写在这儿。<br>我不求交换什么。也不要给我分。。。<br>Knowledge is free for all... CIONO1<br>
 
关闭所有正在运行的程序:<br>procedure TForm1.ButtonKillAllClick(Sender: TObject);<br>var<br>&nbsp; &nbsp; pTask : PTaskEntry;<br>&nbsp; &nbsp; Task : Bool;<br>&nbsp; &nbsp; ThisTask: THANDLE;<br>begin<br>&nbsp; &nbsp; GetMem (pTask, SizeOf (TTaskEntry));<br>&nbsp; &nbsp; pTask^.dwSize := SizeOf (TTaskEntry);<br>&nbsp; &nbsp; Task := TaskFirst (pTask);<br>&nbsp; &nbsp; while Task do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if pTask^.hInst = hInstance then<br>&nbsp; &nbsp; &nbsp; &nbsp; ThisTask := pTask^.hTask<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; TerminateApp (pTask^.hTask, NO_UAE_BOX);<br>&nbsp; &nbsp; &nbsp; &nbsp; Task := TaskNext (pTask);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; TerminateApp (ThisTask, NO_UAE_BOX);<br>end;<br>重新启动Windows,不是重新启动机器!包含Thunk技术。<br><br>感谢http://www.experts-exchange.com/Q.10115419, 以下代码RestartWindows可以实现:<br>{$StackFrames On} &nbsp;//QT_Thunk needs a stack frame, Thunking call to 16-bit USER.EXE. The ThunkTrash argument allocates space on the stack for QT_Thunk<br>function LoadLibrary16(LibraryName: PChar): THandle; stdcall; external kernel32 index 35;<br>procedure FreeLibrary16(HInstance: THandle); stdcall; external kernel32 index 36;<br>function GetProcAddress16(Hinstance: THandle; ProcName: PChar): Pointer; stdcall; external kernel32 index 37;<br>procedure QT_Thunk; cdecl; external kernel32 name 'QT_Thunk';<br>var<br>&nbsp; hInst16: THandle;<br>&nbsp; GFSR: Pointer;<br>function RestartWindows: WordBool;<br>var<br>&nbsp; ThunkTrash: array[0..$20] of Word;<br>&nbsp; dw: DWord;<br>&nbsp; w: Word;<br>begin<br>&nbsp; if Win32Platform = VER_PLATFORM_WIN32_NT then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; ThunkTrash[0] := hInst16; //Prevent the optimizer from getting rid of ThunkTrash<br>&nbsp; hInst16 := LoadLibrary16('user.exe');<br>&nbsp; if hInst16 &lt; 32 then<br>&nbsp; &nbsp; raise Exception.Create('Cannot load USER.EXE');<br>&nbsp; FreeLibrary16(hInst16); //Decrement the usage count. This doesn't really free the library, since USER.EXE is always loaded<br>&nbsp; GFSR := GetProcAddress16(hInst16, 'ExitWindows'); //Get the function pointer for the 16-bit function in USER.EXE<br>&nbsp; if GFSR = nil then<br>&nbsp; &nbsp; raise Exception.Create('Cannot get address of ExitWindows');<br>&nbsp; dw := EW_RestartWindows;<br>&nbsp; w &nbsp;:= 0;<br>&nbsp; asm &nbsp;//Thunk down to USER.EXE<br>&nbsp; &nbsp; push dw &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { push arguments }<br>&nbsp; &nbsp; push w<br>&nbsp; &nbsp; mov edx, GFSR &nbsp; &nbsp; { load 16-bit procedure pointer }<br>&nbsp; &nbsp; call QT_Thunk &nbsp; &nbsp; { call thunk }<br>&nbsp; &nbsp; mov Result, ax &nbsp; &nbsp;{ save the result }<br>&nbsp; end;<br>end;<br>{$StackFrames Off}<br><br>如何知道进程使用了多少内存<br>// Works only on Windows NT systems (WinNT, Win2000, WinXP) <br>uses psAPI;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; pmc: PPROCESS_MEMORY_COUNTERS;<br>&nbsp; cb: Integer;<br>begin<br>&nbsp; cb := SizeOf(_PROCESS_MEMORY_COUNTERS);<br>&nbsp; GetMem(pmc, cb);<br>&nbsp; pmc^.cb := cb;<br>&nbsp; if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then<br>&nbsp; &nbsp; Label1.Caption := IntToStr(pmc^.WorkingSetSize) + ' Bytes'<br>&nbsp; else<br>&nbsp; &nbsp; Label1.Caption := 'Unable to retrieve memory usage structure';<br><br>&nbsp; FreeMem(pmc);<br>end;<br>几个基本的例子,由此可演化得到许多硬件信息。<br>结果放在Memo1中。<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; systeminfo: SYSTEM_INFO;<br>&nbsp; &nbsp; &nbsp; &nbsp; memory: MEMORYSTATUS;<br>&nbsp; &nbsp; &nbsp; &nbsp; sector,byte,cluster,free: DWORD;<br>&nbsp; &nbsp; &nbsp; &nbsp; freespace,totalspace: longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; CDtype: UINT;<br>&nbsp; &nbsp; &nbsp; &nbsp; name: CHAR;<br>&nbsp; &nbsp; &nbsp; &nbsp; drvname: string;<br>&nbsp; &nbsp; &nbsp; &nbsp; volname,filesysname: PCHAR;<br>&nbsp; &nbsp; &nbsp; &nbsp; sno,maxl,fileflag: DWORD;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Clear();<br>&nbsp; &nbsp; &nbsp; &nbsp; //获得CPU型号<br>&nbsp; &nbsp; &nbsp; &nbsp; GetSystemInfo(systeminfo);<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('您的CPU类型是:' + inttostr(systeminfo.dwProcessorType));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; //获得内存状态<br>&nbsp; &nbsp; &nbsp; &nbsp; memory.dwLength := sizeof(memory); //初始化<br>&nbsp; &nbsp; &nbsp; &nbsp; GlobalMemoryStatus(memory);<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('您的物理内存是(' + inttostr(integer(memory.dwTotalPhys div 1024 div 1024)) + 'MB)。');<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('其中可用内存是(' + inttostr(integer(memory.dwTotalPhys div 1024)) + 'KB)。');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; //获得C盘可用空间<br>&nbsp; &nbsp; &nbsp; &nbsp; GetDiskFreeSpace('C:', LPDWORD(@sector)^, LPDWORD(@byte)^, LPDWORD(@free)^, LPDWORD(@cluster)^); //获得返回参数<br>&nbsp; &nbsp; &nbsp; &nbsp; totalspace := cluster * byte * sector div 1024 div 1024; //计算总容量<br>&nbsp; &nbsp; &nbsp; &nbsp; freespace := free * byte * sector div 1024 div 1024; //计算可用空间<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('C盘总空间(' + inttostr(integer(totalspace)) + 'MB)。');<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('C盘可用空间(' + inttostr(integer(freespace)) + 'MB)。');<br><br>&nbsp; &nbsp; &nbsp; &nbsp; //检测CD-ROM,是否有光盘<br>&nbsp; &nbsp; &nbsp; &nbsp; GetMem(volname, 255);<br>&nbsp; &nbsp; &nbsp; &nbsp; GetMem(filesysname, 100);<br>&nbsp; &nbsp; &nbsp; &nbsp; for name :='C' to 'Z' &nbsp;do//循环检测A~Z<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drvname := name + ':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CDtype := GetDriveType(PCHAR(@drvname[1])); //获得磁盘类型<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (CDtype = DRIVE_CDROM) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add('您的光驱盘符为[' + drvname + ']');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volname^ := Chr(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filesysname^ := Chr(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( not (GetVolumeInformation(PCHAR(@drvname[1]), volname, 250, LPDWORD(@sno), LPDWORD(@maxl)^, LPDWORD(@fileflag)^, filesysname,100))) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add(drvname + '驱中没有发现光盘') //如果返回值为假<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else //如果返回值为真<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add (drvname + '驱中光盘卷标为: [' + String(volname) + ']');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Add (drvname + '驱中光盘序号为: [' + inttostr(sno) + ']');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(volname);<br>&nbsp; &nbsp; &nbsp; &nbsp; FreeMem(filesysname)<br>end;<br><br>
 
小弟菜鸟,关注先。
 
to ioi2000<br>你到底想实现什么功能?<br>&gt;&gt;结束进程与终止进程有什么不同,<br>结束进程最好是从执行该进程的一个线程中调用EXITPROCESS函数,<br>这可以把进程所打开的所有资源一一关闭,如DLL等。结束进程后,GETEXITCODEPROCESS<br>得到进程返回值(是否出错)。<br>关闭进程 TERMINATEPROCESS(。。。)强行关闭进程,进程所关联的DLL等其他系统清除<br>工作不执行(内存泄露)。<br>另外向主窗体发关闭消息也可以,进程采用方法一退出。<br><br>&gt;&gt;如果我自己的程序启动另外一个程序,那个程序自己结束了,我还需不需要结束或终止这个进程<br>不需要<br>
 
手痒,就上来写下述。希望对某些人有帮助。<br>在NT Family如果想枚举正在运行的进程(像win2k,xp的任务管理)就可以用<br>NtQuerySystemInformation(or ZwQuerySystemInformation)此服务在NTDLL.dll<br>(格式在NativeAPI那本书介绍的),要用SystemProcessAndThreadInformation=5 作为要得到系统信息<br>用SystemProcessInformation=2 可以得到处理器的一些信息。还有SystemProcessorTimes =8'<br>还有很多信息。。。 多看我说的那本书。。<br>反汇编有关的软件可以知道他们用的是一样的方法(win2k的任务管理,pview,...)<br>如果要针对一个进程的话可以看那本书的第六章(ZwCreateProcess,ZwOpenProcess,ZwTerminateProcess,<br>ZwQueryInformationProcess,...)<br>
 
to cg1120代码有出严重的小错误:)呵呵汇编部分<br>,怎么没有人贴温酒吧的代码??????……
 
SystemProcessAndThreadInformation=5<br>不能获得进程路径,此外此函数运行起来不稳定,存在内存漏洞<br>唯一优点就是提供的进程信息比较多<br>……<br><br>CreateToolhelp32Snapshot()<br>Process32First()<br>Process32Next()<br>其实间接调用了SystemProcessAndThreadInformation=5<br>而且消除了SystemProcessAndThreadInformation的不稳定因素<br>如果提供信息就比SystemProcessAndThreadInformation=5<br>少了,我个人还是喜欢<br>CreateToolhelp32Snapshot()<br>Process32First()<br>Process32Next()<br>程序的稳定压倒一切
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; pmc: PPROCESS_MEMORY_COUNTERS;<br>&nbsp; cb: Integer;<br>begin<br>&nbsp; cb := SizeOf(_PROCESS_MEMORY_COUNTERS);<br>&nbsp; GetMem(pmc, cb);<br>&nbsp; pmc^.cb := cb;<br>&nbsp; if GetProcessMemoryInfo(GetCurrentProcess(), pmc, cb) then<br>&nbsp; &nbsp; Label1.Caption := IntToStr(pmc^.WorkingSetSize) + ' Bytes'<br>&nbsp; else<br>&nbsp; &nbsp; Label1.Caption := 'Unable to retrieve memory usage structure';<br><br>&nbsp; FreeMem(pmc);<br>end;<br>有多代码多余,和可能引起出错地方
 
后退
顶部