ShellExecute使用管理员身份执行

#include 
#include
#include

int main(void)
{
	SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };
	sei.lpVerb = TEXT("runas");
	sei.lpFile = TEXT("cmd.exe");//add  application  which you want to run as administrator here
	sei.nShow = SW_SHOWNORMAL;//without this,the windows will be hiden
	if (!ShellExecuteEx(&sei))
	{
		DWORD dwStatus = GetLastError();
		if (dwStatus == ERROR_CANCELLED)
		{
			printf("提升权限被用户拒绝\n");
		}
		else if (dwStatus == ERROR_FILE_NOT_FOUND)
		{
				printf("所要执行的文件没有找到\n");
		}
	}

	getchar();
	getchar();
	return 0;
}

你可能感兴趣的:(windows编程)