【问题标题】:incompatible types pwidechar and string ShellExecute不兼容的类型 pwidechar 和字符串 ShellExecute
【发布时间】:2016-05-09 09:38:17
【问题描述】:

我尝试使用winrar 命令行压缩文件, 但是当我在命令行中添加一个变量时,我得到了这些错误 “PWideChar”和“字符串”类型不兼容

我将 sdate 变量转换为 WideChar 但它不起作用!!

我该如何解决它!

procedure TForm1.Button1Click(Sender: TObject);
var
mydate : TDateTime;
sdate : string;
begin
mydate:= Now-7;
sdate := FormatDateTime('YYYY/mm/dd',mydate);
  ShellExecute(0, 'open', PChar('C:\Program Files\WinRAR\WinRar.exe'),
    'a -r -ta'+ PChar(sdate) +' D:\xlsFiles.rar D:\*.xls*', nil, SW_SHOW);
end;

【问题讨论】:

  • ShellExecuteShellExecuteEx 弃用,后者能够以理智的方式报告错误。但是,由于您正在执行一个新进程,您最好致电CreateProcess
  • 您可能想跳过调用另一个进程并改用 zip 库,例如内置的 TZipFile、Abbrevia、...

标签: delphi delphi-xe7 shellexecute


【解决方案1】:

ShellExecute 的文本参数的类型为 PChar。但是你为参数号 4 提供了一个字符串。

错误信息非常清楚。通过检查ShellExecute 的声明,您知道有问题的参数是PChar 类型(PWideChar 的别名)。错误消息告诉您您正在传递string

代替

'a -r -ta'+ PChar(sdate) +' D:\xlsFiles.rar D:\*.xls*'

通过

PChar('a -r -ta'+ sdate +' D:\xlsFiles.rar D:\*.xls*')

【讨论】:

  • 谢谢,它的工作,但 PChar 和 PWideChar 之间的区别是什么?因为两者都有效!
  • 这在第二段中有介绍,我说PCharPWideChar 的别名。它也被记录在案(docwiki.embarcadero.com/Libraries/en/System.PChar),但这个特定的文件完全是垃圾......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
相关资源
最近更新 更多