imports in functions

Brad Roberts braddr at puremagic.com
Mon Jun 13 20:35:23 PDT 2011


On 6/13/2011 8:28 PM, Andrej Mitrovic wrote:
> Walter, it looks like this addition inadvertently fixes the issue of
> DLLs not linkable due to Phobos imports.
> 
> I've had this DLL (alongside with dllmodule.d which had initialization
> calls inside DLLMain):
> module EdrLib;
> 
> import std.utf;
> 
> pragma(lib, "gdi32.lib");
> pragma(lib, "comdlg32.lib");
> import win32.windef;
> import win32.wingdi;
> 
> export extern(Windows) BOOL EdrCenterText(HDC hdc, PRECT prc, string pString)
> {
>      SIZE size ;
>      GetTextExtentPoint32(hdc, pString.toUTF16z, pString.length, &size) ;
>      return TextOut(hdc, (prc.right - prc.left - size.cx) / 2,
>                          (prc.bottom - prc.top - size.cy) / 2,
> pString.toUTF16z, pString.length);
> }
> 
> The header file produced from this would cause any client code which
> imports the header to look for ModuleInitZ, which wouldn't exist in
> the generated import library since it's an import library and not a
> static library.
> 
> But, if I move the phobos import inside the EdrCenterText function:
> 
> export extern(Windows) BOOL EdrCenterText(HDC hdc, PRECT prc, string pString)
> {
>      import std.utf;
> 
>      SIZE size ;
>      GetTextExtentPoint32(hdc, pString.toUTF16z, pString.length, &size) ;
>      return TextOut(hdc, (prc.right - prc.left - size.cx) / 2,
>                          (prc.bottom - prc.top - size.cy) / 2,
> pString.toUTF16z, pString.length);
> }
> 
> Then it works. My related bug report about this (and its complicated
> to read due to various misunderstanding on my part) is
> http://d.puremagic.com/issues/show_bug.cgi?id=6019.
> 
> But it's great that there's an actual workaround now!

This makes me think that there's actually a bug in the function-local imports.  I'm guessing they don't run module-level
ctors and dtors for the imported modules.  Would you mind putting together a test case to check?

Thanks,
Brad


More information about the Digitalmars-d mailing list