0

i am working on a visual studio project in windows. i want to check what privileges a file has (special write), for now i am using :

hFile = CreateFile(wtext,           // name of the write
            GENERIC_WRITE,          // open for writing
            0,                      // do not share
            NULL,                   // default security
            OPEN_EXISTING,          // open only if exists
            FILE_ATTRIBUTE_NORMAL,  // normal file
            NULL);                  // no attr. template

i dont want to use CreateFile because for some files i need to run my prog as Administrator to get the results.

how do i check file access details without using CreateFile?

Alex
  • 11,115
  • 12
  • 51
  • 64
Omega Doe
  • 325
  • 1
  • 2
  • 17
  • 1
    Your code doesn't check privileges. It opens a file. What are you actually trying to do? – David Heffernan Mar 11 '15 at 12:50
  • i know, i want to check write privileges on a file and because i dont know how to do it , i use the CreateFile function with the generic_write and open_existing and after use if (hFile == INVALID_HANDLE_VALUE) to manipulate and see if i had write privileges – Omega Doe Mar 11 '15 at 12:56
  • Define what you mean by "check write privileges". Do you mean whether or not the current process user token has sufficient rights to perform that action? – David Heffernan Mar 11 '15 at 13:01
  • If the user session has write privileges on a file. for example, some dll in c:\program files has RW access to SERVICE\TrustedInstaller while BUILTIN\Users has only R access – Omega Doe Mar 11 '15 at 13:03

1 Answers1

0

Why not checking if the user has Administrator rights ?

BOOL CYourClass::IsElevated(void)
{
    BOOL ret = FALSE;
    HANDLE hToken = nullptr;
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
    {
        TOKEN_ELEVATION Elevation = {0};
        DWORD dwSize = sizeof(TOKEN_ELEVATION);
        if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &dwSize))
        {
            ret = Elevation.TokenIsElevated;
        }
    }
    if (hToken)
        CloseHandle(hToken);
    return ret;
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53