4

Runtime.loadLibraryと を使用して Win32 API 関数をロードしたいと考えていますGetProcAddress(...)。使用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;

この方法で (クラス コンストラクターで) インスタンス化できます。

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

ただし、別の機能に再度使用する場合:

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

コンパイラは文句を言います:

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

要点がわかりません。最初のインスタンス化が作成されGetProcA、それを再度使用できない場合、ここでどのように役立ちますか?

4

2 に答える 2

6

コードから判断すると、テンプレート mixinではなくmixin expressionが必要です。

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"));

実際には、ミックスインも必要ありません。内側のテンプレート関数で十分です:

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");
于 2012-01-10T15:29:32.560 に答える
3

KennyTMが正しいと思います。ただし、完全を期すために、名前を付ければ、同じテンプレートmixin を複数回使用できます。ここで「MixinIdentifier」を検索してください。

于 2012-01-10T15:40:12.217 に答える