I have a third-party console application. I need run it from my application but I cannot run it as a separate process (because I need to work with its dependencies: fill Import tables manually, setup hooks etc.). So probably I should call main
function of this executable manually. Here is how I'm trying to do this:
- Load this EXE using
auto hMod = LoadLibrary("console_app.exe")
- Fill Import table of this exe manually
- Get entry point of this EXE and call it
And I'm stuck with the last step.
Here is how I'm trying to call entry point:
void runMain(HINSTANCE hInst)
{
typedef BOOL(WINAPI *PfnMain)(int, char*[]);
auto imageNtHeaders = ImageNtHeader(hInst);
auto pfnMain = (PfnMain)(DWORD_PTR)(imageNtHeaders->OptionalHeader.AddressOfEntryPoint + (DWORD_PTR)hInst);
char* args[] = { R"(<console_app_path>)", R"(arg1)", R"(arg2)" };
pfnMain(3, args);
}
It works. But it works as if there is no arguments.
Where am I wrong? How can I run an executable inside my process with arguments? Thanks.
UPDATE:
I've investigated how my particular third-party exe gets cmd arguments and found that:
- It doesn't import
GetCommandLine
at all and do not call it - After
call _initterm
callargc
andargv
arguments are available throughcs:argc
andcs:argv
(see pictures below) - CMD arguments that I pass to my main console app are transferred to child EXE too.
Can you explain, please, what _initterm
actually do and where CMD arguments are actually stored?
main
itself, but a wrapper aroundmain
. In which case, the parameters are highly implementation dependent. – HalifaxEXE
have relocations. which not always true forEXE
– BerubeEXE
in separate process, and inject ownDLL
to it at start point byQueueUserAPC
which set the hooks – Berube__getmainargs
or__wgetmainargs
frommsvcrt.dll
- msdn.microsoft.com/en-us/library/ff770599.aspx also possible use_acmdln
, or_wcmdln
- msdn.microsoft.com/en-us/library/ff770586.aspx – Berube_initterm
call global object constructors and global vars initialization – Berube