delphi 下如何关闭进程 执行 winexec('arj32 -a 源地址 目的地址’,SW_HIDE)时,产生ARJ32进程,请问如何在delphi 下关闭 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.用CreateToolhelp32Snapshot或EnumProcesses枚举系统进程,得到arj32进程的handle,然后用TerminateProcess终止进程2.最简的方法,不用winexec而改用CreateProcess调用arj32,这样可直接得到arj32进程的handle,然后用TerminateProcess终止进程 楼下的抄了两个例子 :-)------------------------------------------------------------------------------------// uses Windows, SysUtilsprocedure ProgramRunWait(const CommandLine,DefaultDirectory: string;Wait: boolean);var StartUpInfo: TStartUpInfo; ProcInfo: Process_Information; Dir, Msg: PChar; ErrNo: integer; E: Exception;begin FillChar(StartUpInfo, SizeOf(StartUpInfo), 0); StartUpInfo.cb := SizeOf(StartUpInfo); if DefaultDirectory <> '' then Dir := PChar(DefaultDirectory) else Dir := nil; if CreateProcess(nil, PChar(CommandLine), nil, nil, False, 0, nil, Dir, StartUpInfo, ProcInfo) then begin try if Wait then WaitForSingleObject(ProcInfo.hProcess, INFINITE); finally CloseHandle(ProcInfo.hThread); CloseHandle(ProcInfo.hProcess); end; end else begin ErrNo := GetLastError; Msg := AllocMem(4096); try FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrNo, 0, Msg, 4096, nil); E := Exception.Create('Create Process Error #' + IntToStr(ErrNo) + ': ' + string(Msg)); finally FreeMem(Msg); end; raise E; end;end;-----------------------------------------------------------------------------var x : STARTUPINFO; y : PROCESS_INFORMATION; j : Longbool; z : DWORD; b : array[1..100] of char; s : string;begin x.cb := sizeof(x); x.lpReserved := nil ; //required x.lpDesktop := nil ; //required x.lpTitle := nil ; //required x.dwFlags := STARTF_USESHOWWINDOW ; x.wShowWindow := SW_SHOW; x.cbReserved2 := 0 ; // required x.lpReserved2 := nil; // lpCommandLine finds the application in the current path j := CreateProcess(nil, 'notepad.exe', nil, nil, false, CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS, nil, 'c:', x, y); if j=false then begin z := GetLastError() ; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,z,0,@b, 99, nil); s := b; ShowMessage(s); end;end; // With lpApplicationName, the full path must be specified j := CreateProcess('c:\windows\notepad.exe', '', nil, nil, false, CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS, nil, 'c:\', x, y);--http://www.agui.googlepages.commailto: agui.cn(a)gmail.com 简单问题直接Baidu,Google啦 下面是我从网上找到的方法,很好用,目前已解决谢谢大家procedure Tdmpublic.KillProc(procname: string);//程序名为变量const PROCESS_TERMINATE=$0001;var ExeFileName: String; ContinueLoop: BOOL; FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32;begin ExeFileName := procname; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := Sizeof(FProcessEntry32); ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32); while integer(ContinueLoop) <> 0 do begin if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0); ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32); end;end; 散分,顺带问个事 深圳的哥们都进来啊。。。。有点事情想问一下 如何能截到当前播放的视频的图片? 如何控制文本框输入的字数 使用playsound播放wav的问题? fastreport问题,在线等,高分!!!!!!!!!!! treeview问题! InstallShield Expross for Delphi7 的问题,再简单不过了 问题感觉不容易搞定,高手们进来看看。 一个简单问题!但困扰我很久! 文件正在有其他软件写入,无法读取,如何解决? quickreport 放置了一个QRDBText但是为什么只能出来一条记录?
2.最简的方法,不用winexec而改用CreateProcess调用arj32,这样可直接得到arj32进程的handle
,然后用TerminateProcess终止进程
------------------------------------------------------------------------------------
// uses Windows, SysUtils
procedure ProgramRunWait(const CommandLine,DefaultDirectory: string;Wait: boolean);
var
StartUpInfo: TStartUpInfo;
ProcInfo: Process_Information;
Dir, Msg: PChar;
ErrNo: integer;
E: Exception;
begin
FillChar(StartUpInfo, SizeOf(StartUpInfo), 0);
StartUpInfo.cb := SizeOf(StartUpInfo);
if DefaultDirectory <> '' then
Dir := PChar(DefaultDirectory)
else
Dir := nil;
if CreateProcess(nil,
PChar(CommandLine),
nil,
nil,
False,
0,
nil,
Dir,
StartUpInfo,
ProcInfo) then
begin
try
if Wait then
WaitForSingleObject(ProcInfo.hProcess,
INFINITE);
finally
CloseHandle(ProcInfo.hThread);
CloseHandle(ProcInfo.hProcess);
end;
end
else
begin
ErrNo := GetLastError;
Msg := AllocMem(4096);
try
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
nil,
ErrNo,
0,
Msg,
4096,
nil);
E := Exception.Create('Create Process Error #'
+ IntToStr(ErrNo)
+ ': '
+ string(Msg));
finally
FreeMem(Msg);
end;
raise E;
end;
end;-----------------------------------------------------------------------------
var
x : STARTUPINFO;
y : PROCESS_INFORMATION;
j : Longbool;
z : DWORD;
b : array[1..100] of char;
s : string;
begin
x.cb := sizeof(x);
x.lpReserved := nil ; //required
x.lpDesktop := nil ; //required
x.lpTitle := nil ; //required
x.dwFlags := STARTF_USESHOWWINDOW ;
x.wShowWindow := SW_SHOW;
x.cbReserved2 := 0 ; // required
x.lpReserved2 := nil;
// lpCommandLine finds the application in the current path
j := CreateProcess(nil, 'notepad.exe', nil, nil,
false, CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS, nil, 'c:', x, y); if j=false then begin
z := GetLastError() ;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,z,0,@b, 99, nil);
s := b;
ShowMessage(s);
end;
end; // With lpApplicationName, the full path must be specified
j := CreateProcess('c:\windows\notepad.exe', '', nil, nil,
false, CREATE_NEW_CONSOLE + NORMAL_PRIORITY_CLASS, nil, 'c:\', x, y);--
http://www.agui.googlepages.com
mailto: agui.cn(a)gmail.com
直接Baidu,Google啦
下面是我从网上找到的方法,很好用,目前已解决谢谢大家
procedure Tdmpublic.KillProc(procname: string);//程序名为变量
const
PROCESS_TERMINATE=$0001;
var
ExeFileName: String;
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
ExeFileName := procname;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0),
FProcessEntry32.th32ProcessID), 0);
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;