程序调用ShellExecuteEx打开其他程序(兼容UAC获取管理员权限)

版权声明:本文为博主原创文章,转载请注明原帖地址。 https://blog.csdn.net/sunflover454/article/details/61916245

参考文章: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;
}



猜你喜欢

转载自blog.csdn.net/sunflover454/article/details/61916245