winexec()的使用(50分)

  • 主题发起人 主题发起人 jockyi
  • 开始时间 开始时间
J

jockyi

Unregistered / Unconfirmed
GUEST, unregistred user!
讓程式在運行完winexec()后再運行后面的語句!
 
程序不会等待的,运行完winexec他就继续运行,你试试看[:)]
winexec('calc.exe', SW_SHOW);
showmessage('sdf');
 
winexec('d:/jdk/bin/java.exe',SW_SHOWNOACTIVATE)
关键是后面的那个参数的问题,自己看看这个不和你意思的话自己看看help应该可以的
 
给两个例子,自己试试哪个好用吧。我没试。
一、
Function WinExecExW(cmd,workdir:pchar;visiable:integer):DWORD;
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
FillChar(StartupInfo,SizeOf(StartupInfo),#0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=visiable;
if not CreateProcess(nil,cmd,nil,nil,false,Create_new_console or Normal_priority_class,nil,nil,StartupInfo,ProcessInfo) then
result:=0
else
begin
waitforsingleobject(processinfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
二、
var
sCommandLine: string;
bCreateProcess: boolean;
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
sCommandLine := 'ARJ.EXE /?';
bCreateProcess := CreateProcessA(nil, PChar(sCommandLine),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
lpStartupInfo, lpProcessInformation);
if bCreateProcess then
WaitForSingleObject(lpProcessInformation.hProcess, INFINITE);
end;
//////////////////////////////////////////////
var
pWindowsList: pointer;
hActiveWindow: HWnd;
hExeHandle: THandle;
begin
pWindowsList := DisableTaskWindows(0);
hActiveWindow := GetActiveWindow;
try
hExeHandle := WinExec('arj.exe /?',SW_SHOWNORMAL);
while GetModuleUsage(hExeHandle) <> 0 do
Application.ProcessMessages;
finally
EnableTaskWindows(pWindowsList);
SetActiveWindow(hActiveWindow);
end;
end;
/////////////////////////////////////////////////////////////////////
{the first example, based on WaitForSingleObject}
function WinExecAndWait32_v1(FileName: string; Visibility: integer):
Cardinal; {integer}
var
zAppName: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
true, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name, PChar}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
then Result := INFINITE {-1} else
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle(ProcessInfo.hProcess); { to prevent memory leaks }
CloseHandle(ProcessInfo.hThread);
end;
end;

{the second example, based on PeekMessage(msg,,,,PM_REMOVE)->DispatchMessage(msg)}
function WinExecAndWait32_v2(FileName: string; Visibility: integer):
Cardinal; {integer}
var
zAppName: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
Msg: TagMsg;
ExitCode: cardinal;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW; // STARTF_FORCEONFEEDBACK;
StartupInfo.wShowWindow := Visibility;
if CreateProcess(nil, { and once more: }
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
NORMAL_PRIORITY_CLASS, { creation flags }
nil, { pointer to the new environment block }
nil, { current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS_INF }
begin
repeat
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
begin
if (Msg.message = WM_QUIT) then
Result := Msg.wParam;
TranslateMessage(msg);
Dispatchmessage(msg);
Sleep(100); {your choice}
end;
GetExitCodeProcess(ProcessInfo.hProcess, ExitCode);
until (ExitCode <> STILL_ACTIVE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
Result := 0;
end
else begin
Result := GetLastError;
end;
end;
 
Brave兄的例子够酷!
 
多人接受答案了。
 
后退
顶部