在服务程序中创建用户进程的方法

 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, 0sizeof(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:
 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         }
posted @ 2007-09-26 12:01  至尊王者  阅读(1295)  评论(0编辑  收藏  举报