I am trying to use these features to get the username running the process. Here is the code:
#include <windows.h>
#include <psapi.h>
using namespace std;
int main()
{
HANDLE hProcess = GetCurrentProcess();
HANDLE hToken;
OpenProcessToken(hProcess, TOKEN_QUERY, &hToken);
DWORD len = 0;
GetTokenInformation(hToken, TokenOwner, NULL, 0, &len);
PTOKEN_OWNER to = (PTOKEN_OWNER)LocalAlloc(LPTR, len);
GetTokenInformation(hToken, TokenOwner, (LPVOID)&to, len, &len);
char nameUser[50];
DWORD nameUserSize = sizeof(nameUser);
SID_NAME_USE snu;
cout << "work";
LookupAccountSidA(NULL, to->Owner, nameUser, &nameUserSize, NULL, NULL, &snu);
cout << "not work";
cout << nameUser << endl;
LocalFree(to);
CloseHandle(hToken);
CloseHandle(hProcess);
return 0;
}
I have all the breaks at the moment where LookupAccountSidA()
is called. But I suspect the problem is due to the fact that I was wrong with the arguments to this function, or I am wrong to create a structure TOKEN_OWNER
. The second option is more likely, since I do not understand how memory is allocated here.
I took an example from MSDN and rewrote it to my needs, but nothing works. Here are some examples from MSDN:
Searching for a SID in an Access Token in C++
P.S. For russian-speaking: