Delphi 使用ParamStr()获取带双引号的参数
所以我们有一个第三方工具,我们不能重新编译,但它使用这个ParamStr(Index:Integer)来获取命令行参数。我已经尝试了我能在互联网上找到的一切,但它不会接受双引号字符。我使用了转义字符等,并创建了一个测试应用程序来简化测试过程,并将问题锁定到此函数 是否有人使用此函数成功地通过命令行参数传递双引号 编辑:我已经在下面发布了我的测试应用程序。processfromcommandline函数来自第三方库 这方面的输入示例如下:Delphi 使用ParamStr()获取带双引号的参数,delphi,Delphi,所以我们有一个第三方工具,我们不能重新编译,但它使用这个ParamStr(Index:Integer)来获取命令行参数。我已经尝试了我能在互联网上找到的一切,但它不会接受双引号字符。我使用了转义字符等,并创建了一个测试应用程序来简化测试过程,并将问题锁定到此函数 是否有人使用此函数成功地通过命令行参数传递双引号 编辑:我已经在下面发布了我的测试应用程序。processfromcommandline函数来自第三方库 这方面的输入示例如下: "file1.adb file2.adb -p4THISI
"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的源代码,就无法更改它
偶数(参见第2个和第4个示例),但至少它支持嵌入式“
字符转义为\”
ParamStr()
完全不支持任何类型的转义语法
事实上,ParamStr()
有一些与其引号处理相关的错误1(例如引号中的空参数,“”
,被完全忽略),因此我建议使用Win32GetCommandLine()
获取原始命令行数据,然后自己解析它
1:最初记录在旧的Quality Central bug tracker中的bug,但从未被移植到较新的Quality Portal bug tracker,并且QC已经退役,现在处于脱机状态。我认为您应该添加一个您想要处理的命令行形式的示例。您说您已经创建了一个测试应用程序:它在哪里?ParamStr是预处理的tty非常能够处理双引号。这里甚至有文档记录:“使用双引号将多个单词包装为一个参数(例如包含空格的长文件名)。”我相信OP的问题是“如何将包含双引号字符的字符串传递给应用程序?”,即“是否可以使ParamStr(1)成为参数?”返回一个包含双引号字符的字符串?”。@Uwe不确定这是重点。您如何将foo”bar作为参数传递?我已经更新了我的东西以包含测试应用程序。转义字符不起作用。如果你能证明我错了,那就太好了,但我已经尝试了embarcaderos网站上列出的所有东西,但都没有用。很酷,谢谢你澄清这一点。不幸的是,我无法更改此代码,因为我们的第三方库拒绝为我们提供支持。但是很高兴知道问题是函数而不是我。谢谢