请高人进来看看,希望把这段代码修改成DELPHI的。(100分)

  • 主题发起人 devilsniffer
  • 开始时间
D

devilsniffer

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大大,和這個代碼類似的程式功能用BCB如何實現?<br>  服務程式中如何以當前登陸用戶身份運行程式<br><br>  開發中有時會遇到這樣的問題,當服務程式需要使用某些功能時,由於用戶的關係而受到限制,比如訪問註冊表的HKEY_CURRENT_USER鍵,使用網路等等,這時候就需要以當前登陸用戶的身份去進行操作,通常會創建一個進程來完成需要的功能。如果使用CreateProcess來創建進程的話,新創建的進程和服務程式依然是相同的用戶身份,還是無法達到目的,只有使用CreateProcessAsUser了。但CreateProcessAsUser的第一個參數是HANDLE hToken,該參數通常應該用LogonUser來獲得,但是LogonUser又需要用戶名和用戶密碼,這樣就很不現實。那應該怎麼辦呢?我想到了一個方法可以繞過LogonUser直接獲得hToken。因為用戶已經登陸,那麼肯定有Shell(就是EXPLORER.EXE)運行了,我們可以通過遍曆進程來取得Shell的hToken來運行進程。下面就是示例代碼:<br><br>BOOL GetTokenByName(HANDLE &amp;hToken,LPSTR lpName)<br>{<br>&nbsp;if(!lpName)<br>&nbsp;{<br>&nbsp; return FALSE;<br>&nbsp;}<br>&nbsp;HANDLE &nbsp; &nbsp; &nbsp; &nbsp; hProcessSnap = NULL;<br>&nbsp; &nbsp; BOOL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bRet &nbsp; &nbsp; &nbsp;= FALSE;<br>&nbsp; &nbsp; PROCESSENTRY32 pe32 &nbsp; &nbsp; &nbsp;= {0};<br>&nbsp;<br>&nbsp; &nbsp; hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; &nbsp; if (hProcessSnap == INVALID_HANDLE_VALUE) <br>&nbsp; &nbsp; &nbsp; &nbsp; return (FALSE);<br>&nbsp;<br>&nbsp; &nbsp; pe32.dwSize = sizeof(PROCESSENTRY32);<br>&nbsp;<br>&nbsp; &nbsp; if (Process32First(hProcessSnap, &amp;pe32)) <br>&nbsp; &nbsp; { &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; do <br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp;if(!strcmp(_strupr(pe32.szExeFile),_strupr(lpName)))<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,<br>&nbsp; &nbsp; &nbsp;FALSE,pe32.th32ProcessID);<br>&nbsp; &nbsp; bRet = OpenProcessToken(hProcess,TOKEN_ALL_ACCESS,&amp;hToken);<br>&nbsp; &nbsp; CloseHandle (hProcessSnap);<br>&nbsp; &nbsp; return (bRet);<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; &nbsp; &nbsp; while (Process32Next(hProcessSnap, &amp;pe32));<br>&nbsp; &nbsp; &nbsp; &nbsp; bRet = TRUE;<br>&nbsp; &nbsp; } <br>&nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; bRet = FALSE;<br>&nbsp;<br>&nbsp; &nbsp; CloseHandle (hProcessSnap);<br>&nbsp; &nbsp; return (bRet);<br>}<br><br>BOOL RunProcess(LPCSTR lpImage)<br>{<br>&nbsp;if(!lpImage)<br>&nbsp;{<br>&nbsp; return FALSE;<br>&nbsp;}<br>&nbsp;HANDLE hToken;<br>&nbsp;if(!GetTokenByName(hToken,"EXPLORER.EXE"))<br>&nbsp;{<br>&nbsp; return FALSE;<br>&nbsp;}<br>&nbsp;STARTUPINFO si;<br>&nbsp;PROCESS_INFORMATION pi;<br>&nbsp;<br>&nbsp;ZeroMemory(&amp;si, sizeof(STARTUPINFO));<br>&nbsp;si.cb= sizeof(STARTUPINFO);<br>&nbsp;si.lpDesktop = TEXT("winsta0//default");<br>&nbsp;<br>&nbsp;BOOL bResult = CreateProcessAsUser(hToken,lpImage,NULL,NULL,NULL,<br>&nbsp; FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&amp;si,&amp;pi);<br>&nbsp;CloseHandle(hToken);<br>&nbsp;if(bResult)<br>&nbsp;{<br>&nbsp; OutputDebugString("CreateProcessAsUser ok!/r/n");<br>&nbsp;}<br>&nbsp;else<br>&nbsp;{<br>&nbsp; OutputDebugString("CreateProcessAsUser false!/r/n");<br>&nbsp;}<br>&nbsp;return bResult;<br>}
 
下面是我自己改的代码,但是不成功,希望知道的帮忙指出错误:<br><br>program Run;<br><br>uses<br>&nbsp; Windows,TlHelp32,SysUtils;<br><br>function GetProcessId(const ProcessName: string): Cardinal;<br>var<br>&nbsp; snap: THandle;<br>&nbsp; pe: TProcessEntry32;<br>&nbsp; r: LongBool;<br>&nbsp; fn: string;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; pe.dwSize := SizeOf(pe);<br>&nbsp; r := Process32First(snap, pe);<br>&nbsp; while r do<br>&nbsp; begin<br>&nbsp; &nbsp; fn := pe.szExeFile;<br>&nbsp; &nbsp; fn := LowerCase(ExtractFileName(fn));<br>&nbsp; &nbsp; if fn = LowerCase(ProcessName) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Result := pe.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; r := Process32Next(snap, pe);<br>&nbsp; end;<br>&nbsp; CloseHandle(snap);<br>end; //GetProcessId<br><br>function MyCreateProcessA(CommandLine: PChar): BOOL;<br>var<br>&nbsp; hToken :THandle;<br>&nbsp; si: TStartupInfo;<br>&nbsp; pi: TProcessInformation;<br>begin<br>&nbsp; result := False;<br>&nbsp; ZeroMemory(@si, SizeOf(si));<br>&nbsp; si.cb:= sizeof(STARTUPINFO);<br>&nbsp; si.lpDesktop := PChar('winsta0/default');<br>&nbsp; hToken:=GetProcessId('Explorer.exe'); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; if CreateProcessAsUser(hToken,CommandLine,nil,nil,nil,true,CREATE_NEW_CONSOLE,nil,nil,si,pi) then<br>&nbsp; begin<br>&nbsp; &nbsp; CloseHandle(pi.hThread);<br>&nbsp; &nbsp; CloseHandle(pi.hProcess);<br>&nbsp; &nbsp; result := True;<br>&nbsp; end;<br>end;<br><br>begin<br>&nbsp; MyCreateProcessA(pchar('c:/123.exe'));<br>end.
 
你的翻译偷工减料了,根本就没得到hToken,[:D]
 
虽然问题昨天已经自己解决了,还是非常感谢楼上的帮助!
 
顶部