Last update March 7, 2012

Doc Comments /
Windows



D for Win32    

Table of contents of this page
D for Win32   
More Information   
Examples   
Messages   
Links   

More Information    

EricAnderton: There are some caveats to working with Dll's in D, these are largely due to D's status as a beta technology (for now).

  • Bug: stdin/stdout will be set to an invalid state when a win32 D-dll (built against dmd) is unloaded.
  • The dll stub code provided in the comments is incomplete (see example below)
  • As is mentioned in the original code, multithreading is not explicitly handled in DllMain. You'll have to roll your own hooks for this.

Examples    

EricAnderton: The DllMain provided on the digitalmars site doesn't catch exceptions during startup, nor does it execute the module destructors on process-detach. This is in contrast to the WinMain example that at least performs the exception handling.

The example below is a how to handle those two cases correctly:

extern (Windows)
BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
{
	switch (ulReason)
	{
	case DLL_PROCESS_ATTACH:
		try{
			gc_init();
			_minit();
			_moduleCtor();
			debug _moduleUnitTests(); // remove if you don't want to run the unittest
		}
		catch(Exception e){
			printf("Uncaught exception thrown in dll init: %.*s\n",e.toString() ~ "\0");
			return(false);
		}
                break;
	case DLL_PROCESS_DETACH:
		debug printf("detaching process.\n");
		_moduleDtor();
		gc_term();
		break;
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
		return false;
	}
	g_hInst=hInstance;
	return true;
}

Messages    

Add your comments here...

Links    


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: March 7, 2012 20:26 (diff))