Bug 22335 – Exporting variables from DLLs is unusable
Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2021-09-25T12:07:51Z
Last change time
2023-06-05T06:05:10Z
Keywords
dll
Assigned to
No Owner
Creator
Max Samukha
Comments
Comment #0 by maxsamukha — 2021-09-25T12:07:51Z
dll.d:
module dll;
export __gshared int x = 41;
extern(Windows) int DllMain(void*, uint, void*)
{
return 1;
}
----
client.d:
module client;
import dll;
void main()
{
assert(x == 41); // fails. x refers to garbage
}
----
dmd -m64 -shared dll.d
dmd -m64 client.d dll.lib
client.exe
'dmd -m64 -H -o- dll.d' emits the definition instead of declaration, so using the interface file is not an option either.
The only way to make this work is to manually change the definition in the .di file to the declaration ('extern export __gshared int x'), which is impractical.
Comment #1 by bugzilla — 2023-01-26T07:57:44Z
It's currently necessary to manually create a .di file with the contents:
export extern __gshared int x;
for this to work.
Comment #2 by maxsamukha — 2023-01-26T09:15:33Z
(In reply to Walter Bright from comment #1)
> It's currently necessary to manually create a .di file with the contents:
>
> export extern __gshared int x;
>
> for this to work.
That's literally what I wrote. The point of this bug report is that it has to be done manually because neither simple import nor "-H" work.
Comment #3 by bugzilla — 2023-06-04T06:30:08Z
The current master emits this:
---
// D import file generated from 'dll.d'
module dll;
export extern __gshared int x;
extern (Windows) int DllMain(void*, uint, void*);
---
And your example is now working!
Comment #4 by maxsamukha — 2023-06-05T06:05:10Z
(In reply to Walter Bright from comment #3)
>
> And your example is now working!
Cool, thanks!