Solved

how can i set a file locked?

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host to IP 7 78
not able to insert into temp table 68 162
ASCII Non-Printable characters/codes and their HTML equivalents 6 115
matchUp  challenge 9 110
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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