如何轻松地初始化函数指针?

发布于 2024-12-26 12:28:22 字数 860 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

塔塔猫 2025-01-02 12:28:22

从您的代码来看,您需要一个 mixin 表达式,而不是 模板 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"));

实际上,你甚至不需要 mixin。一个内部模板函数就足够了:

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

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");
初相遇 2025-01-02 12:28:22

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文