为什么 std::function 的传统 GetProcAddress 不能直接工作

Why traditional GetProcAddress to std::function is not working straightforward

如标题所示,我想将 GetProcAddress 转换为 std::function。是的,堆栈溢出有多种解决方案,但 none 实际上解释了为什么需要这些解决方法。我无法真正理解确切的错误消息及其发生的原因。样本来源很简单:

#include <functional>
#include <Windows.h>

using fn_NtAllocateVirtualMemory = NTSTATUS (NTAPI*)(
        HANDLE      ProcessHandle,
        PVOID*      BaseAddress,
        ULONG_PTR   ZeroBits,
        PSIZE_T     RegionSize,
        ULONG       AllocationType,
        ULONG       Protect
    );

int main()
{
    std::function<fn_NtAllocateVirtualMemory> myFn(reinterpret_cast<fn_NtAllocateVirtualMemory>(0xDEADBABE));
}

( https://godbolt.org/z/FhaeLA )

那么,我的问题是为什么会出错?


相关: 我也试过这个: Function pointer to multiple argument C++11 std::function: Templating GetProcAddress 但它也无法编译 ( https://godbolt.org/z/1wSDZj )

我也找到了这个主题: C++ Dynamically load arbitrary function from DLL into std::function 这可能行得通 (我还没有尝试过,我很快就会做) 但我试图理解为什么需要这种看起来很神秘的巨大样板。


备注:

std::function 将签名作为模板参数,而不是指针。

所以应该是:

using fn_NtAllocateVirtualMemory = NTSTATUS NTAPI (
        HANDLE      ProcessHandle,
        PVOID*      BaseAddress,
        ULONG_PTR   ZeroBits,
        PSIZE_T     RegionSize,
        ULONG       AllocationType,
        ULONG       Protect
    );

int main()
{
    std::function<fn_NtAllocateVirtualMemory> myFn(
        reinterpret_cast<fn_NtAllocateVirtualMemory*>(0xDEADBABE));
}