因此,我们有一个无法重新编译的第 3 方工具,但它使用此 ParamStr(Index: Integer) 来获取命令行参数。我已经尝试了在互联网上可以找到的所有内容,但它不接受双引号字符。我使用了转义字符等,并创建了一个测试应用程序来简化测试过程并将问题定位到该函数。
有人曾经使用此函数成功地通过命令行参数传递双引号吗?
编辑:我在下面发布了我的测试应用程序。 processfromcommandline 函数来自第 3 方库。
输入示例如下:
"file1.adb file2.adb -p4THISISAPASSWORD"
密码就在-p4之后。我们的密码是“加密的”,看起来更像这样
file1.adb file2.adb -p4$/.;}{3"aG13Sz/"9@;.'
测试应用程序输出使用 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.
最佳答案
ParamStr()
会去除 "
引号字符,即使在嵌入的带引号的字符串中也是如此,如您的示例所示。因此
-p4$/.;}{3"aG13Sz/"9@;.'
返回为
-p4$/.;}{3aG13Sz/9@;.'
这是硬编码行为,如果不更改 RTL 的源代码,您就无法更改它。
偶数Microsoft does the same thing (参见第二个和第四个示例),但至少它支持嵌入的 "
字符转义为 \"
。 ParamStr()
根本不支持任何类型的转义语法!
事实上,ParamStr()
有一些与其引用处理相关的错误1(例如带引号的空参数、""
、被完全忽略),所以我建议使用 Win32 GetCommandLine() 来获取原始命令行数据,然后根据需要自行解析它。
1:最初记录在旧的 Quality Central 错误跟踪器中的错误,但从未移植到较新的 Quality Portal 错误跟踪器中,并且 QC 已停用且现已离线。
关于delphi - 使用 ParamStr() 获取带双引号的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525969/