Avatar of tantormedia
tantormedia
Flag for United States of America asked on

MFC: OnFileOpen problem

Dear experts,

I have an MFC application. My application class is derived from CWinApp. It implements OnFileOpen(). When the user selects File -> Open, this method is called. But my users usually open documents in another way: they just drag a document file to the .exe file, and the document opens with my program. But this way, OnFileOpen() is not called. Where should I place my code then to make sure it is called in any scenario?
Thanks.
C++

Avatar of undefined
Last Comment
tantormedia

8/22/2022 - Mon
jkr

'OnDropFiles()' is the place for that, see http://msdn.microsoft.com/en-us/library/62zys01d%28v=VS.100%29.aspx and http://www.codeproject.com/KB/dialog/JibDragDrop.aspx ("Drag and Drop in a Dialog") for an example.
tantormedia

ASKER
Thank you for your answer. But I am not sure this is what I need. The user doesn't drop anything over a window, he drops the document file over the program's executable in Windows Explorer.
ASKER CERTIFIED SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
tantormedia

ASKER
The problem is that I need my code to be executed after opening document, not on opening the program istself.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jkr

Well, in this case here, 'opening the document' and 'opening the program' are the same thing.
tantormedia

ASKER
But I can e.g. open the program without opening a document.  Then do File->Open.
jkr

Yes, that's clear. But when a file is dropped on your executable in explorer, this place is what opens the file after your app has been started.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
tantormedia

ASKER
You see, what the code should do, depends on version of the document that is being opened. So if I put my code in InitInstance(), and then open document with File->Open, the code will not be executed again. And placing it in document's OnOpenDocument() doesn't work for me, either...
tantormedia

ASKER
This is the code I want to execute after opening document. You will see why it probably will not work from InitInstance:
if(FILE_VERSION > ((CBookDoc*)pDoc)->GetFileWriteVersion())
		{
			if(AfxMessageBox("This .bok file was created using an older version of the Book Program.\r\n"
				"Would you like to save it now with a different name to have all new features available?",
				MB_YESNO|MB_ICONQUESTION) == IDYES)
			{
				((CBookDoc*)pDoc)->DoFileSaveAs(FALSE, false);
			}
		}

Open in new window

jkr

Well, if you are doing that via serialization, you can get the version via 'CArchive::GetObjectSchema()', e.g.
IMPLEMENT_SERIAL(CMyObject, CObject, VERSIONABLE_SCHEMA|1)

void CMyObject::Serialize(CArchive& ar) 
{
   if (ar.IsLoading())
   {
      int nVersion = ar.GetObjectSchema();

      switch(nVersion)
      {
      case 0:
         // read in previous version of 
         // this object
         break;
      case 1:
         // read in current version of
         // this object
         break;
      default:
         // report unknown version of 
         // this object
         break;
      }
   }
   else
   {
      // Normal storing code goes here
   }

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
tantormedia

ASKER
No, I am afraid, I am not doing this via serialization.
jkr

Well, then how are you doing it?
tantormedia

ASKER
Doing what exactly?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
tantormedia

ASKER
Thank you for your help. I placed my code in a separate method, and call it from InitInstance() as you said with a condition if a document is already open (in case it was dragged over the exe), and also I call it from OnFileOpen()  after opening the file.