Command Line processing - II

How do I go about automatically opening a file that was passed on
the command line?

I'm trying
to write some code in the Application InitInstance.

Maybe there's a better way to accomplish what I'm trying to do?

My main problem is that I can't
seem to make any calls from my
application InitInstance to any
functions from my Document, View, or
MainFrame classes (something about
illegal call to static member
function).  My handler for OnFileOpen
(when it is selected from the menu)
sits in my document class.  Ideally,
I would like to get this routine
called somehow upon startup of the

Is there a good place to initially
handle command line processing?
Maybe in OnInitialUpdate for my

Who is Participating?
ZoppoConnect With a Mentor Commented:
It doesn't work this way coz OnFileOpen is never called with ProcessShellCopmmand. Simply do your loading in an overridden OnOpenDocument instead of OnFileOpen, then it should work
By default command line parameters are handled in the InitInstance function of your CWinApp-derived class with following code:

 CCommandLineInfo cmdInfo;

the ParseCommandLine() function fills the cmdInfo object with data passed as command line (or default data, i.e. command FileNew if no filename passed)

the ProcessShellCommand() function then calls document function related on the data set in cmdInfo.

see help for these functions and the CCommandLineInfo class...

hope that helps,

mosaicerAuthor Commented:
Yes, this is as far as I was able to
get with the documentation.  It is
unclear to me where to proceed from
there.  (Sorry, I'm just a novice.
Just started with MFC 2 months ago)
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

By default you have to do nothing! If you pass a file via command line there happens following:

ParseCommandLine() sets cmdInfo like this:
cmdInfo.m_ShellCommand = CCommandLineInfo::FileOpen
cmdInfo.m_strFileName = "PassedFile.ext"

then ProcessShellCommand() does following:
BOOL bResult = TRUE;
switch (rCmdInfo.m_nShellCommand)
 case CCommandLineInfo::FileOpen:
 if (!OpenDocumentFile(rCmdInfo.m_strFileName))
  bResult = FALSE;

so, CWinApp::OpenDocumentFile() is called which calls CDocManager::OpenDocumentFile() which searches for best matching document template and calls its CDocTemplate::OpenDocumentFile() which creates a new document and then calls the new document's CDocument::OnOpenDocument() which calls the new documents CObject::Serialize().

Both CDocument::OnOpenDocument() and CObject::Serialize() are virtual.

Since the default implementation of CWinApp::OnFileOpen() simply calls CDocManager::OnFileOpen() which then calls CWinApp::OpenDocumentFile() there should be nothing to do for you, except you're opening your document files from OnFileOpen() by extra processing instead of simply calling base class's OnFileOpen() and load your document through Serialize().

Questions ?

mosaicerAuthor Commented:
Well, I'm not using serialize, so
I would have to write routines for
that.  I don't have a totally clear
understanding of how exactly that
would work.

I have a specialized file structure
to be loaded in that is generated
by another program.  This structure
doesn't store all of the document
parameters that serialize needs
(I think?... Like I say, I don't
have a good understanding of

One other hitch is that I'm actually
loading in three files, 1 text file
which then gives me two other filenames
of files that need to be loaded.
These files are loaded with some
third party libraries (a special
variant of TIFF) and I then
process the resulting data and
store it in my document as a Device
Independent Bitmap.  As the data
structures (aerial imagery) are already
quite large (our machines are
requiring up to 2 GIGs of RAM!),
I don't want to keep extra data
around (ie. the original files
that were loaded) as that would bump
up the requirements even more.  So I suspect that this adds more complications to the Serialize implementations.

I'm sure that it is easily doable if
you know what you're doing :)
but unfortunately, being a novice,
I'd rather do things the easy way.
Since I've already implemented an
OnFileSave that works, it would
be nice to just use that one.

Thanks you very much for your
suggestions though.

Are you using Visual C++ 6 ?
if you make a normal application
the first command line would automatically be opened in the Document classes. Busy rite now so cannot give you a more complete answer


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.