4
I have two functions that create a file bat
and runs it, but I’m just not getting to create the process (CreateProcess
returns False
) and I can’t identify the error.
I use Windows 7, 64 bit. I must change some parameter in the function WinExecAndWait32
? How to debug the CreateProcess
?
Note: The same function worked in XE5.
This is the function that creates the file bat
, executes, and then deletes:
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:
**EDITION:**After the help of the colleagues she was 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;
Instead of simply returning -1, call the Raiselastoserror function
– EProgrammerNotFound
You are letting leak the process Handles and the thread you created as well, you should clean them:
CloseHandle(ProcessInfo.hProcess);``CloseHandle(ProcessInfo.hThread);
– EProgrammerNotFound
Thanks for the tips!
– Artur_Indio