Click here to Skip to main content
6,595,444 members and growing! (17,235 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Vista Security » User Account Control     Beginner License: The Code Project Open License (CPOL)

How to Elevate Your Application at Windows Startup in Vista

By Kwon Yong Hwi

An article on elevating an application at Windows startup in Vista
C++Vista, Dev
Posted:17 May 2008
Views:20,134
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.75 Rating: 2.50 out of 5
3 votes, 60.0%
1

2
1 vote, 20.0%
3

4
1 vote, 20.0%
5

Introduction

This article describes how to elevate your program in Windows startup in Vista. Elevating an application is so easy, but it's not easy registering an application in startup.

Background

This article requires readers to have some background.
Firstly, it's important to understanding Vista's UAC. UAC is too complex to understand. Hence, this article does not have any information about UAC.
Secondly, MFC and API knowledge are required since the sample code of this article uses MFC and API.

Vista and Windows Startup

Many programs need to run at Windows startup.

In Windows XP, run at startup was not big deal. But, it became a big deal in Windows Vista. Windows Vista's new protection features restrict a startup program which wants to run as administrator. (It needs to elevate.)

If your application does not need an administrator privilege, there is nothing to do for migration to Windows Vista. But, if your application need an administrator privilege, your application can't run at startup because Vista's new protection blocks your program.

It seems to be impossible to run an administrator privileged program at Windows startup in Vista. But, there is a way to do it (even if it is not the same as XP).

This article describes Windows Vista's protection and a possible method to figure out our problem.

Another Way

To find another way, we have to think about the architecture of protection software. Start with this:

"Windows Vista is allowed to run software which does not need an administrator privilege."

So, one possible method is as follows:

  • We have to create a normal program which does not need an administrator privilege. (New protection does not block none privilege application.)
  • That normally has to elevate itself with CreateProcess and terminate.

The following pseudo code shows it:

void Main()
{
    if(IsVista() == TRUE && Elevation() == FALSE) {
        ElevationItself();
        Terminate();
    }
}

C++ Code (MFC)

Pseudo code is easy, but real code is not.

CString g_szProgramFolder;
CString g_szCmdLine;

BOOL RunAsAdministrator(LPSTR lpszFileName, LPSTR lpszDirectory)
{
    SHELLEXECUTEINFOA TempInfo = {0};

    TempInfo.cbSize = sizeof(SHELLEXECUTEINFOA);
    TempInfo.fMask = 0;
    TempInfo.hwnd = NULL;
    TempInfo.lpVerb = "runas";
    TempInfo.lpFile = lpszFileName;
    TempInfo.lpParameters = "runasadmin";
    TempInfo.lpDirectory = lpszDirectory;
    TempInfo.nShow = SW_NORMAL;

    BOOL bRet = ::ShellExecuteExA(&TempInfo);

    return bRet;
}

BOOL CMFCApplication::InitInstance()
{
    char szCurFolder[1024] = { 0, };
    GetModuleFileName(GetModuleHandle(NULL), szCurFolder, 1023);

    CString szFullPath = szCurFolder;
    szFullPath = szFullPath.Left(szFullPath.ReverseFind('\\'));

    g_szProgramFolder = szFullPath;

    CCommandLineInfo oCmdLineInfo;
    ParseCommandLine(oCmdLineInfo);

    g_szCmdLine = oCmdLineInfo.m_strFileName;

    if(IsVista()) {
        if(stricmp(g_szCmdLine, "elevation") == 0) {
            char szCmdLine[1024] = { 0, };
            char szCurFileName[1024] = { 0, };
            GetModuleFileName(GetModuleHandle(NULL), szCurFileName, 1023);

            BOOL bRet = 
                RunAsAdministrator( szCurFileName, (LPSTR)(LPCTSTR)g_szProgramFolder );

            if(bRet == TRUE) {
                return FALSE;
            }
        } else if(stricmp(g_szCmdLine, "runasadmin") == 0) {
            //
            // go trough!.
            //
        } else {
            char szCmdLine[1024] = { 0, };
            char szCurFileName[1024] = { 0, };
            GetModuleFileName(GetModuleHandle(NULL), szCurFileName, 1023);

            sprintf(szCmdLine, "\"%s\" elevation", szCurFileName);

            WinExec(szCmdLine, SW_SHOW);

            return FALSE;
        }
    }
    .
    .
    .
}

Points of Interest

RunAsAdministrator function returns FALSE when a user selects 'Cancel'. So, you can execute a program repeatedly until the user selects 'Allow'.

History

  • 2008-05-16: First released

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kwon Yong Hwi


Member
I have an experience of C++, Windows Programming.
These days, Programming with Visual C++ and Windows Platform such as COM. And I often explore something new to research.
I release some freewares on my website (http://rodream.net)

- Microsoft Most Valuable Professional (Visual C++ Part)
Occupation: Software Developer
Location: Korea, Republic Of Korea, Republic Of

Other popular Vista Security articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralFull source code Pinmemberlemonlyt1:41 31 Mar '09  
GeneralProcessShellCommand() reports error Pinmemberkuan deng7:05 18 Mar '09  
GeneralSource code PinmemberMasterPr0:38 23 Jun '08  
GeneralRe: Source code PinmemberMember 39280821:21 18 Sep '08  
GeneralRe: Source code Pinmemberlzf89772:00 24 Oct '08  
GeneralBuffer overflow Pinmembermav.northwind21:51 17 May '08  
GeneralRe: Buffer overflow PinmemberKwon Yong Hwi5:36 18 May '08  
GeneralRe: Buffer overflow PinmemberSteveKing20:23 18 May '08  
GeneralRe: Buffer overflow PinmemberKwon Yong Hwi4:54 19 May '08  
GeneralRe: Buffer overflow PinmemberMihai Nita9:26 19 May '08  
GeneralRe: Buffer overflow PinmemberKwon Yong Hwi14:45 19 May '08  
GeneralRe: Buffer overflow PinmemberMihai Nita14:42 22 May '08  
GeneralYou shouldn't display an elevation prompt at login PinmemberDaniel Grunwald7:13 17 May '08  
GeneralRe: You shouldn't display an elevation prompt at login Pinmemberaxelriet16:00 17 May '08  
GeneralRe: You shouldn't display an elevation prompt at login PinmemberLeo Davidson8:36 18 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 May 2008
Editor: Deeksha Shenoy
Copyright 2008 by Kwon Yong Hwi
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project