Delphi – How to Detect Build Errors When Launching Delphi Compiler (dcc32.exe) from the Command Line

delphidelphi-7

Our Delphi project can be built directly from the IDE, of course. But launching from the command line allows the process of building multiple projects to be automated. We are using:

ShellExecute(0,
    'open',
    PChar('[PATH-TO-DELPHI]\Bin\DCC32.EXE'),
    PChar('-B [PATH-TO-PROJECT]\MyProject.dpr'),
    PChar('[PATH-TO-PROJECT]'),
    SW_HIDE);

Normally this works great when there are no code or build errors, and ShellExecute returns a value of "42". However, if there are build errors, the return value is still "42".

You could build the project in the IDE to find the error(s). But is it possible to detect compilation errors from the command line, or to check some file or log to see if the build was successful or not?

Best Answer

Use CreateProcess() (or ShellExecuteEx()) instead of ShellExecute(). That way, you can get a HANDLE to the process, then use WaitForSingleObject() to wait for the process to end, and finally get its exit code with GetExitCodeProcess().

var
  cmd: string;
  si: TStartupInfo;
  pi: TProcessInformation;
  exitCode: DWORD;

cmd := '"[PATH-TO-DELPHI]\Bin\DCC32.EXE" -B "[PATH-TO-PROJECT]\MyProject.dpr"';
UniqueString(cmd);

si := Default(TStartupInfo);
si.cb := sizeof(si);
si.dwFlags := STARTF_USESHOWWINDOW;
si.wShowWindow := SW_HIDE;

if not CreateProcess(nil, PChar(cmd), nil, nil, False, 0, nil, PChar('[PATH-TO-PROJECT]'), si, pi) then
  RaiseLastOSError;

CloseHandle(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, exitCode);
CloseHandle(pi.hProcess);

// use exit Code as needed...
Related Question