We help IT Professionals succeed at work.

running signtool from a Delphi 2009 Application

davidcase
davidcase asked
on
I need to run the signtool application from a Delphi 2009 application that gathers a list of files in a folder, backs up the files and then signs them. The command line I need to execute is something like this...

signtool sign /fMyCert.pfx /pPassword /t"timestamp URL" "%s\%s"

I then use the format function to replace the %s\%s with the path to the filename to sign.

I have tried the cmdline in a batch file and it works perfectly.

The application runs from the folder where the certificate tools are situated (incuding signtool.exe)...

Needs to run on Windows 7 and Windows 2003 Server R2 Standard Edition.
Comment
Watch Question

Top Expert 2010
Commented:
try something like this:

procedure TForm1.Button1Click(Sender: TObject);
const
  signCmdTemplate = 'signtool sign /fMyCert.pfx /pPassword /t"timestamp URL" "%s%s" ';
var
  signCmd: string;  
begin
  signCmd:= Format( signCmdTemplate, [IncludeTrailingBackslash(MyPath), MyFile] );
  if RunAndWait(signCmd) = 0 then
    ShowMessage('Couldn''t execute the application');
end;

function RunAndWait(const CommandLine: String): integer;
var
  aProcessInfo: PROCESS_INFORMATION;
  aStartupInfo: STARTUPINFO;
  n: Boolean;
  Cmd: string;
begin
  with aStartupInfo do
  begin
    cb := sizeOf(aStartupInfo);
    lpReserved := nil;
    lpDesktop := nil;
    lpTitle := nil;
    dwX := 0;
    dwY := 0;
    dwXSize := 0;
    dwYSize := 0;
    dwXCountChars := 80;
    dwYCountChars := 25;
    dwFillAttribute := 0;
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := SW_SHOWMINIMIZED; // or SW_HIDE 
    cbReserved2 := 0;
    lpReserved2 := nil;
    hStdInput := 0;
    hStdError := 0;
    hStdOutput := 0;
  end;

  Cmd:= CommandLine;
  UniqueString(Cmd);
  n := CreateProcess(nil, PChar(Cmd), nil, nil,
                     False, 0,
                     nil, nil, aStartupInfo, aProcessInfo);

  if n then
  begin
    WaitForSingleObject(aProcessInfo.hProcess, INFINITE);
    Result:= aProcessInfo.hProcess;
  end
  else
    Result:= 0;
end;

Open in new window

Author

Commented:
The solution gave me all the clues I needed to figure the rest of the problem out. Great Help.