Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:56:10+00:00 2026-06-06T18:56:10+00:00

Im trying to create new file on D: drive with c/c++ I found this

  • 0

Im trying to create new file on D: drive with c/c++

I found this code to get windows write privileges but it does not working

Can anybody help me i am new in c++?

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
        &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
        &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;
}

my Main Function

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, &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 << "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;
    }

the output in console looks like this:

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 . . .
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:56:12+00:00Added an answer on June 6, 2026 at 6:56 pm
    1. SeSecurityPrivilege is the “Manage auditing and security log” user right (see the list of privilege constants). It has absolutely nothing to do with writing files. In fact, under normal circumstances, you don’t need to enable any privilege to write a file to the root of a drive, although the process does need to be running as an administrator.

    2. Error 1300 means “Not all privileges or groups referenced are assigned to the caller.” That is, the privilege was not successfully enabled, because the process isn’t entitled to it. This will be because the process isn’t being run as an administrator.

    So, first, you can remove almost all the code in your example, everything but the four lines that actually write the file. Then you just need to run the application as an administrator.

    To do this, right-click on the executable file and select “Run as administrator”. If you run the application in this way it will be able to write the file. (Note: in Windows XP, you don’t need to do this, but you do need to be logged in as a user with administrative rights.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a new log file every hour with the following code
I am trying to create a file using File newFile = new File(myFile); However
I am trying to create a new Element in my javascript code and append
I am trying to create new Event objects to be persisted in the database
I am trying to create new rules profile in Sonar 2.9 with my checkstyle
When trying to create a new team project with any of the default project
I'm trying to create a new object of type T via its constructor when
I'm trying to create a new project in NetBeans PHP from existing sources. When
I am trying to create a new dict using a list of values of
I am trying to create a new branch using the API, and have used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.