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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:45:55+00:00 2026-06-14T22:45:55+00:00

I am working on an application that will trigger a UAC prompt in whatever

  • 0

I am working on an application that will trigger a UAC prompt in whatever program is opened by ShellExecute.

I can’t figure out how to hard-code a path for the ShellExecute to run. As of now this program uses whatever path is in arg[0]. How can I build a string to put in the place of arg[0] on the line sinfo.lpFile = arg[0];?

I am very new so if you can’t see why making a string to put in that line will solve my problem then you are most likely right.

#include "stdafx.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <shellapi.h>
#include <process.h>

#include "uac-example.h"

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst,LPSTR cmdLine, int nCmdShow){
    LPWSTR *arg;
    int argc = 0;
    HRESULT ret = SUCCESS;
    WCHAR imagePath[MAXPATHLEN];
    WCHAR workingDir[MAXPATHLEN];
    WCHAR uacDir[MAXPATHLEN];
    WCHAR uacRunningLockFilePath[MAXPATHLEN];
    HANDLE uacRunningLockFileHandle = INVALID_HANDLE_VALUE;
    WCHAR elevatedLockFilePath[MAXPATHLEN];
    HANDLE elevatedLockFileHandle = INVALID_HANDLE_VALUE;
    arg = CommandLineToArgvW(GetCommandLineW(),&argc);
    //if(arg == NULL || argc < 2) {
    //  ERRORBOX("Missing required program arguments.\n\nUsage:\nuac-example.exe <working directory>");
    //  return FAILURE;
    //}
    GetModuleFileName(NULL, imagePath, MAXPATHLEN);
    arg[0] = imagePath;
    wcscpy_s((wchar_t *)uacDir, MAXPATHLEN, arg[1]);
    _snwprintf_s(uacRunningLockFilePath, MAXPATHLEN, MAXPATHLEN,
                    _T("%s/") _T(RUNNING_LOCK_FILE), uacDir);
    wcscpy_s(workingDir, MAXPATHLEN, imagePath);
    WCHAR *slash = wcsrchr(workingDir, '\\');
    wcscpy_s(slash, MAXPATHLEN, _T(""));
    _snwprintf_s(elevatedLockFilePath, MAXPATHLEN, MAXPATHLEN,_T("%s/") _T(ELEVATE_LOCK_FILE), workingDir);
    uacRunningLockFileHandle = CreateFileW(uacRunningLockFilePath,(GENERIC_READ | GENERIC_WRITE),0,NULL,OPEN_ALWAYS,FILE_FLAG_DELETE  _ON_CLOSE,NULL);
    if (uacRunningLockFileHandle == INVALID_HANDLE_VALUE) {
        if (_waccess(elevatedLockFilePath, F_OK) == 0 &&
                _wremove(elevatedLockFilePath) != 0) {
            return FAILURE;
        }
        elevatedLockFileHandle = CreateFileW(elevatedLockFilePath,(GENERIC_READ | GENERIC_WRITE),0,NULL,OPEN_ALWAYS,FILE_FLAG_DELETE  _ON_CLOSE,NULL);
        if(elevatedLockFileHandle == INVALID_HANDLE_VALUE){
            ERRORBOX("Unable to acquire the necessary permissions to run demo app.");
            return FAILURE;
        }
        LPWSTR spawnCmdLine = BuildCommandLine(argc - 1, arg + 1);
        if(!spawnCmdLine){
            CloseHandle(elevatedLockFileHandle);
            ERRORBOX("An error occured while respawning self.");
            return FAILURE;
        }
        SHELLEXECUTEINFO sinfo;
        memset(&sinfo, 0, sizeof(SHELLEXECUTEINFO));
        sinfo.cbSize = sizeof(SHELLEXECUTEINFO);
        sinfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS;
        sinfo.hwnd = NULL;
        sinfo.lpFile = arg[0];
        sinfo.lpParameters = spawnCmdLine;
        sinfo.lpVerb = L"runas"; // <<-- this is what makes a UAC prompt show up
        sinfo.nShow = SW_SHOWMAXIMIZED;
        BOOL result = ShellExecuteEx(&sinfo);
        LocalFree(spawnCmdLine);
        if(result){
            WaitForSingleObject(sinfo.hProcess, INFINITE);
            CloseHandle(sinfo.hProcess);
            return SUCCESS;
        }else{
            return FAILURE;
        }
    }
    EXIT_IF_ELEVATED(elevatedLockFilePath,uacRunningLo  ckFileHandle,SUCCESS);
    LocalFree(arg);
    return SUCCESS;
}

