这类题目正好我以前做过一个,用于封装UPX的功能。
便于每次发布程序时压缩可执行文件。
主要使用CreateProcess函数
例子如下:
procedure TForm1.Button1Click(Sender: TObject);
var
sCommandLine: string;
lpStartupInfo: TStartupInfo;
my_priority
WORD;
lpProcessInformation: TProcessInformation;
my_file:TFileStream;
begin
if OpenDialog1.Execute then
begin
//winexec(pchar('upx -9 -k '+ExtractFileName(OpenDialog1.FileName)),SW_MAXIMIZE);
if not FileExists(ExtractFilePath(ParamStr(0)) + 'Upx.exe') then
begin
with TResourceStream.Create(hInstance, 'UPX_1', 'UPX') do
try
SaveToFile(ExtractFilePath(ParamStr(0)) + 'Upx.exe');
finally
Free;
end;
end;
form1.Visible:=false;
//my_out:=TFileStream.Create(ExtractFilePath(ParamStr(0))+'temp.txt',fmCreate);
sCommandLine:='upx -9 -k '+ExtractFileName(OpenDialog1.FileName);
FillChar(lpStartUpInfo,sizeof(StartUpInfo),#0);
//lpStartupInfo.cb:=sizeof(my_out);
//lpStartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
//lpStartupInfo.wShowWindow:=SW_HIDE;
//lpStartupInfo.hStdOutput:=my_out.Handle;
if CheckBox1.Checked then
begin
my_priority:=HIGH_PRIORITY_CLASS;
my_file:=TFileStream.Create('temp.txt',fmCreate);
//SetStdHandle(STD_OUTPUT_HANDLE,my_file.Handle);
lpStartupInfo.cb:=sizeof(lpStartUpInfo);
lpStartupInfo.dwFlags:=STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
lpStartupInfo.wShowWindow:=SW_HIDE;
lpStartupInfo.hStdOutput:=my_file.Handle;
end
else
my_priority:=0;
if CreateProcess(nil,PChar(sCommandLine),nil,nil,True,my_priority,nil,nil,lpStartUpInfo,lpProcessInformation) then
begin
WaitForSingleObject(lpProcessInformation.hProcess,INFINITE);
deletefile(ExtractFilePath(ParamStr(0)) + 'Upx.exe');
if CheckBox1.Checked then
begin
//借用sCommandLine
my_file.Free;
{SetLength(sCommandLine,64);
GetWindowsDirectory(pchar(sCommandLine),64);
strcat(pchar(sCommandLine),pchar('/Notepad.exe '+'temp.txt'));
WinExec(pchar(sCommandLine),SW_NORMAL);}
Application.CreateForm(TForm2, Form2);
form2.Memo1.Lines.LoadFromFile('temp.txt');
form2.ShowModal;
deletefile('temp.txt');
end;
form1.Visible:=true;
end;
end;
end;