程序調用ShellExecuteEx打開其他程序(兼容UAC獲取管理員權限)

參考文章:http://blog.csdn.net/xmnathan/article/details/39498431

// ConsoleApplication1.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include <Windows.h>

#pragma warning(disable: 4996)   

//檢查系統版本是否是Vista或更高的版本
bool IsOsVersionVistaOrGreater()
{
	OSVERSIONINFOEX ovex = { sizeof(OSVERSIONINFOEX), 0 };
	if (!GetVersionEx((LPOSVERSIONINFO)(&ovex)))
	{
		return false;
	}
	//通過版本號,判斷是否是vista及之後版本
	if (ovex.dwMajorVersion > 5)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//檢查並根據系統版本選擇打開程序方式
void MyShellExecuteEx(LPCTSTR lpFile, LPCTSTR lpParameters)
{
	if (IsOsVersionVistaOrGreater())
	{
		SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFOW), 0 };
		sei.fMask = SEE_MASK_NOCLOSEPROCESS;
		sei.nShow = SW_SHOWNORMAL;
		sei.lpFile = lpFile;
		sei.lpParameters = lpParameters;
		sei.lpVerb = _T("runas");
		ShellExecuteEx(&sei);
	}
	else
	{
		ShellExecute(NULL, _T("open"), lpFile, lpParameters, NULL, SW_SHOWNORMAL);
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	MyShellExecuteEx(_T(".\\Setup.exe"), _T("/S"));

	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章