Sierra
Sierra

Reputation: 103

Execute tregsvr.exe through Delphi with parameters

I am using the tregsvr.exe program that comes with Delphi to register an OCX.

The reason for using this program instead of regsrv32.exe is that a -c parameter can be passed which would allow registration only for the current user.

I execute the program through the ExecAndWait() function, copied below. It uses ShellExecuteEx() (instead of CreateProcess()), precisely because (as long as I don't manage to pass the parameter for current user) I must ask for elevation and this is done by passing the boolean Adm which fixes 'runas' (I read about an alternative way with the app manifest, but did not manage to get it working, maybe that is for another question).

function ExecAndWait(const ExecuteFile, ParamString : string; Adm: boolean): boolean;
var
  SEInfo: TShellExecuteInfo;
  ExitCode: DWORD;
begin
  FillChar(SEInfo, SizeOf(SEInfo), 0);
  SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  with SEInfo do
  begin
    fMask := SEE_MASK_NOCLOSEPROCESS;
    Wnd := Application.Handle;
    lpFile := PChar(ExecuteFile);
    lpParameters := PChar(ParamString);
    If Adm then lpVerb:='runas';
    nShow := SW_HIDE;
  end;
  if ShellExecuteEx(@SEInfo) then
  begin
    repeat
      Application.ProcessMessages;
      GetExitCodeProcess(SEInfo.hProcess, ExitCode);
    until (ExitCode = STILL_ACTIVE) or Application.Terminated;
    Result:=True;
  end
  else Result:=False;
end;

In this example of usage, the variable Path is the full path to tregsvr.exe and Server is the full path to the OCX. It works well (it does the job of registration):

ExecandWait(Path,Chr(9)+Server+Chr(9), True);

But my problem comes when I try to pass -c or other parameters, like in any of these attempts, where the function is unsuccessful:

ExecandWait(Path,'-c '+Chr(9)+Server+Chr(9), True);
ExecandWait(Path,'\c '+Chr(9)+Server+Chr(9), True);

Upvotes: 0

Views: 401

Answers (1)

pepak
pepak

Reputation: 742

Your Chr(9)+Server+Chr(9) is almost certainly incorrect, in a minor and a major way:

  • The minor problem: Chr(9) is a tab. While that is a perfectly legitimate whitespace character, it's rather uncommon to encounter it as a part of a command line and as a result, I would not be surprised if the application you are trying to run failed to process it correctly.
  • The major problem: If Server contains spaces, you will actually pass multiple command line arguments to tregsvr.exe rather than one argument, and quite unsurprisingly tregsrv.exe will not be able to understand them.

Solution: Put the argument into quotes to signal that it is actually one argument, not many: AnsiQuotedStr(Server, '"').

Upvotes: 1

Related Questions