RESOLUTION
Derive a class from CDocManager and override the OpenDocumentFile function
by copying the existing code and make some small changes. Because this fix
is dependent on the internal MFC implementation, make sure you include the
#if _MFC_VER == 0x400 statements.
Copy the following code into your application's .cpp file above the
implementation of InitInstance. If you're looking at a paper copy of this
article, note that you can find most of the code for OpenDocumentFile in
Mfc\Src\Docmgr.cpp; rather than retyping the entire text, you could copy
most of the code and make the change noted below:
#if _MFC_VER == 0x400
static inline BOOL IsDirSep(TCHAR ch)
{
return (ch == _T('\\') || ch == _T('/'));
}
#ifndef _MAC
BOOL AFXAPI AfxResolveShortcut(CWnd* pWnd, LPCTSTR pszShortcutFile,
LPTSTR pszPath, int cchPath);
#endif
#define _countof(array) (sizeof(array)/sizeof(array[0]))
BOOL AFXAPI AfxFullPath(LPTSTR lpszPathOut, LPCTSTR lpszFileIn);
void AFXAPI AfxGetRoot(LPCTSTR lpszPath, CString& strRoot);
class CMyDocManager : public CDocManager
{
public:
CMyDocManager() {};
virtual ~CMyDocManager() {};
virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName);
};
CDocument* CMyDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
// find the highest confidence
POSITION pos = m_templateList.GetHeadPosition();
CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
CDocTemplate* pBestTemplate = NULL;
CDocument* pOpenDocument = NULL;
TCHAR szPath[_MAX_PATH];
ASSERT(lstrlen(lpszFileName) < _countof(szPath));
#ifndef _MAC
TCHAR szTemp[_MAX_PATH];
if (lpszFileName[0] == '\"')
++lpszFileName;
lstrcpyn(szTemp, lpszFileName, _MAX_PATH);
LPTSTR lpszLast = _tcsrchr(szTemp, '\"');
if (lpszLast != NULL)
*lpszLast = 0;
AfxFullPath(szPath, szTemp);
TCHAR szLinkName[_MAX_PATH];
// CHANGE: check the windows version before calling AfxResolveShortcut
if((BYTE)GetVersion() >=4 && AfxResolveShortcut(AfxGetMainWnd(),
szPath, szLinkName,_MAX_PATH))
lstrcpy(szPath, szLinkName);
#else
lstrcpyn(szPath, lpszFileName, _MAX_PATH);
WIN32_FIND_DATA fileData;
HANDLE hFind = FindFirstFile(lpszFileName, &fileData);
if (hFind != INVALID_HANDLE_VALUE)
VERIFY(FindClose(hFind));
else
fileData.dwFileType = 0; // won't match any type
#endif
while (pos != NULL)
{
CDocTemplate* pTemplate =
(CDocTemplate*)m_templateList.GetNext(pos);
ASSERT_KINDOF(CDocTemplate, pTemplate);
CDocTemplate::Confidence match;
ASSERT(pOpenDocument == NULL);
#ifndef _MAC
match = pTemplate->MatchDocType(szPath, pOpenDocument);
#else
match = pTemplate->MatchDocType(szPath,
fileData.dwFileType, pOpenDocument);
#endif
if (match > bestMatch)
{
bestMatch = match;
pBestTemplate = pTemplate;
}
if (match == CDocTemplate::yesAlreadyOpen)
break; // stop here
}
if (pOpenDocument != NULL)
{
POSITION pos = pOpenDocument->GetFirstViewPosition();
if (pos != NULL)
{
CView* pView = pOpenDocument->GetNextView(pos);
// get first one
ASSERT_VALID(pView);
CFrameWnd* pFrame = pView->GetParentFrame();
if (pFrame != NULL)
pFrame->ActivateFrame();
else
TRACE0(
"Error: Cannot find frame for document to activate.\n");
CFrameWnd* pAppFrame;
if (pFrame !=
(pAppFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd))
{
ASSERT_KINDOF(CFrameWnd, pAppFrame);
pAppFrame->ActivateFrame();
}
}
else
{
TRACE0(
"Error: Can not find view for document to activate.\n");
}
return pOpenDocument;
}
if (pBestTemplate == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
return NULL;
}
return pBestTemplate->OpenDocumentFile(szPath);
}
#endif //mfc version 4.0 workaround
Now, include the following three lines to the first part of your
MyApp::InitInstance function:
#if _MFC_VER == 0x400
m_pDocManager = new CMyDocManager;
#endif