How to use resource files in D
Koroskin Denis
2korden at gmail.com
Thu Jul 3 12:36:57 PDT 2008
On Thu, 03 Jul 2008 23:10:45 +0400, Jim Gadrow <makariverslund at gmail.com>
wrote:
> Ok, on to my next problem in my unceasing struggle to learn both D and
> the Windows API at the same time.
>
> I've done a LOT of searching and cannot manage to find the correct
> method of creating and using a resource in a windows program.
>
> What I would like to see is a minimal example of creating a 'hello
> world' windows application with the strings replaced by string resources.
>
> Obviously, I have no idea how to import the symbol ID_HELLO into my
> application. I've tried following one example I saw in one news group
> that suggested replacing the symbol name with the id value and doing:
>
> const char* ID_HELLO = cast (char*) 1;
>
> But that resulted in an Access Violation.
>
> And declaring something like:
>
> extern (D)
> const char* ID_HELLO;
>
> resulted in a message box with an empty message. I've never dealt with
> resource files before, so this is all new to me. It would probably be
> nice to have a sample available with all the others that shows the
> proper way of handling resources.
>
> I had the following in my .rc file (It's probably wrong...)
> #define ID_HELLO 1
>
> STRINGTABLE
> BEGIN
> ID_HELLO "Hello World!"
> END
>
> And this is my program:
> import std.c.windows.windows;
> extern (C)
> {
> void gc_init ();
> void gc_term ();
> void _minit ();
> void _moduleCtor ();
> void _moduleDtor ();
> void _moduleUnitTests ();
> }
>
> extern (Windows)
> int WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int
> nCmdShow)
> {
> int result;
>
> gc_init ();
> _minit ();
>
> try
> {
> _moduleCtor ();
> _moduleUnitTests ();
>
> result = myWinMain (hInst, hPrev, lpCmdLine, nCmdShow);
>
> _moduleDtor ();
> }
> catch (Object o)
> {
> MessageBoxA (null, cast (char*) o.toString (), "Error", MB_OK |
> MB_ICONEXCLAMATION);
> result = 0;
> }
>
> gc_term ();
> return result;
> }
>
> int myWinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int
> nCmdShow)
> {
> MessageBoxA (null, ID_HELLO, "Hello world example ", MB_OK);
> return 0;
> }
Just looked at the DFL implementation. rctest example has the rctest.res
and rctest.rc file of the following contents:
~~~~~~~~~~~~~ // beginning of the file
101 ICON DISCARDABLE "tray.ico"
"mspaint" BITMAP DISCARDABLE "rctest.bmp"
STRINGTABLE DISCARDABLE
BEGIN
3001 "This string was loaded from the resource!"
END
~~~~~~~~~~~~~ // end of file
These can be generated with Visual Studio, for example.
Command line looks like this:
dmd.exe somefile.d somefile.res
There are also a few functions to retrieve resources from executable:
Use LoadIcon, LoadImage, FindResourceEx and LoadResource methods to
extract data from those .res files.
For code snippets take a look into dfl.resources module (getIcon,
getString etc)
DFL can be downloaded from www.dprogramming.com/dfl.php
Hope you find this useful.
More information about the Digitalmars-d
mailing list