无法读取我的资源(.res)文件

时间:2018-07-18 15:12:35

标签: delphi

我写了一个简单的加载器来安装程序及其帮助文件。

unit PSInstaller;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Registry, Vcl.StdCtrls, HTMListB,
  HTMLabel, System.Zip;

type
  TfmPDSInstaller = class(TForm)
    HTMLabel1: THTMLabel;
    HTMListBox1: THTMListBox;
    btnNext: TButton;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function InstallFile(ResID: integer; pName: String): Boolean;
  public
    { Public declarations }
  end;

var
  fmPDSInstaller: TfmPDSInstaller;

implementation

{$R 'ProtonStudio32.res' 'ProtonStudio32.rc'}
{$R *.dfm}

Var IDEDirectory: String;

Const APP = 100;
      HELP = 200;

procedure TfmPDSInstaller.btnNextClick(Sender: TObject);
begin
  HTMListBox1.AddItem('Copying Proton Studio to Proton IDE directory',nil);
  if InstallFile(APP, 'Studio Application') then begin
    HTMListBox1.AddItem('Copying Proton Studio Help to Proton IDE directory',nil);
    If InstallFile(HELP, 'Studio Help') then
      HTMListBox1.AddItem('Proton Studio Installed', nil);
  end;
end;

function TfmPDSInstaller.InstallFile(ResID: integer; pName: String): Boolean;
Var rs: TResourceStream;
    Zip: TZipFile;
    s: String;
begin
  Result := false;
  try
    Rs := TResourceStream.CreateFromID(HInstance, ResID, RT_RCDATA);
    Zip := TZipFile.Create;
    try
      Zip.Open(Rs,zmRead);
      Zip.ExtractAll(IDEDirectory);
    finally
      Rs.Free;
      Zip.Free;
      Result := true;
    end;
  except
    on EFOpenError do
      s  := 'Unable to Open resource ' + pName;
    else
      s := 'Unable to Copy file from resource ' + pName;
  end;
  HTMListBox1.AddItem(s, nil);
end;

procedure TfmPDSInstaller.Button1Click(Sender: TObject);
begin
Close;
end;

procedure TfmPDSInstaller.FormCreate(Sender: TObject);
Var Reg: TRegistry;
begin
  btnNext.Enabled := false;
  Reg := TRegistry.Create;
  HTMListBox1.AddItem('Checking for ProtonIDE',nil);
  if Reg.OpenKey('Software\MecaniqueUK\ProtonIDE\Install', false) then begin
    IDEDirectory := Reg.ReadString('IDE');
    Reg.CloseKey;
  end;
  Reg.Free;
end;

procedure TfmPDSInstaller.FormShow(Sender: TObject);
begin
  btnNext.Enabled := false;
  if DirectoryExists(IDEDirectory) then begin
    HTMListbox1.AddItem('Click Next to install Proton Studio in ' + IDEDirectory, nil);
    btnNext.Enabled := true;
  end
  else
    HTMListBox1.AddItem('Proton IDE must be installed first', nil);
end;    
end.

我创建了一个.rc脚本来加载程序和帮助

#100 RT_RCDATA "D:\Data\Documents\RAD Studio\Projects\ProtonNewIDE\Win32\Debug\ProtonNewIDE.zip"
#200 RT_RCDATA "D:\Data\Documents\RAD Studio\Projects\ProtonNewIDE\Win32\Debug\Proton Studio.zip"

我在Delphi Berlin 10.1中工作,生成导致生成我的资源文件,可以在资源编辑器中打开它,但是当我尝试打开资源时:

Rs := TResourceStream.CreateFromID(Application.Handle, ResID, RT_RCDATA);

我收到地址冲突。此时,它会破坏System.Classes:

  HResInfo := FindResource(Instance, Name, ResType);

并且Name和ResType均为空。

我希望得到一个指向我在做什么错的指针?

1 个答案:

答案 0 :(得分:2)

您正在传递窗口句柄而不是模块句柄。相反,将HInstance传递给包含此代码的模块的句柄。