【问题标题】:Delphi cannot execute brcc32 via shellexecuteDelphi 无法通过 shellexecute 执行 brcc32
【发布时间】:2017-08-01 10:58:54
【问题描述】:

我已经开始编写组件,我想编写一个程序来为我生成 DCR 文件。组件的图片必须是24x24位图,所以我需要创建资源文件,然后使用brcc32创建DCR。

步骤:

  1. 创建 24x24 位图(油漆,旧但金色)
  2. 创建 RC 文件
  3. 使用 brcc32 创建 DCR

所以,我想编写一个程序来为我制作所有这些东西,这就是表格。在编辑字段中,我写了他们的Name 属性。

这是代码:

unit uBmp2rc;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.IOUtils,
  Vcl.ExtDlgs, Vcl.ExtCtrls, ShellApi;

type
  TBitmapConverter = class(TForm)
    Label1: TLabel;
    edtClassName: TEdit;
    Label2: TLabel;
    edtSource: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    edtDirectory: TEdit;
    OpenPicture: TOpenPictureDialog;
    Label5: TLabel;
    edtBitmap: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label6: TLabel;
    Preview: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    path: string;
  public
    { Public declarations }
  end;

var
  BitmapConverter: TBitmapConverter;

implementation

{$R *.dfm}

procedure TBitmapConverter.Button1Click(Sender: TObject);
begin

 if OpenPicture.Execute then
  begin
   edtBitmap.Text := OpenPicture.FileName;
   Preview.Picture.LoadFromFile(OpenPicture.FileName);
  end;

end;

procedure TBitmapConverter.Button2Click(Sender: TObject);
var sw: TStreamWriter;
    tmpName, source, command: string;
begin

 path := edtDirectory.Text;
 source := edtSource.Text;

 tmpName := TPath.Combine(path, source+'.rc');
 Preview.Picture.SaveToFile(TPath.Combine(path, source + '.bmp'));

 sw := TStreamWriter.Create(tmpName, False, TEncoding.UTF8);
 try
  sw.Write(edtClassName.Text + ' BITMAP "' + source + '.bmp"');
 finally
  sw.Free;
 end;

 command := '/C brcc32 -fo"' + TPath.Combine(path, source) + '.dcr" "' + TPath.Combine(path, source) + '.rc"';
 ShellExecute(0, nil, PChar('cmd.exe'), PChar(command), nil, SW_HIDE);

end;

procedure TBitmapConverter.FormCreate(Sender: TObject);
begin
 edtDirectory.Text := TPath.GetDocumentsPath;
end;

我可以正确创建 RC 文件,但没有创建 DCR。我在命令中做错了吗?

我在谷歌搜索后添加了PChar(),我在 StackOverflow 上找到了提示,但我仍然不确定。

【问题讨论】:

  • 为什么不检查返回错误?您可能需要指定brcc32.exe 的整个路径。我也认为整个序言与问题无关。
  • 嗯,我怀疑问题出在您的 rc 文件中。我会重现你的代码,我会检查一下,我认为 UTF8 不是你要找的。谷歌上也有一些关于它的东西
  • @RaffaeleRossi,很公平,但是你为什么不检查错误呢?错误(即使来自 Windows shell 本身中的 cmd.exe,也可能导致错误)
  • Raffaele 找到答案,正如@kobik 所说,您可以检查错误。为了找到它,我简单地保存了一个 UTF8 rc 并用 brcc32 对其进行了测试。它给了我错误“源输入中的错误字符”,我用谷歌搜索并找到了解决方案(= UTF8 不好,ANSI 很好)。
  • 无意义的要求shell创建一个cmd进程来创建一个brcc32进程。调用 CreateProcess 创建一个 brcc32 进程。

标签: delphi delphi-10.2-tokyo


【解决方案1】:

当我为我的组件创建图像时,我使用记事本并将文件保存为 filename.rc 并使用默认编码(ANSI,而不是 UTF8)。您正在使用 UTF8,您应该更改:

sw := TStreamWriter.Create(tmpName, False, TEncoding.UTF8);

用这个:

sw := TStreamWriter.Create(tmpName, False, TEncoding.ANSI);

如果您使用 ANSI 编码,您的程序将可以运行。你的命令是正确的;如果您尝试运行 cmd.exe 并使用这些参数调用 brcc32,您将看到 UTF8 编码给您一个错误。相反,ANSI 编码可以完美运行,您可以使用 *.drc 文件。

参见here 类似的东西,它是关于 c++ builder 但它表明问题与 UTF8 编码有关。

【讨论】:

  • 但是ANSI和UTF8有什么区别呢?他们如何存储字符?
  • @RaffaeleRossi google 可以完全回答您的问题
  • 好的。无论如何更改为ANSI解决了我的问题,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 2020-05-26
  • 2018-11-14
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多