标题看起来很奇怪,我想做的很简单。查看以下定义:
ImportFunc(void, il2cpp_init_utf16, (const Il2CppChar* domain_name));
ImportFunc(void, il2cpp_shutdown, ());
ImportFunc(void, il2cpp_set_config_dir, (const char* config_path));
ImportFunc(void, il2cpp_set_data_dir, (const char* data_path));
ImportFunc(void, il2cpp_set_commandline_arguments, (int argc, const char* const argv[], const char* basedir));
ImportFunc(void, il2cpp_set_commandline_arguments_utf16, (int argc, const Il2CppChar* const argv[], const char* basedir));
ImportFunc(void, il2cpp_set_config_utf16, (const Il2CppChar* executablePath));
ImportFunc(void, il2cpp_set_config, (const char* executablePath));
我有很多这样的定义,我想“将它们变成函数”。我已经尝试过这样的事情:
#define ImportFunc(t, n, a) \
t n a \
{ \
void* pfunc = (void*)CallSystem(kernel32, GetProcAddress, Globals::GameAssembly, n); \
return SpoofCall<type>((void*)pfunc, args...); \
}
问题是如何解析函数参数并将其传递给另一个函数。SpoofCall函数的确如下所示:
template<typename Ret = void, typename First = void*, typename Second = void*, typename Third = void*, typename Fourth = void*, typename... Stack>
__forceinline Ret SpoofCall(void* Func, First a1 = First{}, Second a2 = Second{}, Third a3 = Third{}, Fourth a4 = Fourth{}, Stack... args) {
有没有办法做到这一点?我可以定义要接受模板的函数,然后仅使用这些参数调用SpoofCall,但是我想用参数定义函数,以便我可以更轻松地使用它们并将它们与IntelliSense集成:
编辑:更改了宏中的错字
您可以尝试类似:
#define ImportFunc(_type,_name,...) \
_type (&x_##_name) (__VA_ARGS__) = *ImportT<_type,## __VA_ARGS__>(#_name)
// Convenience typedef, as spelling template function
// returning template pointer to function
// is *too* error-prone otherwise
template <typename Ret, typename ...Args>
using FuncT = Ret (Args...);
template <typename Ret, typename ...Args>
FuncT<Ret, Args...> *ImportT(char const *name) {
std::printf("Name: %s\n", name);
return (FuncT<Ret, Args...> *)dlsym(RTLD_DEFAULT, name); // GetProcAddress on windows
}
ImportFunc(double, sin, double);
ImportFunc(double, sqrt, double);
ImportFunc(int, fn3, float);
int main() {
printf("sin(1) = %.3f\n", x_sin(1.0f));
printf("sqrt(2) = %.3f\n", x_sqrt(2.0f));
}
该符号将引用功能而不是功能,但是IntelliSense应该接受。该引用将在程序启动时初始化。允许参数名称更成问题,但是使用更多的函数重载魔术应该可以实现(有关更多信息,请参见CppReference文章)。
编辑:它确实更简单,就像float (int a, int b)
C ++中的有效类型一样:
#define ImportFunc(_type,_name,...) \
_type (&x_##_name) (__VA_ARGS__) = *ImportT<_type (__VA_ARGS__)>(#_name)
template <typename Func>
Func *ImportT(char const *name) {
std::printf("Name: %s\n", name);
return (Func *)dlsym(RTLD_DEFAULT, name); // GetProcAddress on windows
}
ImportFunc(double, sin, double t);
ImportFunc(double, sqrt, double x);
ImportFunc(int, fn3, float a, char const * b);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句