Windows: How do I get the mode/access rights of an already opened file?

后端 未结 3 1974
旧时难觅i
旧时难觅i 2020-12-06 15:53

I am writing a program that is using a database library. The library provides me access to the file handle it uses to access my table. I\'ve found a windows API that allows

相关标签:
3条回答
  • Figured it out maybe? From my reading of the docs, it seems you might be able to discern the original "share mode" of an open handle by calling ReOpenFile with dwDesiredAccess of 0 and dwShareMode for one (or more?) of the modes to query, i.e. FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE. If ReOpenFile returns a valid handle--which you can then close/dispose--then that sharing mode was present on the original file?

    Source:

    "If dwDesiredAccess is zero (0), the application can query device attributes without accessing the device. This is useful if an application wants to determine the size of a floppy disk drive and the formats it supports without requiring a floppy in the drive."
    https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-reopenfile

    0 讨论(0)
  • 2020-12-06 16:03

    OK. So I finally got this part all figured out - thanks to your direction, a lot of MSDN study and a LOT of trial and error.

    There were a couple of tricky points to getting this all figured out.

    1) The ACCESS_MASK certainly did not reflect the access modes as I expected. The documentation led me to expect the upper 4 bits to reflect the "GENERIC" modes that I opened the file with - wrong! Those show up in the Specific Rights section. Heck - I still don't have a clue when those upper 4 bits actually get used, but for this exercise I don't need to.

    2) Once I got THAT clear in my mind I had to stumble across documentation that let me know that when I opened with GENERIC_READ that was translated into:

    FILE_GENERIC_READ

    which is made up of:

    STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE

    Understanding this concept made all the rest of it fall into place. Now my understanding correlated with the information that Process Hacker was telling me.

    3) I also had a serious error going from my UI to the code (read one constant misplaced), which once figured out made the whole world lay down before my feet.

    This is great because I can now figure out what Access Modes were used when the file was opened. Original question answered!

    Now I would like to be able to determine the "share" mode the file is in - if possible. Any ideas?

    Thanks again for your help

    0 讨论(0)
  • 2020-12-06 16:05

    I don't know if there is a corresponding Win32 API for this, but if you really need it you can call NtQueryObject(ObjectBasicInformation).

    0 讨论(0)
提交回复
热议问题