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 ?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With