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 7011941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:11:28+00:00 2026-05-27T22:11:28+00:00

My app needs to do some privileged operations at runtime. For example,when the user

  • 0

My app needs to do some privileged operations at runtime. For example,when the user is first time to run my app, i have to create and format a virtual dirve. I use the undocumented api formatex to do the fomating job, but formatex need administrator privileges. If OS is vista or later, i can prompt UAC dialog with ‘COM Elevation Moniker’ and it works fine. But on xp, this technique is not suitable, so i use the impersonate method.
if app is run as limited user, i do formating like this:

CredUIPromptForCredentials() -> prompt to get administrator credentials
LogonUser()
ImpersonateLoggedOnUser()
formatex()
RevertToSelf()

formatex still fail…
Of course, run my app as administrator will works, but it is not good, my app is installed as per user, not per machine, which should work under current user context, even if the user is a
limited user.

How can i correctly elevate my app at runtime to do the formating job? Any one help?

  • 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-05-27T22:11:29+00:00Added an answer on May 27, 2026 at 10:11 pm

    A possible solution is to re-execute the application with elevated privileges and some command line parameter which will instruct to perform actual formatting.

    Example code:

    if (!IsUserAdmin()) {
         RunAsAdmin(hwnd, exeName, "--do-format");
    } else {
         DoFormat();
    }
    ...
    
    BOOL RunAsAdmin(HWND hWnd, LPCTSTR lpFile, LPCTSTR lpParameters)
    {
        SHELLEXECUTEINFO sei;
        ZeroMemory(&sei, sizeof(sei));
    
        sei.cbSize          = sizeof(SHELLEXECUTEINFOW);
        sei.hwnd            = hWnd;
        sei.fMask           = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
        sei.lpVerb          = _TEXT("runas");
        sei.lpFile          = lpFile;
        sei.lpParameters    = lpParameters;
        sei.nShow           = SW_SHOWNORMAL;
    
        if (!ShellExecuteEx(&sei)) {
                return FALSE;
        }
        return TRUE;
    }
    
    BOOL IsUserAdmin(VOID)
    {
        BOOL b;
        SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY };
        PSID AdministratorsGroup;
        b = AllocateAndInitializeSid(
                &NtAuthority,
                2,
                SECURITY_BUILTIN_DOMAIN_RID,
                DOMAIN_ALIAS_RID_ADMINS,
                0, 0, 0, 0, 0, 0,
                &AdministratorsGroup);
        if (b) {
                if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) {
                        b = FALSE;
                }
                FreeSid(AdministratorsGroup);
        }
    
        return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app needs to display the process time of some operations. One of the
My Cocoa app needs some small dynamically generated windows. How can I programmatically create
I have a MySQL/Rails app that needs search. Here's some info about the data:
In an iPhone app I'm working on, the user needs to enter some configuration
I am writing my first rails app. It needs to aggregate some data from
I have an app which needs administrator privileges to do some things. I'd like
I am currently developing a Silverlight 3 app that needs some sort of user
my app needs to download some data (about 50k) on launch time. I'm currently
When I have a WinForms app that needs to do some heavy lifting in
I'm developing a silverlight app which needs to create some files in the hosting

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.