0

Based on examples at for instance here, here, and here, I'm trying to include SVN revision info in a project. The result of a svn info call is stored in rev.txt (it's a plain ansi file). My revinfo.rc looks like this:

REV_TEXT    TEXT    rev.txt

My project looks like this:

unit rev;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm2 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;
var
  Form2: TForm2;
implementation
{$R *.dfm}
{$R revinfo.res}
procedure TForm2.Button1Click(Sender: TObject);
var
  RS : TResourceStream;
  MyStr : AnsiString;
begin
  RS := TResourceStream.Create(hInstance, 'REV_TEXT', RT_RCDATA);
  SetLength(MyStr, RS.Size);
  RS.Read(MyStr[1], RS.Size);
  RS.Free;
  Memo1.Text := MyStr;
end;
end.

The project compiles, in other words, the resource file itself is located by the compiler (or perphaps it is the linker?). Anyway; when the statement TResourceStream.Create(hInstance, 'REV_TEXT', RT_RCDATA); is executed, I get an EResNotFound exception, complaining it can't find resource REV_TEXT. I can confirm that the resource file is compiled satisfactory, containing the content of the rev.txt text file. Are there anyone out there who're able to reproduce my troubles?

BTW: I've also tried to use the indexed version of the TResourceStream-constructor, but I don't know which index to use (tried 0, 1, and 2 to no avail).

I really appreciate your help! :)

Community
  • 1
  • 1
conciliator
  • 6,078
  • 6
  • 41
  • 66

1 Answers1

4

The problem in your code is the line:

 TResourceStream.Create(hInstance, 'REV_TEXT', RT_RCDATA);

You must call the TResourceStream.Create with the same type of the resource TEXT.

The following code should work:

var
  RS : TResourceStream;
  MyStr : AnsiString;
begin
  RS := TResourceStream.Create(hInstance, 'REV_TEXT', 'TEXT');
  try
   SetLength(MyStr, RS.Size);
   RS.Read(MyStr[1], RS.Size);
  finally
    RS.Free;
  end;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • Thanks a lot! Works like a charm. (Although the text comes out in Chinese ... oh, well. One step further, at least.) :) – conciliator Jan 12 '11 at 07:38
  • Figured it out. The string type obviously have to be AnsiString as shown in RRUZ answer (the file is, as mentioned, a Ansi encoded file). I've changed the MyStr in the original question to not lead people astray. – conciliator Jan 12 '11 at 07:57