// ----------------------------------------------------------------------
// The following code was taken directly from the Mozilla Firefox Updater
// source tree, and slightly modified to support "Wide" strings in
// Visual C++.
// ----------------------------------------------------------------------

LPWSTR
BuildCommandLine(int argc, LPWSTR *argv){
    int i;
    int len = 0;
    // The + 1 of the last argument handles the
    // allocation for null termination
    for (i = 0; i < argc; ++i) {
        len += ArgStrLen(argv[i]) + 1;
    }
    // Protect against callers that pass 0 arguments
    if (len == 0) {
        len = 1;
    }
    LPWSTR s = (LPWSTR)malloc(len * sizeof(LPWSTR));
    if (!s) {
        return NULL;
    }
    LPWSTR c = s;
    for (i = 0; i < argc; ++i) {
        c = ArgToString(c, argv[i]);
        if (i + 1 != argc) {
            *c = ' ';
            ++c;
        }
    }
    *c = '\0';
    return s;
}
int
ArgStrLen(LPWSTR s) {
  int backslashes = 0;
  int i = wcslen(s);
  BOOL hasDoubleQuote = wcschr(s, L'"') != NULL;
  // Only add doublequotes if the string contains a space or a tab
  BOOL addDoubleQuotes = wcspbrk(s, L" \t") != NULL;
  if (addDoubleQuotes) {
    i += 2; // initial and final duoblequote
  }
  if (hasDoubleQuote) {
    while (*s) {
      if (*s == '\\') {
        ++backslashes;
      } else {
        if (*s == '"') {
          // Escape the doublequote and all backslashes preceding the doublequote
          i += backslashes + 1;
        }
        backslashes = 0;
      }

      ++s;
    }
  }

  return i;
}
LPWSTR
ArgToString(LPWSTR d, LPWSTR s) {
  int backslashes = 0;
  BOOL hasDoubleQuote = wcschr(s, L'"') != NULL;
  // Only add doublequotes if the string contains a space or a tab
  BOOL addDoubleQuotes = wcspbrk(s, L" \t") != NULL;
  if (addDoubleQuotes) {
    *d = '"'; // initial doublequote
    ++d;
  }
  if (hasDoubleQuote) {
    int i;
    while (*s) {
      if (*s == '\\') {
        ++backslashes;
      } else {
        if (*s == '"') {
        // Escape the doublequote and all backslashes\
        // preceding the doublequote
          for (i = 0; i <= backslashes; ++i) {
            *d = '\\';
            ++d;
          }
        }
        backslashes = 0;
      }
      *d = *s;
      ++d; ++s;
    }
  } else {
    wcscpy(d, s);
    d += wcslen(s);
  }
  if (addDoubleQuotes) {
    *d = '"'; // final doublequote
    ++d;
  }
  return d;
}
  • 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-14T22:45:57+00:00Added an answer on June 14, 2026 at 10:45 pm

    Simply as:

    char path[] = "C:\\program.exe";
    sinfo.lpFile = path;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application that will have attachments, and I would like to
I am working on an application that will perform a search against an online
I am working on web application that will use PHP & MySQL. Application will
I am currently working on an application that will retrieve other users' locations based
I'm working on a web application that will have a custom UI for the
I'm working on a desktop application that will produce several in-memory datasets as an
We are working on a Vista/Windows 7 application that will be running in 64
I'm working on a Rails 3.2 application that will allow users to authenticate with
I am working on an application at the minute that will originally be just
I am working on splitting out an existing, working application that I currently have

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.