- Joined
- Aug 6, 2011
- Messages
- 405
- Reaction score
- 2
- Points
- 18
So I had an idea, and it involves dynamically loading module DLLs, much like Orbiter does.
So I began with testing with one of them, everyone's favorite, ScnEditor.dll
Using Dependency Walker, I found all the DLL exports, and dynamically loaded them, and tried to pass along the Orbiter callbacks.
Interestingly, nothing crashes (surprisingly, actually :lol, until I tried to use the scenario editor.
With my DLL being the only module loaded (the actual ScnEditor is not loaded), two copies of Scenario Editor appear in the Custom menu. The first one crashes due to some memory access in my DLL, whereas the second one works.
Debugging my code, scInit is only called once, but two entries are still added.
Here's my code:
The offending line is likely:
All other "forwarded" functions work without crashing. I am forwarding all of ScnEditor's exports, except ModuleDate and GetModuleVersion.
At first, I tried using my DLLs handle as the argument to scInit:
But that fails because scenario editor then tries to load non-existent images from inside my DLL with ScnEditor's offset.
What would be the correct way to dynamically load and forward a DLL (if there is one)?
So I began with testing with one of them, everyone's favorite, ScnEditor.dll
Using Dependency Walker, I found all the DLL exports, and dynamically loaded them, and tried to pass along the Orbiter callbacks.
Interestingly, nothing crashes (surprisingly, actually :lol, until I tried to use the scenario editor.
With my DLL being the only module loaded (the actual ScnEditor is not loaded), two copies of Scenario Editor appear in the Custom menu. The first one crashes due to some memory access in my DLL, whereas the second one works.
Debugging my code, scInit is only called once, but two entries are still added.
Here's my code:
Code:
HMODULE scenarioEditorDLL;
typedef void(*ExternalInitModule)(HINSTANCE);
typedef void(*ExternalExitModule)(HINSTANCE);
typedef void(*ExternalPause)(bool);
typedef void(*ExternalDeleteVessel)(OBJHANDLE hVessel);
ExternalInitModule scInit;
ExternalExitModule scExit;
ExternalPause scPause;
ExternalDeleteVessel scDeleteVessel;
DLLCLBK void InitModule(HINSTANCE hDLL)
{
scenarioEditorDLL = LoadLibrary("ORBITER_ROOT\\Modules\\Plugin\\ScnEditor.dll"); //replaced with actual path to orbiter root in my code
scInit = (ExternalInitModule)GetProcAddress(scenarioEditorDLL, "InitModule");
scExit = (ExternalExitModule)GetProcAddress(scenarioEditorDLL, "ExitModule");
scPause = (ExternalPause)GetProcAddress(scenarioEditorDLL, "opcPause");
scDeleteVessel = (ExternalDeleteVessel)GetProcAddress(scenarioEditorDLL, "opcDeleteVessel");
scInit(scenarioEditorDLL);
}
DLLCLBK void opcPause(bool pause)
{
scPause(pause);
}
DLLCLBK void ExitModule(HINSTANCE hDLL)
{
scExit(scenarioEditorDLL);
}
DLLCLBK void opcDeleteVessel(OBJHANDLE hVessel)
{
scDeleteVessel(hVessel);
}
The offending line is likely:
Code:
scInit(scenarioEditorDLL);
All other "forwarded" functions work without crashing. I am forwarding all of ScnEditor's exports, except ModuleDate and GetModuleVersion.
At first, I tried using my DLLs handle as the argument to scInit:
Code:
scInit(hDLL)
What would be the correct way to dynamically load and forward a DLL (if there is one)?