www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - dll bug

Is there a trick to create a DLL? Take these two files:

[bar.d]
import std.c.windows.windows, std.string;

extern(Windows)
 alias int function () FOO;

int main() {
 HINSTANCE dll = LoadLibraryA(toStringz("foo.dll"));
 if (!dll) {
  return -1;
 }

 FOO foo = cast(FOO) GetProcAddress(dll,"_foo 0");
 if (!foo) {
  return -1;
 }

 printf("1: aleat() = %d\n",foo());
 printf("2: aleat() = %d\n",foo());
 printf("3: aleat() = %d\n",foo());

 FreeLibrary(dll);

 return 0;
}

[foo.d]
 import std.c.windows.windows;
 HINSTANCE g_hInst;

 extern (C)
 {
  void gc_init();
  void gc_term();
  void _minit();
  void _moduleCtor();
  void _moduleUnitTests();
 }

extern (Windows):
export int f () {
 static int a;
 return a++;
}

 BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
 {
     switch (ulReason)
     {
  case DLL_PROCESS_ATTACH:
      gc_init();   // initialize GC
      _minit();   // initialize module list
      _moduleCtor();  // run module constructors
      _moduleUnitTests();  // run module unit tests
      break;

  case DLL_PROCESS_DETACH:
      gc_term();   // shut down GC
      break;

  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
      // Multiple threads not supported yet
      return false;
     }
     g_hInst=hInstance;
     return true;
 }

After I run bar, it doesn't print anything! Via debugging I found out that
there's an AV in the dll right when f is called.

If I do implib over the DLL, and link it with the program (instead of
dinamically loading the DLL), it works as expected. Any ideas?

-----------------------
Carlos Santander Bernal
Jun 09 2004