Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I open an existing file with the stock dialog in an MDI I can see lpszFileName is evaluated in the following function . After that I don't know where to look to set a pointer to the CFile.
C++
Document* CMFCApplication3App::OpenDocumentFile(LPCTSTR lpszFileName)
{
    // TODO: Add your specialized code here and/or call the base class
    
    return CWinApp::OpenDocumentFile(lpszFileName);

}

When I step through the code I never enter CDocument::OnOpenDocument. When I try to retrieve data from the file with CDocument::Serialize it eventually fails with an error box saying "Failed to open document". I don't understand why the process never enters CDocument::OnOpenDocument.

What I have tried:

The serialize trail from CDocument to the class where my data is held in CArray is as follows:
C++
void TaxonTreeDoc::Serialize(CArchive& ar)
{
	Taxa.Serialize(ar);
}

void CTaxon::Serialize(CArchive& archive)
{
	CObject::Serialize(archive);
	if (archive.IsStoring())
		archive << Rank << TName << TParent << IntegumentFileName << ExtCharFileName;
	else
		archive >> Rank >> TName << TParent << IntegumentFileName << ExtCharFileName;
	int i = 1;//test code for break point
}

typedef CArray<ctaxon, ctaxon&> TaxonArray; //dynamic array of taxa

// CArray serialization helper specialized for the CTaxon class

template<> inline void AFXAPI SerializeElements<ctaxon>(CArchive& ar, CTaxon *taxa, INT_PTR nCount)
{

	for (int i = 0; i < nCount; i++, taxa++)
	{
		// Serialize each taxon object
		taxa[i].Serialize(ar);
		//taxa->Serialize(ar);
	}
}

Note I commented out "taxa->Serialize(ar);" and replaced it with and index pointer "taxa[i].Serialize(ar);". However the CFile was, also, failing before this change.
Posted
Updated 7-Sep-20 7:45am
v4
Comments
Richard MacCutchan 26-Aug-20 13:52pm    
Where are you calling OpenDocumentFile from?
Member 14855755 26-Aug-20 16:59pm    
It is one of the virtual functions in CMFCApplication3App which is my class derived from CWinApp. I added it in order to trace the path of opening a CDocument from a CDocumentTemplate. That was where I found the file name selected in the standard open file dialog. I assumed somewhere further on in the creation of CDocument, CDocument::OnOpenDocument would be called, but it never happens.
Richard MacCutchan 27-Aug-20 3:35am    
I don't understand. If you have created a function, then you must call it from somewhere with a filename as the parameter. I am not actually sure what piece of information you cannot access.
Member 14855755 27-Aug-20 9:33am    
These virtual functions are standard parts of MFC MDI. I have simply inherited them into my classes. They are called by the MDI chain of process.
Richard MacCutchan 27-Aug-20 10:27am    
But you said above that it is not being called. I suspect there is something missing if that is the case.

1 solution

You need to call
OpenDocumentFile
and the return value is the handle you are looking for.


C++
CMFCApplication3App *myApp;
Document *myDoc = myApp->OpenDocumentFile(_T("myfilename.txt"));


Then, myDoc should hold the handle to the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900