|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis 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. BackgroundThis article requires readers to have some background. Vista and Windows StartupMany 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 WayTo find another way, we have to think about the architecture of protection software. Start with this:
So, one possible method is as follows:
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
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||