我正在尝试为我的函数提供其模板参数的返回类型(以改进类型检查),这是一个函数引用。 no instance of function template matches the argument list argument types are: (const char [32], const char [20])
到目前为止,这是我尝试过的事情:
#include
模板
decltype(auto)proxyFunction(LPCSTR dllPath,LPCSTR functionName){
自动funcType = decltype(func);
funcType funcPtr =(funcType)GetProcAddress(LoadLibraryA(dllPath),functionName);
如果(funcPtr)
std::cout <<“代理成功” << std::endl;
其他
std::cout <<“代理失败” << std::endl;
返回funcPtr;
}
BOOL GetFileVersionInfoProxy(LPCSTR lptstrFilename,DWORD dwHandle,DWORD dwLen,LPVOID lpData){
自动getFileVersion = proxyFunction
getFileVersion(lptstrFilename,dwHandle,dwLen,lpData);
}
但是,关于proxyFunction
,我收到以下编译时错误:
我不太确定该错误的处理方式,因为它相当模糊。所以我想知道是否有人可以解释我的代码段中存在什么问题?
附言如果有帮助,我正在使用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;
}
Live Demo
关于c++ - 在C++中将函数返回类型指定为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62645630/