4

I want to load Win32 API functions using Runtime.loadLibrary and GetProcAddress(...). Using mixin:

template GetProcA(alias func, alias name_in_DLL)
{
    const char[] GetProcA = func ~ ` = cast(typeof(`~func~`)) GetProcAddress(hModule,"`~name_in_DLL~`");`;
}
...
static extern (Windows) Object function (HWND hWnd, int nIndex) GetWindowLong;
static extern (Windows) Object function (HWND hWnd, int nIndex, Object dwNewLong) SetWindowLong;

I can instantiate it (in the class constructor) this way:

mixin GetProcA!("SetWindowLong", "SetWindowLongA");

but if use it again for another function:

mixin GetProcA!("GetWindowLong", "GetWindowLongA");

the compiler complains:

mixin GetProcA!("GetWindowLong","GetWindowLongA") GetProcA isn't a template...

I don't see the point: if the first instantiation created GetProcA and I can't use it again, so how does it help me here ?

2 Answers 2

6

Judging from your code, you want a mixin expression, not template mixin:

string GetProcA(string func, string name_in_dll)
{
   return func ~ ` = cast(typeof(` ~ func ~ `)) ` ~
                       `GetProcAddress(hModule,"` ~ name_in_dll ~ `");`;
}

mixin(GetProcA("SetWindowLong", "SetWindowLongA"));
mixin(GetProcA("GetWindowLong", "GetWindowLongA"));

Actually, you don't even need a mixin. An inner template function is enough:

hModule = ...;

void GetProcA(T)(ref T func, string name_in_all)
{
    func = cast(typeof(func)) GetProcAddress(hModule, toStringz(name_in_all));
}

GetProcA(SetWindowLong, "SetWindowLongA");
GetProcA(GetWindowLong, "GetWindowLongA");
7
  • Both work - thanks! but using template won't increase my executable size ? as for every function with slightly different signature (int instead of uint as input variable, for instance) more code is generated (and nearly identical), isn't it ?
    – Tar
    Jan 10, 2012 at 16:29
  • True, you do get code bloat but it should be tiny. The generated function just forwards the args and return value of another function. The actual cast should be a no-op at the machine code level. In fact, a smart compiler may be able to fold all instances of the function together. Besides, you will get bloat for every usage regardless of how few signatures you have for the string mixin option.
    – BCS
    Jan 10, 2012 at 16:35
  • 1
    @KennyTM: shouldn't that have a template constraint limiting it to function pointers?
    – BCS
    Jan 10, 2012 at 16:36
  • @BCS: My worry is that the whole function code would be duplicated. It's a problem to generalize function types: in one you pass a byte, in the other an int (32/64 bit), and that's just one variable entry - that would be doubled on two vars and so on... Correct - every expression 'bloats' the code, but when I import the whole User32.dll, I just want to have N function pointers - I don't want to add to that additional N function-pointer-loaders.
    – Tar
    Jan 10, 2012 at 16:45
  • @Tal, the template Kenny proposed would be instantiated for each signature you use it for. If you have 100 function you load but they only have 3 signatures, it will be instantiated only 3 times. Also, I would expect each instantiation would be a dozen op-codes at the most so even that is a minor consideration.
    – BCS
    Jan 10, 2012 at 17:25
3

I think KennyTM is correct. However for completeness, you can use the same template mixin more than once if you name them. Search for "MixinIdentifier" here.

4
  • That whole mixin/template/template mixin stuff confuses me.. Is it true that the mixin result is embedded somehow within the code ? when the first CPU-instruction is executed, the result of the mixin is already "hard-coded" in the binary ?
    – Tar
    Jan 10, 2012 at 16:55
  • 2
    @Tal: A string mixin causes the expression being mixed in to be evaluated (at compile time), the resulting string injected into the code and the compiler to process it (kinda like C #define). A template mixin instantiates the template and grafts its declarations into the scope. -- For both cases, if the resulting code has run-time dependencies (e.g. calls to GetProcAddress) then the generated D code can't be evaluated at compile time. If OTOH, the generated code doesn't have such dependencies, the compiler might do constant folding.
    – BCS
    Jan 10, 2012 at 17:32
  • So what I understand is that the compiler makes best effort to plant it hard-coded during compilation, but if it can't then it executes the mixin during run-time. Correct ?
    – Tar
    Jan 10, 2012 at 20:28
  • 2
    @Tal. Ah no. The string expression Will get evaluated at compile time (if it can't be you will get an error) and it's result gets processed as part of your code. At that point (still during compilation), it is just another piece of code that the compiler might be able to partially evaluate (via constant folding) just like any other bit of code. -- Any string mixin can be replaced, in the source file, by the text of the expression that it mixes in and the result will be the same (line numbering aside) as the original code.
    – BCS
    Jan 10, 2012 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.