我正在尝试为我的函数提供其模板参数的返回类型(以改进类型检查),这是一个函数参考。到目前为止,这是我尝试过的事情:
#include <Windows.h> 模板<typename func> decltype(auto)proxyFunction(LPCSTR dllPath,LPCSTR functionName){ auto funcType = decltype(func); funcType funcPtr =(funcType)GetProcAddress(LoadLibraryA(dllPath),functionName); if(funcPtr) std :: cout <<“代理成功” << std :: endl; 否则 std :: cout <<“代理失败” << std :: endl; 返回funcPtr; } BOOL GetFileVersionInfoProxy(LPCSTR lptstrFilename,DWORD dwHandle,DWORD dwLen,LPVOID lpData){ auto getFileVersion = proxyFunction <GetFileVersionInfoProxy>(“ C:\\ Windows \\ System32 \\ Version.dll”,“ GetFileVersionInfoA”); }
但是,关于以下内容,我遇到了以下编译时错误proxyFunction
:
没有与参数列表匹配的函数模板实例,参数类型为:(const char [32],const char [20])
我不太确定该错误的处理方式,因为它相当模糊。所以我想知道是否有人可以解释我的代码段中存在什么问题?
PS我正在使用MS VS 2019和C ++ 17标准,以防万一。
您的func
模板参数已经是您要返回的类型,因此,请照原样使用它,而无需使用decltype(auto)
。而您的使用auto funcType = decltype(func);
是完全错误的。
尝试以下方法:
template <typename funcType>
funcType proxyFunction(LPCSTR dllPath, LPCSTR functionName)
{
funcType funcPtr = (funcType) GetProcAddress(LoadLibraryA(dllPath), functionName);
if (funcPtr)
std::cout << "Proxy success" << std::endl;
else
std::cout << "Proxy fail" << std::endl;
return funcPtr;
}
BOOL GetFileVersionInfoProxy(LPCSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData)
{
using GetFileVersionInfoA_FuncType = BOOL (WINAPI *)(LPCSTR, DWORD, DWORD, LPVOID);
auto getFileVersion = proxyFunction<GetFileVersionInfoA_FuncType>("C:\\Windows\\System32\\Version.dll", "GetFileVersionInfoA");
if (getFileVersion)
return getFileVersion(lptstrFilename, dwHandle, dwLen, lpData);
return FALSE;
}
另外,如果让编译器为您推断出模板参数,则可以省略传递模板参数:
template <typename funcType>
bool proxyFunction(LPCSTR dllPath, LPCSTR functionName, funcType &funcPtr)
{
funcPtr = (funcType) GetProcAddress(LoadLibraryA(dllPath), functionName);
if (funcPtr)
std::cout << "Proxy success" << std::endl;
else
std::cout << "Proxy fail" << std::endl;
return (funcPtr != nullptr);
}
BOOL GetFileVersionInfoProxy(LPCSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData)
{
using GetFileVersionInfoA_FuncType = BOOL (WINAPI *)(LPCSTR, DWORD, DWORD, LPVOID);
GetFileVersionInfoA_FuncType getFileVersion;
if (proxyFunction("C:\\Windows\\System32\\Version.dll", "GetFileVersionInfoA", getFileVersion))
return getFileVersion(lptstrFilename, dwHandle, dwLen, lpData);
return FALSE;
}
更新:基于@MooingDuck的注释,看来您实际上是在尝试将代理函数传递给模板,并推断出与DLL函数一起使用的必要参数和返回类型。如果是这样,请尝试以下类似方法:
template <typename RetType, typename... ArgTypes>
struct proxyTraits
{
using funcType = RetType (WINAPI *)(ArgTypes...);
};
template <typename RetType, typename... ArgTypes>
auto proxyFunction(
LPCSTR dllPath,
LPCSTR functionName,
RetType (*proxy)(ArgTypes...))
{
using funcType = typename proxyTraits<RetType, ArgTypes...>::funcType;
funcType funcPtr = (funcType) GetProcAddress(LoadLibraryA(dllPath), functionName);
if (funcPtr)
std::cout << "Proxy success" << std::endl;
else
std::cout << "Proxy fail" << std::endl;
return funcPtr;
}
BOOL GetFileVersionInfoProxy(LPCSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData)
{
auto getFileVersion = proxyFunction("C:\\Windows\\System32\\Version.dll", "GetFileVersionInfoA", &GetFileVersionInfoProxy);
if (getFileVersion)
return getFileVersion(lptstrFilename, dwHandle, dwLen, lpData);
return FALSE;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句