Tom Tom
Tom Tom

Reputation: 1209

CDocument::SetPathName(..) crash in VS2015

I upgrade an App from VS2008 to VS2015.

I get an Exception, which I assume is triggered from

/*virtual*/ BOOL CMyAppDoc::SaveModified()
{
  if (!IsModified())
    return TRUE;        // nothing to do

  CString str = GetPathName();
  SetPathName(str, TRUE);   // assign a PathName and add to MRU !
  return OnSaveDocument(str);           

}

The ErrorBox shows: "Encountered an inproper argument".

Upvotes: 2

Views: 778

Answers (2)

thomiel
thomiel

Reputation: 2937

The proper way would rather be to call SetPathName(str, /*bAddToMRU=*/FALSE) so that CRecentFileList::Add() won't run into the ENSURE(SUCCEEDED(hr)) exception when the file doesn't already exist. Consider this as "just providing a default path".

Don't worry about the MRU list not being uptdated: The subsequent OnSaveDocument() will call SetPathName(str, TRUE) after the document is actually serialized to the file.

Upvotes: 2

Tom Tom
Tom Tom

Reputation: 1209

I investigated further, the error comes from

void CRecentFileList::Add(LPCTSTR lpszPathName, LPCTSTR lpszAppID)
{
  ..
  hr = _AfxSHCreateItemFromParsingName(lpWPath, NULL, IID_IShellItem,LPVOID*)&psi);
  ENSURE(SUCCEEDED(hr));
  ..

}

hr Errorcode is 2, which means ERROR_FILE_NOT_FOUND : The system cannot find the file specified.

The workaround is first save the file with OnSaveDocument(..), then call SetPathName(..)

Upvotes: 2

Related Questions