Solved

How do I get the access mode that was used to obtain a pariticular file (or file handle)?

Posted on 2004-07-31
2
343 Views
Last Modified: 2010-04-17
I am trying to write a generic file read programe that accept either a CFile or a file handle.
However, when testing it, I sometimes get error because the file was open in an unexpected mode (like write only etc), hence, I would like to examine the CFile or file handle that is passed to the me and see if it is open appropriately. However, I find that there is NO way to examine the way the file was opened (like with 'r' or 'w' etc).
Can any one tell me if there is a way ( as easy as possible) to examine the mode that was used to open this particular file? to avoid errors.

Thank you
0
Comment
Question by:hshliang
2 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11685759
As you said, there is no way, notice that CFile is an encapsulation of a WinAPI's HANDLE to a file, so both are identical.
WinAPI function GetFileInformationByHandle() doesn't tell you about file access mode, and CFile doesn't save extra information about it, even in its protected members.
0
 
LVL 8

Accepted Solution

by:
adg080898 earned 125 total points
ID: 11692170
You could subclass CFile to intercept the information you desire and store it in your subclass members. This would require a search-and-replace for CFile to whatever you name the subclass, but actual code would not need to be touched.

Something like this:

class MyCFile : public CFile {
protected:
    UINT nMyOpenFlags;
public:
    MyCFile(LPCTSTR pFilename, UINT nOpenFlags);
    BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);

    UINT GetOpenFlags() { return nMyOpenFlags; }
};

MyCFile::MyCFile(LPCTSTR pFilename, UINT nOpenFlags) : CFile(pFilename, nOpenFlags)
{
    nMyOpenFlags = nOpenFlags;
}

BOOL MyCFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL)
{
    nMyOpenFlags = nOpenFlags;

    return ((CFile*)this)->Open(lpszFileName, nOpenFlags, pError);
}

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

777 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