why do i need an extern(C): here?

WhatMeWorry kheaser at gmail.com
Thu Oct 15 21:29:59 UTC 2020


I've go a small DLL and a test module both written in D. Why do I 
need to use the extern(C)?  Shouldn't both sides be using D name 
wrangling?

-------------------- mydll.d --------------------------------
module mydll;

extern(C):   // removing or changing to (D): results in error

export
{
     int addSeven(int a, int b) { return a+b+7; }
}


-------------------- user.d --------------------------------
module user;

import core.sys.windows.winbase;
import std.stdio;

void main()
{
     alias addSevenFuncSignature = int function(int, int);

     addSevenFuncSignature  addSeven;

     import core.runtime;
     auto mydll = Runtime.loadLibrary("mydll.dll");

     assert(mydll);

     addSeven = cast(addSevenFuncSignature)  GetProcAddress(mydll, 
"addSeven");

     int ret = addSeven(2,3);

     writeln("calling addSeven(2,3) = ", addSeven(2,3));

     Runtime.unloadLibrary(mydll);
}

------------------- execution results 
------------------------------

c:\D\dmd2\samples\d\mydll>dmd -m64 mydll.d -L/DLL -L/NOENTRY
    Creating library mydll.lib and object mydll.exp

c:\D\dmd2\samples\d\mydll>dmd -m64 user.d

c:\D\dmd2\samples\d\mydll>user.exe
calling addSeven(2,3) = 12




More information about the Digitalmars-d-learn mailing list