使用ParamStr()获取带双引号的参数

D.D*_*D.D 4 delphi

所以我们有一个第三方工具,我们无法重新编译,但它使用这个ParamStr(索引:整数)来获取命令行参数.我已经尝试了我可以在互联网上找到的所有东西,但它不接受双引号字符.我使用了转义字符等,并创建了一个测试应用程序来简化测试过程并将问题归结为此功能.

有没有人通过这个函数通过命令行参数成功传递双引号?

编辑:我在下面发布了我的测试应用程序.processfromcommandline函数来自第三方库.

对此的示例输入将是这样的:

"file1.adb file2.adb -p4THISISAPASSWORD"
Run Code Online (Sandbox Code Playgroud)

密码直接在-p4之后.我们的密码是"加密的",看起来更像这样

file1.adb file2.adb -p4$/.;}{3"aG13Sz/"9@;.'
Run Code Online (Sandbox Code Playgroud)

测试应用程序使用ShowMessage输出它获得的字符串,这样你就可以看到delphi正在做什么.所以我的输入将是一些东西

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, clipbrd;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FParams:      Boolean;
    FAbort:       Boolean;
    FAppPath:     String;
    FLogPath:     String;
    FSuccess:     Boolean;
    FSourceFile:  String;
    FDestFile:    String;
    FParamCount:  Integer;
    FPassword4:   String;
    FKeyFile4:    String;
    FIVFile4:     String;
    FPassword5:   String;
    FKeyFile5:    String;
    FIVFile5:     String;
    procedure ProcessFromCommandLine;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ProcessFromCommandLine;
var bTo4: boolean;
    i,l,sz: Integer;
    s:      String;
    buf:    PAnsiChar;
begin
  bTo4 := False;
  for i := 1 to FParamCount do
   begin
    s := ParamStr(i);
    //s := getCommandLine;
    l := Length(s);
    if (i = 1) then
     FSourceFile := s
    else
    if (i = 2) and (Pos('-',s) = 0) then
     FDestFile := s
    else
    if (l > 3) and ((Pos('-p4',s) > 0) or (Pos('/p4',s) > 0)) then
    begin
      ShowMessage('uh' + s);
      FPassword4 := Copy(s,4,l-3);
      end
    else
    if (l > 3) and ((Pos('-p5',s) > 0) or (Pos('/p5',s) > 0)) then
      FPassword5 := Copy(s,4,l-3)
    else
    if (l > 3) and ((Pos('-i4',s) > 0) or (Pos('/i4',s) > 0)) then
      FIVFile4 := Copy(s,4,l-3)
    else
    if (l > 3) and ((Pos('-i5',s) > 0) or (Pos('/i5',s) > 0)) then
      FIVFile5 := Copy(s,4,l-3)
    else
    if (l > 3) and ((Pos('-k4',s) > 0) or (Pos('/k4',s) > 0)) then
      FKeyFile4 := Copy(s,4,l-3)
    else
    if (l > 3) and ((Pos('-k5',s) > 0) or (Pos('/k5',s) > 0)) then
      FKeyFile5 := Copy(s,4,l-3)
    else
    if (l > 2) and ((Pos('-l',s) > 0) or (Pos('/l',s) > 0)) then
      FLogPath := Copy(s,3,l-2)
    else
    if (s = '-4') then
      bTo4 := True
    else
    if (s = '-5') then
      bTo4 := False;
   end;
   Clipboard.AsText := FPassword4;
   ShowMessage(FPassword4);

end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FParamCount := ParamCount;
  FParams := (FParamCount >= 1);
  if (FParams) then
   ProcessFromCommandLine;
end;

end.
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 8

ParamStr()剥离"引号字符,即使在嵌入的引号字符串中,如示例中所示.从而

-p4$/.;}{3"aG13Sz/"9@;.'
Run Code Online (Sandbox Code Playgroud)

获取返回为

-p4$/.;}{3aG13Sz/9@;.'
Run Code Online (Sandbox Code Playgroud)

这是硬编码行为,如果不改变RTL的源代码,则无法更改它.

甚至微软都做同样的事情(参见第2和第4个例子),但至少它支持转换为嵌入式"字符\". ParamStr()根本不支持任何类型的转义语法!

实际上,ParamStr()有一些与其引用处理相关的错误1(例如引用的空参数,""完全被忽略),所以我建议使用Win32 GetCommandLine()获取原始命令行数据,然后自己解析它,无论你想要什么.

1:最初记录在旧的Quality Central错误跟踪器中的错误,但从未移植到较新的Quality Portal错误跟踪器,并且QC已经退役并且现在处于脱机状态.