2011-01-11 95 views
0

基于在例如herehere例子,here,我想包括在项目SVN版本信息。 svn信息调用的结果存储在rev.txt(这是一个普通的ansi文件)。我revinfo.rc看起来是这样的:德尔福2010:无法找到资源 - EResNotFound

REV_TEXT TEXT rev.txt 

我的项目是这样的:

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. 

该项目汇编,换句话说,资源文件本身是由编译器位于(或perphaps它是连接?)。无论如何;当执行语句TResourceStream.Create(hInstance, 'REV_TEXT', RT_RCDATA);时,我得到一个EResNotFound异常,抱怨它找不到资源REV_TEXT。我可以确认资源文件的编译效果令人满意,其中包含rev.txt文本文件的内容。有没有人能够重现我的烦恼?我也试过使用TResourceStream构造函数的索引版本,但我不知道要使用哪个索引(尝试0,1和2无效)。

我真的很感谢你的帮忙! :)

回答

4

在你的代码的问题是该行:

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

必须调用TResourceStream.Create与同类型的资源TEXT的。

下面的代码应该工作:

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; 
+0

非常感谢!奇迹般有效。 (虽然文字是用中文出来的......哦,好吧,至少还有一步。):) – conciliator 2011-01-12 07:38:34