我可以传递参数给一个使用Delphi编写的Windows服务应用程序吗?

huangapple go评论48阅读模式
英文:

Can I pass a parameter to a Windows Service Application Delphi

问题

我一直在使用以下命令安装和卸载我的Delphi Windows服务,但现在我想传递自己的参数,这些参数在ParamStr中不显示:

MySrvc.exe /Install /Silent

MySrvc.exe /UnInstall /Silent

以下是我用来检查是否获取到参数的代码。只有ParamStr(0)保存在文本文件中,即可执行文件名。

procedure TVPS_Srvc.ServiceExecute(Sender: TService);
var
...
begin
  bJustStarted := True;
  try
    while not Terminated do
    begin
      if bJustStarted then
      begin
        sl := TStringList.Create;
        sl.Add(paramStr(0) + ' ' + paramStr(1) + ' ' + paramStr(2) + ' ' + paramStr(3));
        sl.SaveToFile('C:\ABC\params.txt');
        FreeAndNil(sl);
      end;
      ....
    end;
  end;
end;

如果您想要传递自定义参数,可以在命令行中添加它们,然后在paramStr中使用相应的索引来获取它们。在上述代码中,paramStr(0)获取可执行文件名,paramStr(1)获取第一个参数,paramStr(2)获取第二个参数,以此类推。您可以根据需要获取更多参数。

英文:

I have been installing and uninstalling my Delphi Windows Service with below mentioned commands, but now I want to pass my own parameter which does not show in ParamStr

MySrvc.exe /Install /Silent

MySrvc.exe /UnInstall /Silent

Below is the code I am using just to check if I get the params. Only ParamStr(0) gets save in the text file ie: the Exe Name.

procedure TVPS_Srvc.ServiceExecute(Sender: TService);
var
...
begin
  bJustStarted:=True ;
  try
    while not Terminated do
    begin
      if bJustStarted then
      begin
        sl:=TStringlist.create ;
        sl.Add(paramStr(0)+' '+paramStr(1)+' '+paramStr(2)+' '+paramStr(3)) ;
        sl.SaveToFile('C:\ABC\params.txt');
        Freeandnil(sl) ;
      end;
      ....
end;

答案1

得分: 1

命令行参数可以通过 ParamStr() 访问。但是,在 TService.OnExecute 事件中使命令行参数可用的唯一方法是手动修改注册表以编辑用于执行服务的命令行。否则,您必须在启动服务时将所需的命令放入SCM中。这些参数可通过 TService.Param[] 属性而不是 ParamStr() 访问。

英文:

Command line parameters are accessible via ParamStr(). But, the only way to make command line parameters available in the TService.OnExecute event is to manually modify the Registry to edit the command line that is used to execute the service. Otherwise, you have to put the desired commands in the SCM when starting the service. Those parameters are accessible via the TService.Param[] property instead of ParamStr().

huangapple
  • 本文由 发表于 2023年5月29日 13:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定