I'm trying to get the username of the process running using these functions.
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 everything crashes at the moment where the function LookupAccountSidA is LookupAccountSidA . But I suspect that this is due to the fact that I incorrectly either pass arguments to this function, or I incorrectly create the TOKEN_OWNER structure. And the second option is more likely, since I do not understand how memory is allocated here.
I took the example of MSDN and redid it to fit my needs, but nothing works. Here are examples with MSDN: