LoadLibrary for the calling program itself
I know that under Linux you can get a handle to the calling program with dlopen( NULL, RTLD_* ). Is there a way to do so in Windows using LoadLibrary? I've tried using LoadLibrary(NULL) which doesn't work, the handle variable is NULL, and I've tried by specifying the executable file as well and that doesn't seem to be working either.
Running the following code just now got me 0x011b0000
in handle
but the call to GetProcAddress
is still returning NULL.
From commands.h
typedef std::tr1::shared_ptr<command> cmd_ptr;
typedef void (_cdecl *CMD)( creature &, string & );
From commands.cpp
HMODULE handle; // <--- Declared towards top of the file
void load_commands() // <--- Called from main
{
handle = LoadLibrary(L"another-mud-project-src.exe");
std::ifstream cmd_file(COMMAND_FILE);
cmd_ptr cmd;
if( cmd_file.is_open() )
{
std::string tmp, hold;
while( true )
{
cmd_file >> tmp;
if( 开发者_如何转开发tmp == "Name" )
cmd_file >> hold;
else if( tmp == "Code")
{
cmd_file >> tmp;
cmd = std::make_shared<command>(hold,tmp);
}
else if( tmp == "Type" )
{
cmd_file >> tmp;
cmd->set_cmdtype(atoi(tmp.c_str()));
if( cmd->is_valid() )
cmd_vec.push_back(cmd);
else
cmd.reset();
}
}
}
}
void command::set_code( string & code )
{
code_ = code;
if( (func = (CMD)GetProcAddress( handle, code.c_str() )) == NULL )
{
std::cerr << "Could not find function name: " << code.c_str() << std::endl;
code_.clear();
}
}
Assuming I'm doing the right thing to allow the program to get a good handle on itself, is there anything special I need to do so GetProcAddress
will find the functions as desired? I've even tried putting EXP_CMD
, #define EXP_CMD __declspec(dllexport), in front of a function which doesn't work either.
I'm using VS2010 under Windows 7 Pro x64. Compiling as a 32 bit console app.
Edit
Thanks for the pointer on using GetModuleHandle(NULL)
Cat and Michael. Unfortunately GetProcAddress
, which I assume I still need to use, is still returning NULL values. After sticking a call to GetLastError()
in there I get 127 for the error code. I've tried putting extern "C"
in front of the function, based on a bit more research, but I'm still getting a NULL return with error code 127.
Edit 2
After a bit more searching it seems that Visual Studio still mangles the names anyways and I might need a definition file to go along with the executable. Is there some way to get rid of that extra bit of mangling through code or in Visual Studio?
Edit 3
Alright. I appear to have gotten it worked out more or less. Taking a technique I saw in some other code I added
#define CMDF( name ) \
extern "C" __declspec(dllexport) \
void (name)( creature & ch, string & argument )
into commands.h and wrapped my desired functions in it, CMDF( func_name )
, and it seems to be working as desired. Had to add the __declspec(dllexport)
bit and that seems to have done the trick.
The handle to your own process is already open, you should not use LoadLibrary
to open a new one. Use GetModuleHandle
(specifically, GetModuleHandle(NULL)
) or instance handle from WinMain
instead.
You can use GetModuleHandle(NULL)
to get a handle to the *.exe file and its symbols. That is preferred and less kludgy than relying on the *.exe file having a specific path.
You might like to check whether dllexport has done anything. Run dumpbin /exports <exe path>
to check that the function that you're attempting to locate with GetProcAddress has actually been exported.
dumpbin is included with Visual Studio and is most easily called from a "Visual Studio Command Prompt" (see "Visual Studio Tools" on the start menu.)
精彩评论