I have two functions that create a file bat
and execute it, but I just can not create the process ( CreateProcess
returns False
) and I can not identify the error.
I use Windows 7, 64-bit. Should I change some parameter in the WinExecAndWait32
function? How to debug CreateProcess
?
Note: The same function worked on XE5.
This is the function that creates the file bat
, executes it, and then deletes it:
function AddDeleteServico(comando: string): boolean;
var
txt: TextFile;
dir: string;
ret: boolean;
begin
ret:=False;
try
dir:=ExtractFilePath(Application.ExeName);
AssignFile(txt, dir + 'meu.bat');
Rewrite(txt);
Write(txt,comando);
CloseFile(txt);
if WinExecAndWait32(dir + 'meu.bat',dir,SW_ShowNormal) = 0 then
ret:=True;
DeleteFile(PChar(dir + 'meu.bat'));
finally
AddDeleteServico:=ret;
end;
end;
This is the function that creates the process:
** EDIT: ** After the help of the colleagues she functioned like this:
function WinExecAndWait32(ExeName: string; CmdLineArgs: string = '';
ShowWindow: boolean = True; WaitForFinish: boolean = False): integer;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
begin
FillChar(StartInfo,SizeOf(TStartupInfo),#0);
FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
StartInfo.cb := SizeOf(TStartupInfo);
if not(ShowWindow) then begin
StartInfo.dwFlags := STARTF_USESHOWWINDOW;
StartInfo.wShowWindow := SW_HIDE;
end;
CreateProcess(nil,PChar(ExeName + ' ' + CmdLineArgs),nil,nil,False,
CREATE_NEW_PROCESS_GROUP + NORMAL_PRIORITY_CLASS,nil,nil,StartInfo,
ProcInfo);
Result := ProcInfo.dwProcessId;
if WaitForFinish then begin
WaitForSingleObject(ProcInfo.hProcess,Infinite);
end;
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;