在服务程序中创建用户进程的方法
1 HWND deskton;
2 DWORD desktonID;
3 HANDLE hProc;
4
5 deskton = FindWindow("Progman", "Program Manager");
6 GetWindowThreadProcessId(deskton,&desktonID);
7 hProc = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,desktonID);
8 if ( hProc == NULL )
9 {
10 return 0;
11 }
12 if ( !OpenProcessToken(hProc,TOKEN_DUPLICATE,&hToken) )
13 {
14 return 0;
15 }
16 if ( DuplicateTokenEx(hToken,TOKEN_ALL_ACCESS,NULL,SecurityImpersonation,TokenPrimary,&hTokenNew ))
17 {
18 memset(&startinfo, 0, sizeof(STARTUPINFO));
19 startinfo.cb = sizeof(STARTUPINFO);
20 startinfo.dwFlags = STARTF_USESHOWWINDOW;
21 startinfo.wShowWindow = SW_SHOWNORMAL;
22 }
23 CreateProcessAsUser( hTokenNew,
24 "C:\WINDOWS\system32\notepad.exe",
25 NULL,
26 NULL,
27 NULL,
28 FALSE,
29 CREATE_DEFAULT_ERROR_MODE,
30 NULL,
31 NULL,
32 &startinfo,&procinfo);
33
方法2:
2 DWORD desktonID;
3 HANDLE hProc;
4
5 deskton = FindWindow("Progman", "Program Manager");
6 GetWindowThreadProcessId(deskton,&desktonID);
7 hProc = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,desktonID);
8 if ( hProc == NULL )
9 {
10 return 0;
11 }
12 if ( !OpenProcessToken(hProc,TOKEN_DUPLICATE,&hToken) )
13 {
14 return 0;
15 }
16 if ( DuplicateTokenEx(hToken,TOKEN_ALL_ACCESS,NULL,SecurityImpersonation,TokenPrimary,&hTokenNew ))
17 {
18 memset(&startinfo, 0, sizeof(STARTUPINFO));
19 startinfo.cb = sizeof(STARTUPINFO);
20 startinfo.dwFlags = STARTF_USESHOWWINDOW;
21 startinfo.wShowWindow = SW_SHOWNORMAL;
22 }
23 CreateProcessAsUser( hTokenNew,
24 "C:\WINDOWS\system32\notepad.exe",
25 NULL,
26 NULL,
27 NULL,
28 FALSE,
29 CREATE_DEFAULT_ERROR_MODE,
30 NULL,
31 NULL,
32 &startinfo,&procinfo);
33
1 if(::LogonUser(L"user", L"Domain", L"password", LOGON32_LOGON_INTERACTIVE, NULL, &hToken))
2 {
3 BOOL bResult = ::CreateProcessAsUser(hToken, L"C:\\WINDOWS\\system32\\notepad.exe", NULL,
4 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &startinfo, &procinfo);
5 if(bResult)
6 {
7 ODS(L"OK");
8 }
9 else
10 {
11 ODS(L"NO");
12 }
13 return bResult;
14 }
2 {
3 BOOL bResult = ::CreateProcessAsUser(hToken, L"C:\\WINDOWS\\system32\\notepad.exe", NULL,
4 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &startinfo, &procinfo);
5 if(bResult)
6 {
7 ODS(L"OK");
8 }
9 else
10 {
11 ODS(L"NO");
12 }
13 return bResult;
14 }
我最擅长从零开始创造世界,所以从来不怕失败,它最多也就让我一无所有。