Check directory for write access by current user

Posted on 2008-10-01
Last Modified: 2013-11-20
How do use MFC to check if the current user has write access to a directory?
Question by:steenpat
LVL 86

Accepted Solution

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;





	  DWORD dwNeeded;


	  psdSD = (PSECURITY_DESCRIPTOR) new BYTE[dwNeeded];

	  if (!GetFileSecurity(pszFolder,si,psdSD,dwNeeded,&dwNeeded)) {



      // AccessCheck() requires an impersonation token.


      OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, 


      // 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, 


              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

LVL 14

Assisted Solution

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 + "/";
CFile tempFile;

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

LVL 17

Expert Comment

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 ;)

Author Comment

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.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now