下面的代码://CreateProcess,创建进程
procedure TForm1.Button3Click(Sender: TObject);
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(ProcessInfo, sizeof(TProcessInformation),0);
FillChar(StartupInfo,Sizeof(TStartupInfo),0);
StartupInfo.cb := Sizeof(TStartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
If CreateProcess('test.exe',
nil,
nil,
nil,
False,
NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInfo) then
begin
CloseHandle(ProcessInfo.hProcess);
// 问题:
这里面的ProcessInfo.hProcess是什么啊?进程句柄还是进程ID ,如果是句柄如何得到ID啊?如何通过这个得到该程序窗口的句柄??? 高手来!!!end
end;......
procedure TForm1.Button3Click(Sender: TObject);
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(ProcessInfo, sizeof(TProcessInformation),0);
FillChar(StartupInfo,Sizeof(TStartupInfo),0);
StartupInfo.cb := Sizeof(TStartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
If CreateProcess('test.exe',
nil,
nil,
nil,
False,
NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInfo) then
begin
CloseHandle(ProcessInfo.hProcess);
// 问题:
这里面的ProcessInfo.hProcess是什么啊?进程句柄还是进程ID ,如果是句柄如何得到ID啊?如何通过这个得到该程序窗口的句柄??? 高手来!!!end
end;......
解决方案 »
- 哪位达人帮我转换下代码------------------
- 如何让dbgrid的某一列的值都除以当前dbgrid的行数,再在此列显示出来
- 帮忙,在线等待!
- 需要一个书店管理系统C#编写基于C/S的
- bitmap image is not valid (图形转换)
- 求救:HP打印机实时打印(单行打印)的问题,呼唤高手!!!
- help me !!
- 请朋友们给一份有关于DELPHI中操作注册表的资料
- 有人知道这是怎么回事吗?
- 如何登陆interbase de local server?
- 某窗体的dbgrid.refresh如何实现
- 如何 捕捉 程序或所有控件 发出的所有异常 信息 , 并 屏闭这些信息 换成 自己的信息 ??????????? 急!!!!!!!!!!
ProcessInfo.dwProcessId是进程ID
可以用EnumWindows枚举出窗口句柄
hDos: Cardinal;
begin
hDos := FindWindow(nil, 'HdosHald');
//showmessage( IntToStr(hDos));
if hDos <> 0 then
begin
sendMessage(hDos, WM_CLOSE, 0, 0);
end;
end;
unit UMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, RpMemo, ComCtrls,math;const intMax = 255; //设置最大控制进程数type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
StatusBar1: TStatusBar;
memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
function funEstablishProcess(strFileName: string): Pointer;
//为程序创建子进程
function funInitFileStatus(): boolean; //初始化进程的相关信息
function funGetCurrentDir(const str: string): string;
//从字符串取得该程序路径
function funAppIsRunning(const str: string): boolean;
//function funTextOut(str: Pointer): Pointer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;type intFileNameStruct = record //保存进程相关信息
intOrder: integer;
strFileName: string;
strExeName: string;
end;var
Form1: TForm1;
piProcInfoGPS: array[1..intMax] of PROCESS_INFORMATION;
intFileStatus: array[1..intMax] of intFileNameStruct;
intNowCount: integer; //目前列表中进程数量
implementation{$R *.dfm}{ TForm1 }function TForm1.funEstablishProcess(strFileName: string): Pointer;
var
siStartupInfo: STARTUPINFO;
saProcess, saThread: SECURITY_ATTRIBUTES;
fSuccess: boolean;
i: integer;
begin
fSuccess := false;
i := 1; ZeroMemory(@siStartupInfo, sizeof(siStartupInfo));
siStartupInfo.cb := sizeof(siStartupInfo);
saProcess.nLength := sizeof(saProcess);
saProcess.lpSecurityDescriptor := PChar(nil);
saProcess.bInheritHandle := true;
saThread.nLength := sizeof(saThread);
saThread.lpSecurityDescriptor := PChar(nil);
saThread.bInheritHandle := true;
while (i <= intNowCount) do
begin
if (intFileStatus[i].strFileName = strFileName) then
break;
i := i + 1;
end;
if (i > 1) then
Sleep(4000);
fSuccess := CreateProcess(PChar(nil), pChar(strFileName), @saProcess,
@saThread, false,
CREATE_DEFAULT_ERROR_MODE, Pchar(nil),
Pchar(funGetCurrentDir(intFileStatus[i].strFileName)),
siStartupInfo, piProcInfoGPS[i]);
if (not fSuccess) then
begin
intFileStatus[i].intOrder := 0;
Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' fail.');
end
else
begin
intFileStatus[i].intOrder := 1;
Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' success.');
end;
Result := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
i := 0; for i := 1 to intNowCount do
if (funAppIsRunning(intFileStatus[i].strExeName) = False) then
funEstablishProcess(intFileStatus[i].strFileName);
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
dwExitCode: DWORD;
fprocessExit: boolean;
i: integer;
begin
Application.ProcessMessages;
dwExitCode := 0;
fprocessExit := false;
i := 0; for i := 1 to intNowCount do
begin
fprocessExit := GetExitCodeProcess(piProcInfoGPS[i].hProcess, dwExitCode);
if (fprocessExit and (dwExitCode <> STILL_ACTIVE)) then
begin
Memo1.Lines.Add(#13 + #10 + DateTimeToStr(Now) + ' : '
+ '进程终止');
CloseHandle(piProcInfoGPS[i].hThread);
CloseHandle(piProcInfoGPS[i].hProcess);
intFileStatus[i].intOrder := 0;
funEstablishProcess(intFileStatus[i].strFileName);
end;
end; statusbar1.Panels[0].Text := '进程管理';
statusbar1.Panels[1].Text := '进程数量:' + inttostr(intNowCount) + '个';
statusbar1.Panels[2].Text := datetimetostr(now);
end;
function TForm1.funInitFileStatus: boolean;
var i: integer;
strFileNameList: TstringList;
begin
i := 0; strFileNameList := TstringList.Create();
strFileNameList.LoadFromFile('FileNameList.txt');
for i := 1 to strFileNameList.Count do
begin
intFileStatus[i].intOrder := 0;
intFileStatus[i].strFileName := strFileNameList.Strings[i - 1];
intFileStatus[i].strExeName :=
copy(intFileStatus[i].strFileName,
length(funGetCurrentDir(intFileStatus[i].strFileName)) + 2,
//2 is len('\')+1
length(intFileStatus[i].strFileName) -
length(funGetCurrentDir(intFileStatus[i].strFileName)) - 5);
//5 is len('.exe')+1
end; intNowCount := strFileNameList.Count;
strFileNameList.Free;
Result := true;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
funInitFileStatus;
end;function TForm1.funGetCurrentDir(const str: string): string;
var i: integer;
begin
for i := length(str) - 1 downto 0 do
begin
if str[i] = '\' then
break;
end;
Result := copy(str, 0, i - 1);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Close;
end;function TForm1.funAppIsRunning(const str: string): boolean;
var intFileHwnd: hWnd;
begin
Result := False; intFileHwnd := windows.FindWindow(nil, Pchar(str));
if intFileHwnd <> 0 then
Result := true;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Timer1.Enabled := not Timer1.Enabled;
case integer(Timer1.Enabled) of
0:
begin
Button3.Caption := '继续(&S)';
end;
1:
begin
Button3.Caption := '停止(&S)';
end;
end;
end;function MyThreadFunc(P: string): Longint; stdcall;
var
i: longint;
DC: HDC;
S: string;
begin
DC := GetDC(Form1.Handle);
for i := 0 to 500000 do begin
S := Inttostr(i);
Textout(DC, 10, 10, Pchar(s), length(s));
end;
ReleaseDC(Form1.Handle, DC);
result := 1;
end;
procedure TForm1.Button4Click(Sender: TObject);
var hThread: THandle;
dwThreadID: dWord;
begin
hThread := CreateThread(nil, 0, @MyThreadFunc, nil, 0, dwThreadID);
if hThread = 0 then
messagebox(handle, '线程创建失败', '报警提示', mb_ok);
end;