Hi, I've searched for this but I found just linux stuff
Is there a quick API or something like that, that returns a struct or just the name of the current logged user?
I have to do it in C on win 2k
Thanx to everyone.
Hi, I've searched for this but I found just linux stuff
Is there a quick API or something like that, that returns a struct or just the name of the current logged user?
I have to do it in C on win 2k
Thanx to everyone.
This forum is the best one I've ever seen. Great ppl, great coders
thanx
This forum is the best one I've ever seen. Great ppl, great coders
Code:#include <windows.h> #include <stdio.h> #define INFO_BUFFER_SIZE 32767 int main() { TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; // Becca e stampa il nome dell'utente loggato bufCharCount = INFO_BUFFER_SIZE; if(!GetUserName(infoBuf,&bufCharCount)) printf("Errore nel determinare il nome dell' utente"); printf("User name: %s\n", infoBuf); // stampa a video system("PAUSE"); }
This forum is the best one I've ever seen. Great ppl, great coders
is there a way to know if a user belongs to a given group?
thanx
This forum is the best one I've ever seen. Great ppl, great coders
To enumerate the groups a user belongs to, you can either get group membership information from the thread token (for the current user only) or call NetUserGetLocalGroups. Each has advantages and disadvantages. For example, the thread token will return pseduo groups such as INTERACTIVE and Everyone. See Usnet thread: How to get the groups of a user for more information.
If you don't need a list of groups the user belongs to and just want to check if the current user belongs to a specific group you can use the CheckTokenMembership function. Even easier, if you need to check if the current user is an admin, you can use the IsUserAnAdmin function.
Last edited by anonytmouse; 03-06-2005 at 09:42 AM.
Ah thanx like allways, you are great
I think NetUserGetLocalGroups can help me.
This forum is the best one I've ever seen. Great ppl, great coders
I spent a lot modifying the example here .. I know I'm a newbie, anyway.. maybe this can help someone..
If you want to declare the username, then:
wchar_t username[] = L"USERNAME";
and if you want to compare it remember that it is a pointer to a Unicode string :
Code:if ( !lstrcmpW(pTmpBuf->lgrui0_name, L"watheveryouwant") ) printf("ok\n"); else printf("not ok\n");
This forum is the best one I've ever seen. Great ppl, great coders
I have some problems.. at least one.. this programs is supposed to just print the group of the logged user
while compiling my devcpp says :Code:#include <stdio.h> #include <assert.h> #include <windows.h> #include "psapi.h" #include <string.h> #include <lm.h> #define INFO_BUFFER_SIZE 32767 int main(void) { char infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; bufCharCount = INFO_BUFFER_SIZE; LPLOCALGROUP_USERS_INFO_0 pBuf = NULL; DWORD dwLevel = 0; DWORD dwFlags = LG_INCLUDE_INDIRECT ; DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; NET_API_STATUS nStatus; GetUserName(infoBuf,&bufCharCount); nStatus = NetUserGetLocalGroups(NULL, infoBuf, dwLevel, dwFlags, (LPBYTE *) &pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries); if (nStatus == NERR_Success) { LPLOCALGROUP_USERS_INFO_0 pTmpBuf; DWORD i; DWORD dwTotalCount = 0; if ((pTmpBuf = pBuf) != NULL) { for (i = 0; i < dwEntriesRead; i++) { assert(pTmpBuf != NULL); if (pTmpBuf == NULL) { fprintf(stderr, "An access violation has occurred\n"); break; } wprintf(L"%s\n", pTmpBuf->lgrui0_name); } } } if (pBuf != NULL) NetApiBufferFree(pBuf); system("PAUSE"); return 0; }
[Warning] passing arg 2 of `NetUserGetLocalGroups' from incompatible pointer type
in effects it is waiting a wchar_t and i'm passing a char.. how can I convert it?
This forum is the best one I've ever seen. Great ppl, great coders
To convert a char string to a wchar_t string, you can use either the C library function mbstowcs or the Windows function MultiByteToWideChar. However, it is usually preferable to avoid the conversion by retrieving the string in unicode in the first place. In this case, this can be achieved by using the wide character version of GetUserName. Most Windows functions that take strings are available in three versions:
As you can see, we can use the GetUserNameW version to retrieve the user name as a wchar_t string. I have also simplified the code for printing out the groups.Code:/* TCHAR version. This is a macro that is replaced with GetUserNameA or GetUserNameW, * depending on whether UNICODE is defined. */ BOOL GetUserName(LPTSTR lpBuffer, LPDWORD nSize); /* Ansi version. This version takes and returns ansi char strings. The function name * is followed by the capital letter A. */ BOOL GetUserNameA(LPSTR lpBuffer, LPDWORD nSize); /* Wide version. This version takes and returns unicode wchar_t strings. The function * name is followed by the capital letter W. Wide character versions of functions are * typically only available on Windows NT/2000/XP/2003, and not Windows 9x/ME. * However, the use of the Microsoft library for Unicode with your project makes * most of them available on Windows 9x/ME. */ BOOL GetUserNameW(LPWSTR lpBuffer, LPDWORD nSize);
Code:int main(void) { WCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; bufCharCount = INFO_BUFFER_SIZE; LPLOCALGROUP_USERS_INFO_0 pBuf = NULL; DWORD dwLevel = 0; DWORD dwFlags = LG_INCLUDE_INDIRECT ; DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; NET_API_STATUS nStatus; GetUserNameW(infoBuf,&bufCharCount); nStatus = NetUserGetLocalGroups(NULL, infoBuf, dwLevel, dwFlags, (LPBYTE *) &pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries); if (nStatus == NERR_Success && pBuf != NULL) { DWORD i; for (i = 0; i < dwEntriesRead; i++) { wprintf(L"%s\n", pBuf[i].lgrui0_name); } } if (pBuf != NULL) NetApiBufferFree(pBuf); system("PAUSE"); return 0; }
Cool! That's gold for me I never heard about that and It's really useful!Originally Posted by anonytmouse
Thanx for you code too, I will include you in the credits hehe
Right now I can't compile it, this evening I will try to finish this project.
Thanx, have a nice day.
This forum is the best one I've ever seen. Great ppl, great coders