title: I found this code to get windows write privileges but it does not working
body: <p>Im trying to create new file on D: drive with c/c++</p>
  
  <p>I found this code to get windows write privileges but it does not working</p>
  
  <p>Can anybody help me i am new in c++?</p>
  
  <pre><code>BOOL SetPrivilege(
      HANDLE hToken,               // access token handle
      LPCTSTR lpszPrivilege,    // name of privilege to enable/disable
      BOOL bEnablePrivilege    // to enable (or disable privilege)
      )
  
  {
      // Token privilege structure
      TOKEN_PRIVILEGES tp;
      // Used by local system to identify the privilege
      LUID luid;
  
      if(!LookupPrivilegeValue(
          NULL,                // lookup privilege on local system
          lpszPrivilege,    // privilege to lookup
          &amp;luid))               // receives LUID of privilege
      {
          printf("LookupPrivilegeValue() error: %u\n", GetLastError());
          return FALSE;
      }
      else
          printf("LookupPrivilegeValue() is OK\n");
  
      tp.PrivilegeCount = 1;
      tp.Privileges[0].Luid = luid;
  
      // Don't forget to disable the privileges after you enabled them,
      // or have already completed your task. Don't mess up your system :o)
      if(bEnablePrivilege)
      {
          tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
          printf("tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED\n");
      }
      else
      {
          tp.Privileges[0].Attributes = 0;
          printf("tp.Privileges[0].Attributes = 0\n");
      }
      // Enable the privilege (or disable all privileges).
      if(!AdjustTokenPrivileges(
          hToken,
          FALSE, // If TRUE, function disables all privileges, if FALSE the function modifies privilege based on the tp
          &amp;tp,
          sizeof(TOKEN_PRIVILEGES),
          (PTOKEN_PRIVILEGES) NULL,
          (PDWORD) NULL))
      {
          printf("AdjustTokenPrivileges() error: %u\n", GetLastError());
          return FALSE;
      }
      else
      {
          printf("AdjustTokenPrivileges() is OK, last error if any: %u\n", GetLastError());
          printf("Should be 0, means the operation completed successfully = ERROR_SUCCESS\n");
      }
      return TRUE;
  }
  </code></pre>
  
  <p>my Main Function</p>
  
  <pre><code>int main()
      {
      LPCTSTR lpszPrivilege = L"SeSecurityPrivilege";
      // Change this BOOL value to set/unset the SE_PRIVILEGE_ENABLED attribute
      BOOL bEnablePrivilege = TRUE;
      HANDLE hToken;
      // Open a handle to the access token for the calling process. That is this running program
      if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &amp;hToken))
      {
          printf("OpenProcessToken() error %u\n", GetLastError());
          return FALSE;
      }
      else
          printf("OpenProcessToken() is OK\n");
  
      // Call the user defined SetPrivilege() function to enable and set the needed privilege
      BOOL test = SetPrivilege(hToken, lpszPrivilege, bEnablePrivilege);
      printf("The SetPrivilege() return value: %d\n\n", test);
  
          ofstream myFile;
          myFile.open("C:\\test.txt");
          myFile &lt;&lt; "I am C";
          myFile.close();
  
  
          bEnablePrivilege = FALSE;
          BOOL test1 = SetPrivilege(hToken, lpszPrivilege, bEnablePrivilege);
          printf("The SetPrivilage() return value: %d\n", test1);
          system("PAUSE");
  
          return 0;
      }
  </code></pre>
  
  <p>the output in console looks like this:</p>
  
  <pre><code>OpenProcessToken() is OK
  LookupPrivilegeValue() is OK
  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED
  AdjustTokenPrivileges() is OK, last error if any: 1300
  Should be 0, means the operation completed successfully = ERROR_SUCCESS
  The SetPrivilege() return value: 1
  
  LookupPrivilegeValue() is OK
  tp.Privileges[0].Attributes = 0
  AdjustTokenPrivileges() is OK, last error if any: 1300
  Should be 0, means the operation completed successfully = ERROR_SUCCESS
  The SetPrivilage() return value: 1
  Press any key to continue . . .
  </code></pre>