Get Username for email id in windows 8 [duplicate] - c++

On Windows 7 to retrieve the name of a logged on user I can do this:
LPTSTR pUserName = NULL;
DWORD dwcbSzUserName = 0;
//'dwSessID' = user session ID
if(WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessID, WTSUserName, &pUserName, &dwcbSzUserName))
{
//Got user name in 'pUserName'
}
if(pUserName)
WTSFreeMemory(pUserName);
But on Windows 8 it returns some abbreviated name, for instance, "john_000" when the actual user's name is "John A. Doe".
So what is the way to retrieve the name of the logged on user (and possibly their email) on Windows 8 with C++ using WinAPIs as it's shown at log-on screen?

You could try NetUserGetInfo with USER_INFO_23 to get full name.
Something basically like:
//Got user name in 'pUserName'
NetUserGetInfo(NULL, pUserName, 23, my_USER_INFO_23);
//Got display name in my_USER_INFO_23.usri23_full_name

Related

Why do usernames end in $ in my Active Directory password filter DLL?

I have a custom Active Directory password filter DLL.
On a two-domain DC (Windows Server 2012 R2), the password filter sometimes receives the usernames with a dollar sign ($).
A user's account name is JSMITH.
The password filter DLL reports that JSMITH$ changed their password.
Why is this happening?
extern "C" __declspec(dllexport) NTSTATUS __stdcall PasswordChangeNotify(
_In_ PUNICODE_STRING UserName,
_In_ ULONG RelativeId,
_In_ PUNICODE_STRING NewPassword
)
{
// Set up process creation arguments
STARTUPINFO startupInformation;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInformation, sizeof(startupInformation));
ZeroMemory(&processInformation, sizeof(processInformation));
// Prepare arguments
std::wstring arguments = std::wstring(UserName->Buffer)
+ L" "
+ std::wstring(NewPassword->Buffer);
// ...
The only time the username (aka the sAMAccountName attribute in AD) automatically contains a $ at the end is for computer accounts. And computer accounts do actually have passwords, so it could just be reporting that a computer updated its password.
I guessing maybe that JSMITH$ was just made up example, since JSMITH sounds like a user account.
However, it is possible for you to explicitly put a $ at the end of a user's username, if you wanted to.

Get current username on windows

I modified my username in Control Panel -> User Accounts -> Change Your Name.
But when I use the GetUserName function, it returned my old username.
How do I get the new one?
EDIT 1
Here's the code as requested:
char user[UNLEN + 1];
DWORD user_len = UNLEN + 1;
GetUserName(user, &user_len);
Try to use the API GetUserNameEx, and passing as a format NameDisplay. I guess you changed the display name of the user, not the logon name.

Get logged on user's name or email on Windows 8 using C++ and WinAPIs

On Windows 7 to retrieve the name of a logged on user I can do this:
LPTSTR pUserName = NULL;
DWORD dwcbSzUserName = 0;
//'dwSessID' = user session ID
if(WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessID, WTSUserName, &pUserName, &dwcbSzUserName))
{
//Got user name in 'pUserName'
}
if(pUserName)
WTSFreeMemory(pUserName);
But on Windows 8 it returns some abbreviated name, for instance, "john_000" when the actual user's name is "John A. Doe".
So what is the way to retrieve the name of the logged on user (and possibly their email) on Windows 8 with C++ using WinAPIs as it's shown at log-on screen?
You could try NetUserGetInfo with USER_INFO_23 to get full name.
Something basically like:
//Got user name in 'pUserName'
NetUserGetInfo(NULL, pUserName, 23, my_USER_INFO_23);
//Got display name in my_USER_INFO_23.usri23_full_name

NetUserAdd() successful, but user is "invisible"

I'm creating a user with the NetUserAdd API. It returns successfully, the user has a User folder and I can see the username with wmic useraccount get name. However, the created user is not visible under the control panel, nor on the logon screen. I assume that I need to add the user to some group but I don't know which or how.
Here is how I create the user:
USER_INFO_1 user_info;
ZeroMemory(&user_info, sizeof(user_info));
user_info.usri1_name = userName;
user_info.usri1_password = password;
user_info.usri1_priv = USER_PRIV_USER;
user_info.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus = NetUserAdd(NULL, dwLevel, (LPBYTE)&user_info, &dwError);
How can I make the user visible on the logon screen?
You have created the user but you need to add it to the users group using NetLocalGroupAddMembers.
EDIT: Just realized I was providing the method for .NET. See this example for C++.
The user was not showing up on the welcome screen because it was not added to the Users group. This is how to do it:
LOCALGROUP_MEMBERS_INFO_3 lmi3;
ZeroMemory(&lmi3, sizeof lmi3);
lmi3.lgrmi3_domainandname = user_info.usri1_name;
DWORD err = NetLocalGroupAddMembers(NULL, L"Users", 3, (LPBYTE) &lmi3, 1);

Sharepoint ResolvePrincipals function

I'm trying to add a user, programmtically, to a field of Type="User" in a SharepointList. Since I do not know the user's unique ID within the site in advance, I'm using the ResolvePrincipals function to add the user to the SPUserCollection as follows:
Dim managerDN() As String = {"some.user#email.com"}
Dim principalInfo() As PrincipalInfo = people.ResolvePrincipals(managerDN, SPPrincipalType.User, True)
Console.WriteLine(principalInfo(0).UserInfoID)
Problem is when I look at the UserInfoID, which is what I'm looking for, I get back -1. I assumed that the ResolvePrincipals function would add the user to the site user collection automatically (According to MSDN documentation) and create a unique, positive UserInfoID in the process. I'm not sure if I have the right idea or not
I always use this code:
SPUser user = web.EnsureUser("login");
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
If you only have an email address, you could use this:
SPUser user = web.AllUsers.GetByEmail("email");
if (user != null)
{
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
}
I'm not really sure if GetByEmail returns null or throws an error if the user cannot be found, so be sure to check that first !