Reputation: 11
I have an existing MFC application which I'm trying to extend to accept command line parameters and run unattended.
I need to kick off some events after the InitInstance() has finished and the existing GUI has been fully loaded. I've looked at winmain.cpp, but it's not clear to me how to run my events as it seems to kick off a thread and 'disappear' from the debugger (i.e. what gets executed next? Must be the MFC loop, right? Is it possible to hook into there?) I'm new to this and it's entirely possible I'm missing something insight at a higher level, which is not that easily googled. Grateful for pointers.
Thanks.
Upvotes: 1
Views: 2456
Reputation: 4887
The easiest way to intercept command line parameters is to reference the global variables __targv (defined as LPCTSTR* __targv
or something like that) and __argc (defined as int
).
For example:
for(int i = 0; i < __argc; ++i) {
DoSomethingWithArg(__targv[i]);
}
Basically just like any other console application.
Upvotes: 1
Reputation: 490058
I would parse the command line in InitInstance
just like usual, but instead of immediately processing all the commands you find, add some special processing for the ones you care about that (for example) post messages back to your own message queue so when you're ready to start processing messages, they'll show up as the first things to do.
To do that, I'd probably derive a class from CComandLineInfo
, and override ParseParam
to handle the commands you care about (and have it send any other arguments it doesn't recognize back to CComandLineInfo::ParseParam
to be handled normally). Then, in InitInstance, replace this bit of code:
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
With one using your custom command line parser:
MyCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
ParseCommandLine
will then invoke your ParseParam
for each parameter on the command line, giving you the first chance at deciding what it means and how to process it. You'll probably want to look up the "standard" commands that CComandLineInfo
already understands, and leave them alone unless you really need to change them.
Upvotes: 1
Reputation: 3505
you can overload PreTranslateMessage and than skip messages there
Upvotes: 0