Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how can i set a file locked?

Posted on 2002-06-25
5
Medium Priority
?
337 Views
Last Modified: 2013-11-20
when i open a file in my program, i could delete the file when it opened.I want to lock it. what can I do?
0
Comment
Question by:quace
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7107013
With MFC's CFile class you lock a file with the constructor or the Open member function:

CFile x(lpszFilename, CFile::modeWrite | CFile::shareExclusive);

the CFile::shareExclusive flag locks the file for EXCLUSIVE access to this opening.
0
 

Author Comment

by:quace
ID: 7109473
Could you tell me how to do in mfc framework?
I use CDocument::OnOpenDocument()
0
 
LVL 32

Expert Comment

by:jhance
ID: 7110137
Believe it or not, CFile _IS_INDEED_ an MFC class.

Beyond that, I'm really don't know what you mean by "do in mfc framework", please explain.
0
 

Author Comment

by:quace
ID: 7112504
doc/view architecture
0
 
LVL 1

Accepted Solution

by:
jianliuer earned 240 total points
ID: 7116174
hi,quace


pls step into BOOL CDocument::OnOpenDocument(...)

and u can find following lines:

....
          CWaitCursor wait;
          if (pFile->GetLength() != 0)
               Serialize(loadArchive);     // load me
          loadArchive.Close();
          ReleaseFile(pFile, FALSE);

....


the file will be closed after u call the default implement CDocument::OnOpentDocument(...).So u can try this:


CYOURDocument::OnOpenDocument(...)
{
if(m_pFile != NULL)
{
 ReleaseFile(pFile, FALSE);
}
 CDocument::OnOpenDocument(...);
 m_pFile = GetFile(lpszPathName,
 CFile::modeRead|CFile::shareDenyWrite, &fe);
 if (m_pFile == NULL)
     {
          ReportSaveLoadException(lpszPathName, &fe,
               FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
          return FALSE;
     }
 ....
}

CYOURDocument::CYOURDocument()
{
m_pFile = NULL;
}

CYOURDocument::~CYOURDocument()
{
if(m_pFile != NULL)
{
 ReleaseFile(pFile, FALSE);
}
}

where m_pFile is a class member of CYOURDocument CFile* m_pFile.

GOOD LUCK!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question