能力值:
( LV4,RANK:50 )
|
-
-
2 楼
有答案了没?
|
能力值:
( LV2,RANK:10 )
|
-
-
3 楼
试试。。。
CreateProcessWithLogonW
|
能力值:
( LV9,RANK:610 )
|
-
-
4 楼
还没。
这个函数需要用户名密码的。。。
|
能力值:
( LV2,RANK:10 )
|
-
-
5 楼
ShellExecute,Operation参数写"RunAs"
|
能力值:
( LV2,RANK:10 )
|
-
-
6 楼
我是来领钱的。
|
能力值:
( LV9,RANK:610 )
|
-
-
7 楼
结贴。楼上是我一朋友,刚好这个问题昨天解决了,想把kx给他转正,求大家不要举报我啊~~~谢谢各位大神。。。
http://hi.baidu.com/blueapple_c/item/52b7d529beb7c30f76272cd7
下面公布代码,可能有点细节需要调整:
-------- CreateProcessEx.h文件 --------
#ifndef _CREATE_PROCESS_EX_H_
#define _CREATE_PROCESS_EX_H_
#include <windows.h>
#include "tchar.h"
#pragma comment(lib, "shell32")
#pragma comment(lib, "user32")
#pragma comment(lib, "Advapi32.lib")
//以普通权限启动进程
BOOL CreateProcessLow(TCHAR * lpApplicationName,
TCHAR * lpCommandLine = NULL,
TCHAR * lpDirectory = NULL,
UINTnShow = SW_SHOWNORMAL);
//以管理员权限启动进程
BOOL CreateProcessHigh(TCHAR * strProcessName,
TCHAR * strCommandLine = NULL,
TCHAR * lpDirectory = NULL,
UINTnShow = SW_SHOWNORMAL);
#endif //_CREATE_PROCESS_EX_H_
-------- CreateProcessEx.h文件 --------
-------- CreateProcessEx.cpp文件 --------
#include "CreateProcessEx.h"
#include <string>
using namespace std;
typedef BOOL (WINAPI *F_CreateProcessWithTokenW)(
__in HANDLE hToken,
__in DWORD dwLogonFlags,
__in LPCWSTR lpApplicationName,
__in LPWSTR lpCommandLine,
__in DWORD dwCreationFlags,
__in LPVOID lpEnvironment,
__in LPCWSTR lpCurrentDirectory,
__in LPSTARTUPINFOW lpStartupInfo,
__out LPPROCESS_INFORMATION lpProcessInfo
);
HANDLE DupExplorerToken();
BOOL IsVistaOrLater();
BOOL IsAdminPrivilege();
//以普通权限启动进程
BOOL CreateProcessLow(TCHAR * lpApplicationName,
TCHAR * lpCommandLine,
TCHAR * lpDirectory,
UINTnShow)
{
if (!IsVistaOrLater()
|| !IsAdminPrivilege())
{
HINSTANCE hRet = ShellExecute(NULL, _T("open"), lpApplicationName, lpCommandLine, lpDirectory, nShow);
return ((int)hRet > 32);
}
HANDLE hToken = DupExplorerToken();
if (hToken == NULL)
return FALSE;
static HMODULE hDll = LoadLibrary(_T("ADVAPI32.dll"));
if (!hDll)
{
CloseHandle(hToken);
return FALSE;
}
F_CreateProcessWithTokenW pfn = (F_CreateProcessWithTokenW)GetProcAddress(hDll, "CreateProcessWithTokenW");
if (!pfn)
{
CloseHandle(hToken);
return FALSE;
}
STARTUPINFO si = {sizeof(STARTUPINFO)};
PROCESS_INFORMATION pi = {0};
BOOL ret = pfn(hToken,
LOGON_WITH_PROFILE,
lpApplicationName,
lpCommandLine,
NORMAL_PRIORITY_CLASS,
NULL,
lpDirectory,
&si,
&pi);
if (ret)
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
CloseHandle(hToken);
return ret;
}
//以管理员权限启动进程
BOOL CreateProcessHigh(TCHAR * lpApplicationName,
TCHAR * lpCommandLine,
TCHAR * lpDirectory,
UINTnShow)
{
#ifdef _UNICODE
wstring command;
#else
string command;
#endif
if (lpCommandLine)
{
command = lpCommandLine;
}
if (IsVistaOrLater()
&& !IsAdminPrivilege())
{
command += _T(" -Admin");
}
HINSTANCE hRet = ShellExecute(NULL, _T("runas"), lpApplicationName, command.c_str(), lpDirectory, nShow);
return ((int)hRet > 32);
}
HANDLE DupExplorerToken()
{
DWORD dwPid = 0;
HWND hwnd = FindWindow(_T("Shell_TrayWnd"), NULL);
if (NULL == hwnd)
return NULL;
GetWindowThreadProcessId(hwnd, &dwPid);
if (dwPid == 0)
return NULL;
HANDLE hExplorer = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwPid);
if (hExplorer == NULL)
return NULL;
HANDLE hToken = NULL;
OpenProcessToken(hExplorer, TOKEN_DUPLICATE, &hToken);
CloseHandle(hExplorer);
HANDLE hNewToken = NULL;
DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hNewToken);
CloseHandle(hToken);
return hNewToken;
}
BOOL IsVistaOrLater()
{
OSVERSIONINFOEX version = {sizeof(OSVERSIONINFOEX)};
if (!GetVersionEx((LPOSVERSIONINFO)&version))
{
version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx((LPOSVERSIONINFO)&version))
{
return FALSE;
}
}
return (version.dwMajorVersion >= 6);
}
BOOL IsAdminPrivilege()
{
BOOL bIsAdmin = FALSE;
BOOL bRet = FALSE;
SID_IDENTIFIER_AUTHORITY idetifier = SECURITY_NT_AUTHORITY;
PSID pAdministratorGroup;
if (AllocateAndInitializeSid(
&idetifier,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0,0,0,0,0,0,
&pAdministratorGroup))
{
if (!CheckTokenMembership(NULL, pAdministratorGroup, &bRet))
{
bIsAdmin = FALSE;
}
if (bRet)
{
bIsAdmin = TRUE;
}
FreeSid(pAdministratorGroup);
}
return bIsAdmin;
}
-------- CreateProcessEx.cpp文件 --------
|
能力值:
( LV2,RANK:10 )
|
-
-
8 楼
CreateProcessLow在管理员下,调用创建非管理员进程失败啊
|