I have a DLL that gets loaded as a plugin by a program I did not create and it's purpose is to replace a pointer to a member function in an array inside the program to provide additional functionality to the existing program that loads it.
The program (that loads the plugin dll) has source code that looks like this
//Core object that all objects inherit
class MObject
{
public:
//variables
//functions
}
enum ECmdIndex
{
CMD_RUN = 0x0,
//ect
CMD_MAX = 0x500
}
//Global command list of command functions
typedef void (MObject::*CommandFunc)(SContext&, void*)
CommandFunc GCommands[CMD_MAX];
//Example command function
void MObject::funcCmdRun(SContext& context, void* result)
{
//do stuff
}
GCommands[CMD_RUN] = &MObject::funcCmdRun;
//This is used to execute command functions
void MObject::ProcessCommand( SContext& context, void* result )
{
//ect
//Execute the command's function on this MObject
//Here is where my function should replace the default one
(this->*GCommands[context.cmdInd])(context, result);
//ect
}
My DLL is fairly straight forward. It mainly replaces a command function in the program's GCommands array with one in the DLL.
//MObject structure is replicated exactly in the DLL
//This is neccesary because my plugin is going beyond the provided API
//And so this is becoming a bit "hacky"
class MObject
{
public:
//variables
//functions
}
typedef void (MObject::*CommandFunc)(SContext&, void*)
void MObject::MyCommandFunc(SContext& context, void* result)
{
//do stuff
}
void onLoad()
{
//Get a pointer to GCommands array
CommandFunc** pGCommands = GetGCommandOffset();
//Replaced the desired command function with the new one
pGCommand[0x1B] = &MObject::MyCommandFunc;
}
A brief abstract: The host program calls a member function pointer. My DLL should make the pointer point to its own function and do what is must to make that function callable when the host application goes to call that pointer.
The issue is my new command function is never entered and the command no longer does anything when executed in the program. I would at the very least expect a crash. I apologize for the inherent hacky appearance of the code but surely there is a correct way to accomplish this?