Click to See Complete Forum and Search --> : how to get the current user id?
crupp
August 23rd, 2006, 09:49 AM
Hi,
my application needs to get the user id of the current user. I'm searching since 30 minutes but i just can't find a function name - there's GetUserName or GetCurrentProcessId or GetCurrentThreadId, but there's no GetCurrentUserId!
Can anybody help me? Thanks a lot
Chris
PS: i use C and the SDK, not C++. usually that doesn't make a difference, but please don't suggest C++ classes :)
ovidiucucu
August 23rd, 2006, 10:26 AM
Is GetUserNameEx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getusernameex.asp) what you are searching for?
crupp
August 24th, 2006, 03:31 AM
I don't think it is. I found that function, but according to the parameters it's not really able to give me a user-id, just a "NameUniqueId", which is a GUID. I'm pretty sure that there's a numerical value, but i don't know how to get it.
pengch
August 24th, 2006, 05:09 AM
What is user id ? SID??
crupp
August 24th, 2006, 05:17 AM
good question.
i'm a unix programmer. so it's clear what a user ID is for me. But i'm not sure if microsoft or windows have the same idea... :)
My problem is this: i have to port unix software to windows. The user-id (numeric, 4 bytes) is used for identifying the user. So i need a comparable id for windows.
And i know that somehow this must be possible, since cygwin has an "id" command, just on unix, and it prints
uid=40197(Christoph) gid=10545(mkgroup-l-d) groups=0(root),544(Administrators),5
45(Users),10545(mkgroup-l-d)
So either they completely fake it or they found a way to get a user id on windows...
pengch
August 24th, 2006, 05:23 AM
If you want to get the SID, please use these API:
LookupAccountName to get the SID
ConvertSidToStringSid to convert sid to string.
pengch
August 24th, 2006, 05:31 AM
from MSDN
SID Components
A SID value includes components that provide information about the SID structure and components that uniquely identify a trustee. A SID consists of the following components:
The revision level of the SID structure
A 48-bit identifier authority value that identifies the authority, such as a Windows NT server domain, that issued the SID.
A variable number of subauthority or relative identifier (RID) values that uniquely identify the trustee relative to the authority that issued the SID.
The combination of the identifier authority value and the subauthority values ensures that no two SIDs will be the same, even if two different SID-issuing authorities issue the same combination of RID values. Each SID-issuing authority issues a given RID only once.
SIDs are stored in binary format in a SID structure. To display a SID, you can call the ConvertSidToStringSid function to convert a binary SID to string format. To convert a SID string back to a valid, functional SID, call the ConvertStringSidToSid function.
These functions use the following standardized string notation for SIDs, which makes it simpler to visualize their components:
S-R-I-S-S...
In this notation, the literal character S identifies the series of digits as a SID, R is the revision level, I is the identifier-authority value, and S... is one or more subauthority values.
The following example uses this notation to display the well-known domain-relative SID of the local Administrators group:
S-1–5-32-544
In this example, the SID has the following components. The constants in parentheses are well-known identifier authority and RID values defined in WINNT.H.
A revision level of 1
An identifier-authority value of 5 (SECURITY_NT_AUTHORITY)
A first subauthority value of 32 (SECURITY_BUILTIN_DOMAIN_RID)
A second subauthority value of 544 (DOMAIN_ALIAS_RID_ADMINS)
crupp
August 24th, 2006, 05:45 AM
i looked in the sourcecode of cygwin, and it was a bit hard to track it down, but it seems that they completely emulate the user ID stuff. it was not helpful for me.
LookupAccountName looks like a pain in the ***, compared to getuid(), but thanks nevertheless. i'll have a look.
crupp
August 24th, 2006, 07:34 AM
here is the solution...
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
HANDLE hToken = NULL;
DWORD dwBufferSize = 0;
PTOKEN_USER pTokenUser = NULL;
int uid;
/* Open the access token associated with the calling process. */
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
printf("OpenProcessToken failed. GetLastError returned: %d\n",
GetLastError());
return -1;
}
/* get the size of the memory buffer needed for the SID */
(void)GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize);
pTokenUser = (PTOKEN_USER)malloc(dwBufferSize);
memset(pTokenUser, 0, dwBufferSize);
/* Retrieve the token information in a TOKEN_USER structure. */
if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dwBufferSize,
&dwBufferSize)) {
printf("2 GetTokenInformation failed. GetLastError returned: %d\n"
GetLastError());
return -1;
}
CloseHandle(hToken);
if (!IsValidSid(pTokenUser->User.Sid)) {
printf("The owner SID is invalid.\n");
free(pTokenUser);
return -1;
}
uid = *GetSidSubAuthority(pTokenUser->User.Sid,
*GetSidSubAuthorityCount(pTokenUser->User.Sid) - 1);
printf("uid = %u\n", uid);
free(pTokenUser);
CloseHandle(hToken);
return 0;
}
that was no fun at all.
crupp
August 24th, 2006, 07:44 AM
@pengch: i missed your last post, thanks a lot for your help. it's working now.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.