如何轻松地初始化函数指针?
我想使用 Runtime.loadLibrary 和 GetProcAddress(...) 加载 Win32 API 函数。使用 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...
我不明白这一点:如果第一个实例化创建了 < code>GetProcA 并且我无法再次使用它,那么它对我有什么帮助?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的代码来看,您需要一个 mixin 表达式,而不是 模板 mixin:
实际上,你甚至不需要 mixin。一个内部模板函数就足够了:
Judging from your code, you want a mixin expression, not template mixin:
Actually, you don't even need a mixin. An inner template function is enough:
我认为 KennyTM 是正确的。但是为了完整起见,如果您命名它们,则可以多次使用相同的模板 mixin。在此搜索“MixinIdentifier”。
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.