| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ and WinAPI Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
how to get the current user id?
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
|
|
#2
|
||||
|
||||
|
Re: how to get the current user id?
Is GetUserNameEx what you are searching for?
__________________
Ovidiu Cucu
|
|
#3
|
|||
|
|||
|
Re: how to get the current user id?
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.
|
|
#4
|
|||
|
|||
|
Re: how to get the current user id?
What is user id ? SID??
__________________
Best Api Monitor tool. Trace the target program automatically and monitor the parameters of all API and COM interfaces. Auto Debug for Windows 4.0 Auto Debug for .Net http://www.autodebug.com/ |
|
#5
|
|||
|
|||
|
Re: how to get the current user id?
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... |
|
#6
|
|||
|
|||
|
Re: how to get the current user id?
If you want to get the SID, please use these API:
LookupAccountName to get the SID ConvertSidToStringSid to convert sid to string.
__________________
Best Api Monitor tool. Trace the target program automatically and monitor the parameters of all API and COM interfaces. Auto Debug for Windows 4.0 Auto Debug for .Net http://www.autodebug.com/ |
|
#7
|
|||
|
|||
|
Re: how to get the current user id?
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)
__________________
Best Api Monitor tool. Trace the target program automatically and monitor the parameters of all API and COM interfaces. Auto Debug for Windows 4.0 Auto Debug for .Net http://www.autodebug.com/ |
|
#8
|
|||
|
|||
|
Re: how to get the current user id?
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. |
|
#9
|
|||
|
|||
|
Re: how to get the current user id? (solved)
here is the solution...
Code:
#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;
}
|
|
#10
|
|||
|
|||
|
Re: how to get the current user id?
@pengch: i missed your last post, thanks a lot for your help. it's working now.
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|