Question

I am using Windows CE 4.2 and MS Embedded VC++ 4.0. The following code gives me the error Access to [file name] was denied., and it creates the file but does not write anything to it.

CString tmp;
tmp.Format(_T("%s%d"), mFileName, ++ctr);
TRY 
{
  mFile.Open(tmp, CFile::modeCreate);
  mFile.Write(&data[ctr%2], 1);
  mFile.Close();
}
CATCH (CException, e)
{
  TCHAR szCause[255];
  CString strFormatted;

  e->GetErrorMessage(szCause, 255);
  strFormatted += szCause;
  AfxMessageBox(strFormatted);
}
END_CATCH

Interestingly, using CreateFile works fine:

CString tmp;
tmp.Format(_T("%s%d"), mFileName, ++ctr);

hFile = CreateFile(tmp, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL /*| FILE_FLAG_WRITE_THROUGH*/, 0);
WriteFile(hFile, &(data[ctr%2]), 1, &bytesWritten, NULL);
CloseHandle(hFile);

Why could this be? Can I even use CFile on WinCE? I'm just starting out with embedded development.

Was it helpful?

Solution

I've not used MFC on Windows CE, but on ordinary Windows, the usual idiom is

  mFile.Open(tmp, CFile::modeCreate|CFile::modeWrite);

i.e. try adding "CFile::modeWrite" to the constructor. The MSDN documentation suggests that this is necessary:

You can combine options listed below by using the bitwise-OR (|) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional.

This implies to me that one of "modeRead" or "modeWrite" must always be supplied.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top