Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to painlessly initialize function pointers?

Tags:

d

dmd

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 ?

like image 628
Tar Avatar asked Jan 17 '23 20:01

Tar


1 Answers

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");
like image 125
kennytm Avatar answered Jan 31 '23 14:01

kennytm