Solved

how can i set a file locked?

Posted on 2002-06-25
5
262 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 60 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now