0

I use a list of words in my Delphi program, and until now I would place the list in the Lines property of a TMemo. I don't need the visual component, though, rather a TStringList, so now I want to do things the proper way by using a resource file for this, and load my TStringList from the resource. I tried applying the information from this answer, but I get an error:

[dcc32 Error] E2161 Error: RLINK32: Unsupported 16bit resource in file "D:\etc\Unit1.rc"

For good measure, I have this Unit1.rc file:

RC_keywords RCDATA "keywords.txt"

I created this in Project → Resources and images..., but it looks like this is the same as writing the .rc file yourself.

In my program I have this resource include:

{$R *.rc}

and in my procedure

procedure TForm1.FormCreate(Sender: TObject);
var
  SL: TStringList;
  ResStream: TResourceStream;
begin
  SL := TStringList.Create;
  try
    ResStream := TResourceStream.Create(hInstance, 'RC_keywords', RT_RCDATA);
    SL.LoadFromStream(ResStream);
    //
    // do a lot of useful stuff here
    //
  finally
    SL.Free;
  end;

What's wrong here?

Community
  • 1
  • 1
stevenvh
  • 2,971
  • 9
  • 41
  • 54
  • Hmm. In earlier Delphi versions, I've always compiled an RC file to a Res using brcc32 before trying to include it. Have you tried that? – MartynA Jul 17 '14 at 12:07
  • @MartynA - I don't know if this is required. Shouldn't the `{$R *.rc}` take care of that? And how do I compile the resource manually? You mention brcc32, do you mean this is command line? (I buried command line in 1995) – stevenvh Jul 17 '14 at 12:12
  • Have you added your Unit1.rc to your Project? i.e. Project...Add To Project.. – Andy_D Jul 17 '14 at 12:13
  • @Andy_D - I have now, but I get the same error. Thanks for your reply though. – stevenvh Jul 17 '14 at 12:14
  • 3
    FWIW, the `*` in `{$R *.res}` is not just a wildcard for every file name, it merely stands for the unit name, i.e. it only looks for a file with the name of the unit, but with extension .res instead of .dcu. – Rudy Velthuis Jul 17 '14 at 12:27
  • @Rudy - Correct, as I found out a while ago already, but it's still worth mentioning. Thanks for the feedback. – stevenvh Jul 17 '14 at 12:33
  • @steven All this information is in the documentation. Probably worth you reviewing that again. http://docwiki.embarcadero.com/RADStudio/en/Resource_file_(Delphi) – David Heffernan Jul 17 '14 at 12:53

2 Answers2

5

You are not compiling the resource script into a compiled resource. You have to compile the script, the .rc file, to a compiled resource, a .res file. Do that using the resource compiler brcc32. Then link the resource like this

{$R keywords.res}

Or get the compiler to invoke the resource compiler for you

{$R keywords.res keywords.rc}

I'm assuming a resource script name of keywords.rc which makes more sense to me that Unit1.rc.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    In Delphi XE3, he can simply add the resource file to the project. The IDE will take care of the rest. No need to use brcc32 on the command line. – Rudy Velthuis Jul 17 '14 at 12:16
  • The compiler created a .res file, which, upon inspection, includes my word list. So, like Rudy says this is done automagically. – stevenvh Jul 17 '14 at 12:21
  • The code in your question differs from mine. As I said, you are attempting to link the script. – David Heffernan Jul 17 '14 at 12:23
  • 1
    @Rudy I wanted to explain why the asker's attempts were failing. Knowing about the process, even the existence of the resource compiler is important. Knowing the difference between script and compiled resource is important. – David Heffernan Jul 17 '14 at 12:28
3

As David says, the compiler can not use the .rc script directly, it must be compiled into a .res file.

I just created a simple text file, keywords.txt. Then I created another text file, with the same content as yours, called keywords.rc, all in the Delphi IDE.

After saving both, I clicked menu Project -> Add to Project... and added keywords.rc to the project. In the .dpr, the line

{$R 'keywords.res' 'keywords.rc'}

was added and the .res file created as soon as I compiled the project.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Yes, that's what I have too. But I noticed I still had an extraneous `{$R *.rc}` in my .pas file. When I removed this the error was gone. – stevenvh Jul 17 '14 at 12:30
  • Accepted because Rudy's answer led to the discovery of the actual error. – stevenvh Jul 17 '14 at 12:31
  • See my comment WRT the `{$R *.res}` line. Note that `{$R *.rc}` makes no sense to the IDE, so I guess that is why it ignored it and didn't remove it. – Rudy Velthuis Jul 17 '14 at 12:33
  • I'm not sure about that. The error message explicitely mentions "unit1.rc", so it looks like the line wasn't simply ignored. – stevenvh Jul 17 '14 at 12:35
  • The compiler has problems with the `*.rc` line. The IDE doesn't. – Rudy Velthuis Jul 17 '14 at 13:07
  • @David - I meant that the `{$R *.rc}` wasn't just ignored, because the derived filename appears in the error message. But my syntax was wrong and it was in the wrong place. – stevenvh Jul 17 '14 at 13:08
  • Rudy said the same as me. Especially after his edit where he added the same explanation for the error as I had already given you. He even explicitly stated that he was saying the exact same as me. I've no problem with any of this, other than your assertion that somehow Rudy's answer explained what you did wrong, and mine did not. It's almost as if you didn't actually read my answer and just looked at the code. Did you see the first sentence? – David Heffernan Jul 17 '14 at 13:14
  • I certainly never said that the `{$R *.rc}` line was being ignored. Did anyone say that? It certainly isn't. If you read the documentation that I linked for you you'll see an explanation of how it is interpreted by the compiler. And the error message in your question makes it clear that the compiler is attempting to link your `Unit1.rc` file as though it were a compiled resource. – David Heffernan Jul 17 '14 at 13:17
  • @David: Yes, I did. I said the IDE ignored it, i.e. forgot to remove it. It would remove a `{$R *.res}`, AFAIK, when necessary. – Rudy Velthuis Jul 17 '14 at 13:21
  • @RudyVelthuis OK, I can see that comment now. I'm a bit confused by this question. I thought I understood what was being asked, but perhaps I've missed the point. Which is why you wrote an answer to explain what I'd missed. Maybe. As I said, I'm confused now. Anyway, never mind. – David Heffernan Jul 17 '14 at 13:24