How to use resource files in D

Jim Gadrow makariverslund at gmail.com
Thu Jul 3 12:10:45 PDT 2008


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;
}



More information about the Digitalmars-d mailing list