Link to home
Start Free TrialLog in
Avatar of mosaicer
mosaicer

asked on

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
program.

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

Avatar of Zoppo
Zoppo
Flag of Germany image

By default command line parameters are handled in the InitInstance function of your CWinApp-derived class with following code:

CMyApp::InitInstance()
{
 ...
 CCommandLineInfo cmdInfo;
 ParseCommandLine(cmdInfo);
 ProcessShellCommand(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,

ZOPPO
Avatar of mosaicer
mosaicer

ASKER

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)
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;
 break;
....
}

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 ?

ZOPPO
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
serialize).

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.




ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Regards,

hh