C# Interop

Rainer Schuetze r.sagitario at gmx.de
Tue Feb 1 00:05:13 PST 2011


Robert Jacques wrote:
> On Mon, 31 Jan 2011 16:25:11 -0500, Eelco Hoogendoorn 
> <hoogendoorn.eelco at gmail.com> wrote:
> [...]
> 
> Lastly, D DLLs will only work on Vista/Windows 7/later. They will not 
> work on XP. This is due to a long known bug with DLLs and thread local 
> storage in general on XP. Also, you'll have to use 32-bit C# currently, 
> as DMD isn't 64-bit compatible yet. (Walter is hard at work on a 64-bit 
> version of DMD, but it will be Linux only at first, with Windows 
> following sometime later)
> 

XP TLS support with dynamically loaded DLLs is fixed for some time now 
with a workaround implemented in druntime. Also, DLLs can be used in 
multi-threading environments.

 > I've listed some example code from my project below:
 >
 > // Written in the D Programming Language (www.digitalmars.com/d)
 > ///Basic DLL setup and teardown code. From D's/bugzilla's public domain
 > example code.
 > module dll;
 >
 > import std.c.windows.windows;
 > import std.c.stdlib;
 > import core.runtime;
 > import core.memory;
 >
 > extern (Windows)
 > BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved) {
 >     switch (ulReason) {
 >         case DLL_PROCESS_ATTACH:
 >             Runtime.initialize();
 >             break;
 >         case DLL_PROCESS_DETACH:
 >             Runtime.terminate();
 >             break;
 >         case DLL_THREAD_ATTACH:
 >         case DLL_THREAD_DETACH:
 >             return false;
 >     }
 >     return true;
 > }
 >

This DLLMain code is a bit outdated (is it D1?), the current proposed 
version is here: http://www.digitalmars.com/d/2.0/dll.html

Unfortunately, there is a regression in the latest dmd release (2.051): 
http://d.puremagic.com/issues/show_bug.cgi?id=5382 that causes TLS not 
to be initialized for new threads created by the host application.

Rainer


More information about the Digitalmars-d mailing list