www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to use resource files in D

reply Jim Gadrow <makariverslund gmail.com> writes:
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;
}
Jul 03 2008
next sibling parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Thu, 03 Jul 2008 23:10:45 +0400, Jim Gadrow <makariverslund 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.
Jul 03 2008
parent reply Jim Gadrow <makariverslund gmail.com> writes:
Koroskin Denis Wrote:

 
 DFL can be downloaded from www.dprogramming.com/dfl.php
 
 Hope you find this useful.

Thanks for this! It should have some examples that I can see exactly how they're loading the resources. As far as the generation of the .rc files, I'm attempting to learn how to manually perform the task. Maybe it's a bad idea, but I generally like to learn how to do things in an non-GUI environment before moving to one. I have found that it reduces my dependancy on the IDE and increases my understanding of what's happening.
Jul 03 2008
parent Jussi Jumppanen <jussij zeusedit.com> writes:
Jim Gadrow Wrote:

 I'm attempting to learn how to manually perform the task. Maybe 
 it's a bad idea, but I generally like to learn how to do things 
 in an non-GUI environment before moving to one. I have found that 
 it reduces my dependancy on the IDE and increases my understanding 
 of what's happening.

For information about the Win32 API I would recommend downloading the Borland Win32 SDK Help File: http://www.zeusedit.com/forum/viewtopic.php?t=7 It's a fantastic Win32 help resource as it covers the basics of the Win32 API's without the excessive bloat of the MSDN.
Jul 03 2008
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Jim Gadrow 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...)

First, basic how-to questions like this are supposed to go to digitalmars.D.learn. No biggie though. As for the answer, you got some answers already about how to compile resource files, but I didn't see anyone yet mention that there is a free resource compiler called 'rcc.exe' in the BUP (Basic Utilities Package): http://ftp.digitalmars.com/bup.zip It doesn't handle some of the newer extensions to RC files like big Vista icons. That zip also contains the very useful 'implib' program that can be used to create dmd import libs from DLLs, so it's worth downloading in any event. --bb
Jul 03 2008
parent Jim Gadrow <makariverslund gmail.com> writes:
Bill Baxter Wrote:
 
 First, basic how-to questions like this are supposed to go to 
 digitalmars.D.learn.  No biggie though.
 

I'll try and remember this next time :)
 As for the answer, you got some answers already about how to compile 
 resource files, but I didn't see anyone yet mention that there is a free 
 resource compiler called 'rcc.exe' in the BUP (Basic Utilities Package):
      http://ftp.digitalmars.com/bup.zip
 It doesn't handle some of the newer extensions to RC files like big 
 Vista icons.
 

Yeah, I had already downloaded the package. Thanks though!
Jul 07 2008