Jump to content
  • Advertisement
Sign in to follow this  
RetroBilly

How to pass function handle from GetProcAddress to luabind::def?

This topic is 3052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my games scripting I need to offer the ability to add extra functions from DLLs. I'm using lua and luabind for scripting in my game but I can't seem to pass the FARPROC to luabind::def how do I bind the function GetProcAddress to lua?
void def_function(std::string lib_name, std::string script_name)
{
	FARPROC func = GetProcAddress( handle, lib_name.c_str() );
	luabind::module( g_Lua )
	[
		luabind::def( script_name.c_str(), func )
	];
}

Share this post


Link to post
Share on other sites
Advertisement
A FARPROC isn't really anything meaningful, it needs cast to the correct function pointer type to call it. So you could pass it to luabind as any pointer/light userdata type (Assuming luabind allows that).

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
A FARPROC isn't really anything meaningful, it needs cast to the correct function pointer type to call it. So you could pass it to luabind as any pointer/light userdata type (Assuming luabind allows that).


I'm not entirely sure I understand what you are saying but I have tried int, DWORD and void* but none of them worked.

I normally just put the name of the function in that spot, I know luabind is using boost, maybe I need to use some type of boost function pointer?

Share this post


Link to post
Share on other sites
Luabind uses compile-time introspection to figure out the signature of the method to call. You can store the function pointer in a small wrapper class and then pass a function on that wrapper class to luabind instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Luabind uses compile-time introspection to figure out the signature of the method to call. You can store the function pointer in a small wrapper class and then pass a function on that wrapper class to luabind instead.


I honestly don't know how to do that, can you show me what you mean?

Share this post


Link to post
Share on other sites
Have you tried casting the return value of GetProcAddress to a function pointer with a signature that matches the function you are binding? For example, consider a void function taking an int as its only parameter:


void (*test)(int) = (void (*)(int)) GetProcAddress(handle, "test");
//...
luabind::def( "test", function );



By itself, the FARPROC is merely the address at which the function starts. Because luabind needs type information in order to generate the appropriate glue code, providing the address is not sufficient. By casting it to a function pointer with the appropriate signature, you should be able to supply luabind with the information it needs. Note that I haven't tested this code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Windryder
Have you tried casting the return value of GetProcAddress to a function pointer with a signature that matches the function you are binding? For example, consider a void function taking an int as its only parameter:

*** Source Snippet Removed ***

By itself, the FARPROC is merely the address at which the function starts. Because luabind needs type information in order to generate the appropriate glue code, providing the address is not sufficient. By casting it to a function pointer with the appropriate signature, you should be able to supply luabind with the information it needs. Note that I haven't tested this code.


I hate to bump such an old post but I've been on vacation and have not been able to reply, anyway so what you are saying is that I need to define the structure of the function in order to pass it to luabind?

The problem with this is that it completely voids the point of adding libraries to my scripting since I would have to recompile the engine in order to add new functions or classes...

Also the code you posted is very vague, I don't understand what the test class is or how the class can represent the structure of the function?

Is there another way to pass the structure of the function during runtime to luabind?

Share this post


Link to post
Share on other sites
Quote:
I hate to bump such an old post
Three days isn't that old :)
Quote:
The problem with this is that it completely voids the point of adding libraries to my scripting since I would have to recompile the engine in order to add new functions or classes...
My understanding is that Luabind requires the signature of the functions that you register to be known at compile time. This is how Luabind was designed to work, and makes sense given the mechanisms that it uses.

It seems that what you're wanting is to register functions with Lua via Luabind whose signatures are not known at compile time. A question you might ask is, if Luabind doesn't know what arguments the function accepts, how is it supposed to call the function? And if all it has is a generic pointer to the function, how would it even cast the pointer to the appropriate type in order to call it?

Here's one way you might think about the problem. Say you were just using 'plain' Lua (without a binding library), and wanted to register arbitrary functions at run time. How would you do it? When it came time to gather the arguments and invoke the function on the C++ side, what would the syntax look like?

I use Lua and Luabind, but I'm not an expert on either, so maybe there's a solution to this problem that I haven't thought of - if so, I'm sure someone will come along and post it :)
Quote:
Also the code you posted is very vague, I don't understand what the test class is or how the class can represent the structure of the function?
The code Windryder posted isn't vague, but it might have a typo in it. I think maybe the first line was supposed to be:
void (*function)(int) = (void (*)(int)) GetProcAddress(handle, "test");

Share this post


Link to post
Share on other sites
Quote:
Original post by SteveDeFacto
The problem with this is that it completely voids the point of adding libraries to my scripting since I would have to recompile the engine in order to add new functions or classes...
As jyk says, you need to at least know the parameters and return type of the function in order to actually call it. That doesn't mean you can't load functions at runtime, though, just that you need to settle on a "standard" signature that all "plugin" functions need to provide.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Quote:
Original post by SteveDeFacto
The problem with this is that it completely voids the point of adding libraries to my scripting since I would have to recompile the engine in order to add new functions or classes...
As jyk says, you need to at least know the parameters and return type of the function in order to actually call it. That doesn't mean you can't load functions at runtime, though, just that you need to settle on a "standard" signature that all "plugin" functions need to provide.


There is no way I can provide a "standard" signature for all functions since all functions will have different inputs and outputs, the better option would be to use pure lua to bind the functions and classes but there are quite a few problems with that as well.

Programming plugins would be a huge pain since all functions would be lua functions and the DLL would have to include lua.hpp further more when I add classes this problem would only be worse...

There must be some way to provide a new signature at runtime?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!