delphi 中的 Cmd 字符串到 PAnsiChar
Cmd String to PAnsiChar in delphi
我对 Delphi 比较陌生,我想使用 ShellExecute
命令快速创建一个应用程序。
我想在编辑框中使用字符串值添加到命令行,以便在应用程序外部执行处理作业。
一切正常,但出现错误:
"Incompatible types: String and PAnsiChar"
我尝试使用以下方法转换它:
Variable := PAnsiChar(AnsiString(editbox.Text)
,但无济于事。
谁能帮我解决这个问题。
编译不了怎么能正常运行呢?
您发布的代码太少,无法确定哪里出了问题,但您的类型转换肯定太多了。 AnsiChar
是只能存储单个字符的类型,这里没有意义。
如果 Variable
是 PAnsiChar
那么您应该使用:
Variable := PAnsiChar(editbox.Text)
在 Delphi 7 中,它是对 PChar
的简单类型转换,它已经是 PAnsiChar
:
PChar(YourStringVariable);
或
PChar('Some text here'); // Cast not needed; demonstration only
PChar('C:\' + AFileName); // Cast needed because of variable use
与ShellExecute
一起使用:
AFile := 'C:\MyDir\Readme.txt';
Res := ShellExecute(0, 'open', PChar(AFile),
nil, nil, SW_NORMAL )
我对 Delphi 比较陌生,我想使用 ShellExecute
命令快速创建一个应用程序。
我想在编辑框中使用字符串值添加到命令行,以便在应用程序外部执行处理作业。
一切正常,但出现错误:
"Incompatible types: String and PAnsiChar"
我尝试使用以下方法转换它:
Variable := PAnsiChar(AnsiString(editbox.Text)
,但无济于事。
谁能帮我解决这个问题。
编译不了怎么能正常运行呢?
您发布的代码太少,无法确定哪里出了问题,但您的类型转换肯定太多了。 AnsiChar
是只能存储单个字符的类型,这里没有意义。
如果 Variable
是 PAnsiChar
那么您应该使用:
Variable := PAnsiChar(editbox.Text)
在 Delphi 7 中,它是对 PChar
的简单类型转换,它已经是 PAnsiChar
:
PChar(YourStringVariable);
或
PChar('Some text here'); // Cast not needed; demonstration only
PChar('C:\' + AFileName); // Cast needed because of variable use
与ShellExecute
一起使用:
AFile := 'C:\MyDir\Readme.txt';
Res := ShellExecute(0, 'open', PChar(AFile),
nil, nil, SW_NORMAL )