Get current user name under Windows - c

How can I get the current users sign in name in Windows?
What I have figured out is the function
char* user_name;
user_name=getenv("USERNAME");
but the problem is that it gives
admin
but when I sign in to Windows, my user name is "Sudip" and not "admin".

You can use GetUserName
#include <windows.h>
#include <Lmcons.h>
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);

You can use GetUserNameEx to get your display name instead of the actual user name. The EXTENDED_NAME_FORMAT enumeration has a NameDisplay entry that should do what you want.

Related

Is there any API in Linux to get the full language name from the locale?

I want to get the complete language name from the locale in Linux. For example, in Windows, there is one API GetLocaleInfoEx we can use, it will return "English" for locale "en-US".
wchar_t buffer[LOCALE_NAME_MAX_LENGTH];
GetLocaleInfoEx(L"en-US", LOCALE_SENGLISHLANGUAGENAME,
(LPWSTR)buffer, LOCALE_NAME_MAX_LENGTH)
This will fill the buffer with "English". Is there anything similar in Linux?
You can use
nl_langinfo(_NL_IDENTIFICATION_LANGUAGE)
from
#include <langinfo.h>
if locale is not set, you can set it with
s = getenv("LANG");
setlocale(LC_ALL, s);
After playing around with the other answers, I thought of concluding it here.
#include <langinfo.h>
#include <locale.h> // for LC_ALL_MASK flag, freelocale(), and newlocale() functions.
Access locale information from the system locale.
char *nl_langinfo(nl_item item);
Access locale information from the locale given as a parameter.
char *nl_langinfo_l(nl_item item, locale_t locale);
Use of GetLocaleInfoEx in the given example equivalent to the following on Linux.
locale_t loc = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
if (loc) {
language = strdup(nl_langinfo_l(_NL_IDENTIFICATION_LANGUAGE, loc));
freelocale(loc);
}
Refer to nl_langinfo for more details.

How to get Windows username and use it in a system function [duplicate]

I want to access the user name in the Windows using C programming and use that name to create the path to the particular file like "c:\users\john\Roaming.....and so on". So for every system user name e.g "john" is different. Help me to find the user name at run time.
#include <stdio.h>
int main(void)
{
printf("%s\n", getenv("USERPROFILE")); // Print user's home directory.
return 0;
}
To get the user name instead of the home path replace USERPROFILE with USERNAME.
What you are looking for, here, is probably more SHGetKnownFolderPath. The function lets you find per-user special folders. This is preferred to querying usernames because the home folder may not have the same name as the user.
WSTR* location;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &location);
if (SUCCEEDED(hr))
{
// location contains the folder path
// call CoTaskMemFree to free up the memory once you're done with it
CoTaskMemFree(location);
}
The list of so-called known folders is available here.
The function to get user name on windows is GetUserName
This answer, probably, will help you too.
you could use the following code to get the Username.
#include <stdlib.h>
void main(void)
{
//following gets the appdata folder
char szAppData[1024];
char * szBufer = 0;
szBufer = getenv ("APPDATA");
if (szBufer != NULL)
{
strcpy(szBufer , szAppData);
}
//following code gets the user name
char szOSUserName[1024];
szBufer = getenv ("USERNAME");
if (szBufer != NULL)
{
strcpy(szBufer , szOSUserName);
}
}
You can get the name of the current user with GetUserName:
#include <Windows.h>
#include <Lmcons.h>
#include <stdio.h>
int main()
{
char name[UNLEN + 1];
DWORD cch = UNLEN + 1;
if (GetUserName(name, &cch))
{
char cmd[100 + UNLEN + 1];
sprintf(cmd, "echo The username is \"%s\"", name); // Silly demo command
system(cmd);
}
return 0;
}
Use GetUserNameEx if you want the name in a specific format.
If you need to get the path to a special folder like "My Documents" or "Desktop" you should use the special folder functions like SHGetFolderPath or SHGetKnownFolderPath.
%USERNAME% will give you the username, but a better solution is to store it on %USERPROFILE%\\Desktop\\key.txt to at least make it OS-independent.
And an even better solution would be not to store private information on the users' desktops. Or anywhere.

GetAppliedGPOList and pGuidExtension value

I'm trying to use the GetAppliedGPOList function, but cannot find/understand what the pGuidExtension should be.
Here's the simple code so far:
#include <Windows.h>
#include <UserEnv.h>
int wmain(int argc, WCHAR *argv[])
{
//GetAppliedGPOList
DWORD flags = GPO_LIST_FLAG_MACHINE;
LPCWSTR machineName = NULL; //Local computer is used
PSID sidUser = NULL;
GUID *pGuidExtension; //What is the GUID of the extension?
PGROUP_POLICY_OBJECT *ppGPOList;
return 0;
}
Cannot run the function because I need to send that value.
Any example about the pGuidExtension value?
I did search here but found nothing about it.
Thank you.
The guids is listed at
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\GPExtensions
For example - Group Policy Client Side Extension List. The GetAppliedGPOList is looked under
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\History\{GuidExtension}
key

How do I get the user name of the current user?

I want to access the user name in the Windows using C programming and use that name to create the path to the particular file like "c:\users\john\Roaming.....and so on". So for every system user name e.g "john" is different. Help me to find the user name at run time.
#include <stdio.h>
int main(void)
{
printf("%s\n", getenv("USERPROFILE")); // Print user's home directory.
return 0;
}
To get the user name instead of the home path replace USERPROFILE with USERNAME.
What you are looking for, here, is probably more SHGetKnownFolderPath. The function lets you find per-user special folders. This is preferred to querying usernames because the home folder may not have the same name as the user.
WSTR* location;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &location);
if (SUCCEEDED(hr))
{
// location contains the folder path
// call CoTaskMemFree to free up the memory once you're done with it
CoTaskMemFree(location);
}
The list of so-called known folders is available here.
The function to get user name on windows is GetUserName
This answer, probably, will help you too.
you could use the following code to get the Username.
#include <stdlib.h>
void main(void)
{
//following gets the appdata folder
char szAppData[1024];
char * szBufer = 0;
szBufer = getenv ("APPDATA");
if (szBufer != NULL)
{
strcpy(szBufer , szAppData);
}
//following code gets the user name
char szOSUserName[1024];
szBufer = getenv ("USERNAME");
if (szBufer != NULL)
{
strcpy(szBufer , szOSUserName);
}
}
You can get the name of the current user with GetUserName:
#include <Windows.h>
#include <Lmcons.h>
#include <stdio.h>
int main()
{
char name[UNLEN + 1];
DWORD cch = UNLEN + 1;
if (GetUserName(name, &cch))
{
char cmd[100 + UNLEN + 1];
sprintf(cmd, "echo The username is \"%s\"", name); // Silly demo command
system(cmd);
}
return 0;
}
Use GetUserNameEx if you want the name in a specific format.
If you need to get the path to a special folder like "My Documents" or "Desktop" you should use the special folder functions like SHGetFolderPath or SHGetKnownFolderPath.
%USERNAME% will give you the username, but a better solution is to store it on %USERPROFILE%\\Desktop\\key.txt to at least make it OS-independent.
And an even better solution would be not to store private information on the users' desktops. Or anywhere.

How to retrieve current Windows user login using C?

I'm new to C. How can i retrieve the current user logged into Windows using C?
I know you can do this in C++ by Environment::UserName, but have no idea how to do it in C.
Thanks :)
You can use GetUserName:
#include <windows.h>
#include <Lmcons.h>
TCHAR username[UNLEN+1];
DWORD len = UNLEN+1;
if (GetUserName(username, &len))
{
//do something with username
}

Resources