HANDLE      hToken;
PROCESS_INFORMATION pi;
PSID pSid = NULL;
STARTUPINFO si;
BOOL bResult;
if (!LogonUser(
"myuser",
".", 
"612349",
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
&hToken) 

   {
      MessageBox("no such user!",NULL,MB_OK);
  exit(0);
   }
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);         memset(&pi, 0, sizeof(pi));
bResult = CreateProcessAsUser(
      hToken,          
      "F:\\TC\\BIN\\520.exe",             
     "F:\\TC\\BIN\\520.exe",    
      NULL,            
      NULL,             
      0,           
      NULL,
      NULL,            
      NULL,            
      &si,              
      &pi              
   ); 

if(0 == bResult ) 
{  CHAR szBuf[80]; 
          DWORD dw = GetLastError(); 
sprintf(szBuf, "failed: GetLastError returned %u\n", NULL, dw); 
 
          MessageBox(szBuf, NULL,  MB_OK); 
ExitProcess(dw); 
}  if (pi.hProcess != INVALID_HANDLE_VALUE) 
   { 
      WaitForSingleObject(pi.hProcess, INFINITE); 
      CloseHandle(pi.hProcess); 
   } 
 else
 {
MessageBox("error has occured",NULL,MB_OK);
 }
 if (pi.hThread != INVALID_HANDLE_VALUE)
      CloseHandle(pi.hThread);  以上代码我是从MSDN那里copy过来的,只作了一点改动,却怎么都运行不成功,我是想用CreateProcessAsUser()函数来执行一个exe,上面的代码中LogonUser()函数没有错,我测试了;但接下来的CreateProcessAsUser()函数就总是失败,不知道为什么

解决方案 »

  1.   

    我用CreateProcessAsUser()是想以一个低级权限的身份运行某个exe
      

  2.   

    F:\\TC\\BIN\\520.exe看着是用tc编出来的吧,那就不是win32程序,用CreateProcessAsUser可能不行。
      

  3.   

    调用GetLastError()函数看看是什么错误代码.
      

  4.   

    Please review the MSDN, espically this paragraph:
    hToken 
    [in] Handle to a primary token that represents a user. The handle must have TOKEN_QUERY, TOKEN_DUPLICATE, and TOKEN_ASSIGN_PRIMARY access. For more information, see Access Rights for Access-Token Objects. The user represented by the token must have read and execute access to the application specified by the lpApplicationName or the lpCommandLine parameter. 
    If your process has the SE_TCB_NAME privilege, it can call the LogonUser function to get a primary token that represents the specified user. Alternatively, you can call the DuplicateTokenEx function to convert an impersonation token into a primary token. This allows a server application that is impersonating a client to create a process that has the security context of the client. 
      

  5.   

    本人再次看了MSDN,在上面代码的中加入了如下的代码:SECURITY_ATTRIBUTES sa;
    sa.nLength=sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle=TRUE;
    sa.lpSecurityDescriptor=NULL;
    HANDLE      hNewToken;
    BOOL        result;
    result=DuplicateToke(hToken,
    TOKEN_READ|TOKEN_QUERY|TOKEN_EXECUTE|TOKEN_ASSIGN_PRIMARY,
    &sa,
    SecurityImpersonation,
    TokenPrimary, 
    &hNewToken) if(0==result)
         MessageBox("DuplicateTokenEx errer has occured",NULL,MB_OK);
    ImpersonateLoggedOnUser(hNewToken); bResult = CreateProcessAsUser(
          hNewToken, .....);
    但是结果还有错呀,CreateProcessAsUser函数还是不成功,各位高手留几句可以解决这个问题的代码吧,或者指出我的代码哪里不行,要改成哪句,拜托了,
      

  6.   

    本人再次看了MSDN,在上面代码的中加入了如下的代码:SECURITY_ATTRIBUTES sa;
    sa.nLength=sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle=TRUE;
    sa.lpSecurityDescriptor=NULL;
    HANDLE      hNewToken;
    BOOL        result;
    result=DuplicateTokeEx(hToken,
    TOKEN_READ|TOKEN_QUERY|TOKEN_EXECUTE|TOKEN_ASSIGN_PRIMARY,
    &sa,
    SecurityImpersonation,
    TokenPrimary, 
    &hNewToken) if(0==result)
         MessageBox("DuplicateTokenEx errer has occured",NULL,MB_OK);
    ImpersonateLoggedOnUser(hNewToken); bResult = CreateProcessAsUser(
          hNewToken, .....);
    但是结果还有错呀,CreateProcessAsUser函数还是不成功,各位高手留几句可以解决这个问题的代码吧,或者指出我的代码哪里不行,要改成哪句,拜托了,
    不好意思,DuplicateTokenEx错写成DuplicateToken了
      

  7.   

    CreateProcessAsUser究竟要怎样才能正确使用呢,急!
      

  8.   

    同意楼上,XP中叫做Fase User Switch....的
      

  9.   

    建议楼主看一下CreateProcessAsUser的说明,找出错误代码。
    Win32API出错都有错误代码的,可以帮助你分析错误。要学会利用这一特性。
    如果拿到错误代码还不清楚的话,再发问吧。
      

  10.   

    CreateProcessAsUser()返回值是0,说明CreateProcessAsUser()没有成功
    ....
    但是
    DWORD dw = GetLastError(); 
    ....返回的错误代码是0;