Hello.
I have a problem with getting function ptr at runtime, an example of a class is below.
I use CScriptHandle and funcdefs in a script to call a function that will be registered after the script will be compiled.
bool library::load(const std::string & libname) {
lib = LoadLibraryA(libname.c_str());
if (lib != NULL)
return true;
return false;
}
CScriptHandle library::get_function(std::string function_address, std::string function_signature){
FARPROC procAddress = GetProcAddress(lib, function_address.c_str());
if (procAddress) {
typedef void* (*FunctionType)(...);
FunctionType function = reinterpret_cast<FunctionType>(procAddress);
asIScriptContext* ctx = asGetActiveContext();
asIScriptEngine* engine = ctx->GetEngine();
asUINT func_id= engine->RegisterGlobalFunction(function_signature.c_str(), asFUNCTION(function), asCALL_CDECL);
asIScriptFunction* f = engine->GetFunctionById(func_id);
CScriptHandle h;
h.Set(f, f->GetObjectType());
return h;
}
}
void library::unload() {
FreeLibrary(lib);
}
function signature is
engine->RegisterObjectMethod("library", "ref@ get_function(const string&in, const string &in)", asMETHOD(library, get_function), asCALL_THISCALL);
my script is
funcdef int message_box(int, string, string, int);
library l;
void main(){
l.load("C:/windows/system32/user32.dll");
message_box@msg=cast<message_box>(l.get_function("MessageBoxA", "int msgbox(int, string, string, int)"));
msg(0, "Test text", "Test title", 0);
}
But instead of message_box, I get the an error
NGTRuntimeError
info: func: void main()
modl: ngtgame
sect: G:/projects/NGT/NGTScript/library_test.ngt
line: 7
desc: Unbound function called
--- call stack ---
But I return handle to the function, as shown in library class. What's my mistake? Thanks for your help.