参考文章:http://blog.csdn.net/xmnathan/article/details/39498431
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#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;
}