以下是在CSDN找到的一段VC代码,有没有高手能翻译一下为DELPHI代码:
//根据进程名称得到进程token
BOOL GetTokenByName(HANDLE &hToken,LPSTR lpName)
{
if(!lpName)
{
return FALSE
}
HANDLE hProcessSnap = NULL
BOOL bRet = FALSE
PROCESSENTRY32 pe32 = {0}
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
if (hProcessSnap == INVALID_HANDLE_VALUE)
return (FALSE)
pe32.dwSize = sizeof(PROCESSENTRY32)
if (Process32First(hProcessSnap, &pe32))
{
do
{
if(!strcmp(_strupr(pe32.szExeFile),_strupr(lpName)))
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
FALSE,pe32.th32ProcessID)
bRet = OpenProcessToken(hProcess,TOKEN_ALL_ACCESS,&hToken)
CloseHandle (hProcessSnap)
return (bRet)
}
}
while (Process32Next(hProcessSnap, &pe32))
bRet = TRUE
}
else
bRet = FALSE
CloseHandle (hProcessSnap)
return (bRet)
}
BOOL GetCurrentUserName()
{
HANDLE hToken
//得到shell的token
if(!GetTokenByName(hToken,"EXPLORER.EXE"
)
{
return FALSE
}
DWORD cbti = 0
PTOKEN_USER ptiUser = NULL
SID_NAME_USE snu
//取得所需空间大小
if (GetTokenInformation(hToken, TokenUser, NULL, 0, &cbti))
{
CloseHandle(hToken)
return FALSE
}
//分配空间
ptiUser = (PTOKEN_USER) HeapAlloc(GetProcessHeap(), 0, cbti)
if(!ptiUser)
{
CloseHandle(hToken)
return FALSE
}
//取得token信息
if (!GetTokenInformation(hToken, TokenUser, ptiUser, cbti, &cbti))
{
CloseHandle(hToken)
HeapFree(GetProcessHeap(), 0, ptiUser)
return FALSE
}
char szUser[50]
char szDomain[50]
DWORD nUser = 50
DWORD nDomain = 50
//根据用户的sid得到用户名和domain
if (!LookupAccountSid(NULL, ptiUser->User.Sid, szUser, &nUser,
szDomain, &nDomain, &snu))
{
CloseHandle(hToken)
HeapFree(GetProcessHeap(), 0, ptiUser)
return FALSE
}
OutputDebugString(szUser)
OutputDebugString("/r/n"
OutputDebugString(szDomain)
OutputDebugString("/r/n"
CloseHandle(hToken)
HeapFree(GetProcessHeap(), 0, ptiUser)
return TRUE
}