Solved

Check directory for write access by current user

Posted on 2008-10-01
4
2,584 Views
Last Modified: 2013-11-20
How do use MFC to check if the current user has write access to a directory?
0
Comment
Question by:steenpat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 22618586
You can use the following code:
#include <windows.h>
#include <svrapi.h>
#include <stdio.h>
 
BOOL IsAccessPermitted(LPCTSTR pszFolder) {
 
          HANDLE hToken;
	  // AccessCheck() variables
	  DWORD           dwAccessDesired;
	  PRIVILEGE_SET   PrivilegeSet;
	  DWORD           dwPrivSetSize;
	  DWORD           dwAccessGranted;
	  BOOL            fAccessGranted = FALSE;
	  GENERIC_MAPPING GenericMapping;
 
          SECURITY_INFORMATION si = (SECURITY_INFORMATION)( OWNER_SECURITY_INFORMATION
            | GROUP_SECURITY_INFORMATION
            | DACL_SECURITY_INFORMATION);
 
 
	  PSECURITY_DESCRIPTOR psdSD = NULL;
	  DWORD dwNeeded;
 
	  GetFileSecurity(pszFolder,si,NULL,0,&dwNeeded);
 
 
	  psdSD = (PSECURITY_DESCRIPTOR) new BYTE[dwNeeded];
 
	  if (!GetFileSecurity(pszFolder,si,psdSD,dwNeeded,&dwNeeded)) {
 
              printf("GetFileSecurity\n");
          }
 
      // AccessCheck() requires an impersonation token.
      ImpersonateSelf(SecurityImpersonation);
 
      OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, 
            &hToken);
 
      // Using AccessCheck(), there are two different things we could do:
      // 
      // 1. See if we have Read/Write access to the object.
      // 
      dwAccessDesired = ACCESS_READ;
 
      // Initialize generic mapping structure to map all.
      memset(&GenericMapping, 0xff, sizeof(GENERIC_MAPPING));
      GenericMapping.GenericRead = ACCESS_READ;
      GenericMapping.GenericWrite = ACCESS_WRITE;
      GenericMapping.GenericExecute = 0;
      GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
 
      // This only does something if we want to use generic access
      // rights, like GENERIC_ALL, in our call to AccessCheck().
      MapGenericMask(&dwAccessDesired, &GenericMapping);
 
      dwPrivSetSize = sizeof(PRIVILEGE_SET);
 
      printf("calling 'AccessCheck()'...\n");
 
      // Make the AccessCheck() call.
      if (!AccessCheck(psdSD, hToken, dwAccessDesired, &GenericMapping,
            &PrivilegeSet, &dwPrivSetSize, &dwAccessGranted, 
            &fAccessGranted)){
 
              printf("AccessCheck: %d\n",GetLastError());
          }
 
      printf("... 'AccessCheck()' succeeded\n");
      delete [] psdSD;
 
  return fAccessGranted;
}
 
void main () {
 
    BOOL bRes = IsAccessPermitted("c:\\Windows\\notepad.exe");
 
    printf("permitted: %s\n", bRes ? "Yes" :  "No");
 
}

Open in new window

0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 250 total points
ID: 22618636
Quick and dirty method would be to just create a file in the directory. If it succeeds, you have write access; if not, you don't.

CString mydir = "c:/windows/system32";

CString tempName = mydir + "/mytemp.foo";
CFile tempFile;

BOOL bOpenOK = tempFile.Open(tempName, CFile::modeCreate | CFile::modeWrite);
if (bOpenOK == TRUE) {
  // success! clean up.
  tempFile.Close();
  CFile::Remove(tempName);
}

0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22623330
try to create a directory using CreateDirectory of try to create a file using CreateFile API function.
if one of above functions failed, check error reason using GetLastError() API to get error code.
if(GetLastError() == ERROR_ACCESS_DENIED) // or if(GetLastError() == 5)
//means that access denied
as "Wayside"s comment: quick and dirty way ;)
Regards
0
 

Author Comment

by:steenpat
ID: 22996449
I would like to comment that RevertToSelf is necessary at the bottom of this function otherwise it keeps running in the context of the client and this is undesirable. It caused issues with my application until I figured this out. I called RevertToSelf, and then CloseHandle(hToken) and this resolved issues.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sorting efficency of sorting algorithm 30 151
Header of docx file 17 145
what type of pointer is this pointer? 3 32
visual C++ 1 43
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 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