mirror of
https://github.com/reactos/reactos.git
synced 2025-07-03 01:21:23 +00:00
implemented GetUserNameA() and GetUserNameW() - thanks to kjk_hyperion and Alex Ionescu for help on how to implement them.
svn path=/trunk/; revision=12075
This commit is contained in:
parent
d2b0c045cd
commit
55c08f71b9
1 changed files with 119 additions and 27 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* $Id: misc.c,v 1.28 2004/12/12 21:25:04 weiden Exp $
|
/* $Id: misc.c,v 1.29 2004/12/13 07:09:56 royce Exp $
|
||||||
*
|
*
|
||||||
* COPYRIGHT: See COPYING in the top level directory
|
* COPYRIGHT: See COPYING in the top level directory
|
||||||
* PROJECT: ReactOS system libraries
|
* PROJECT: ReactOS system libraries
|
||||||
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "advapi32.h"
|
#include "advapi32.h"
|
||||||
#include <accctrl.h>
|
#include <accctrl.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
#define NDEBUG
|
#define NDEBUG
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
@ -478,31 +479,40 @@ RevertToSelf(VOID)
|
||||||
* lpSize [I/O] Size of lpszName.
|
* lpSize [I/O] Size of lpszName.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @unimplemented
|
* @implemented
|
||||||
*/
|
*/
|
||||||
BOOL WINAPI
|
BOOL WINAPI
|
||||||
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
||||||
{
|
{
|
||||||
size_t len;
|
WCHAR* lpszNameW = NULL;
|
||||||
const char* name = "Administrator";
|
DWORD len = 0;
|
||||||
|
|
||||||
DPRINT1("GetUserNameA: stub\n");
|
|
||||||
if ( !lpSize )
|
if ( !lpSize )
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_INVALID_PARAMETER);
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
/* We need to include the null character when determining the size of the buffer. */
|
|
||||||
len = strlen(name) + 1;
|
len = *lpSize;
|
||||||
if (len > *lpSize)
|
lpszNameW = LocalAlloc ( LMEM_FIXED, len * sizeof(WCHAR) );
|
||||||
|
|
||||||
|
if ( !GetUserNameW ( lpszNameW, &len ) )
|
||||||
{
|
{
|
||||||
SetLastError(ERROR_MORE_DATA);
|
LocalFree ( lpszNameW );
|
||||||
*lpSize = len;
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
len = wcstombs ( lpszName, lpszNameW, len );
|
||||||
|
|
||||||
|
LocalFree ( lpszNameW );
|
||||||
|
|
||||||
|
if ( len > *lpSize )
|
||||||
|
{
|
||||||
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
*lpSize = len;
|
*lpSize = len;
|
||||||
strcpy(lpszName, name);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,26 +521,108 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
||||||
*
|
*
|
||||||
* See GetUserNameA.
|
* See GetUserNameA.
|
||||||
*
|
*
|
||||||
* @unimplemented
|
* @implemented
|
||||||
*/
|
*/
|
||||||
BOOL WINAPI
|
BOOL WINAPI
|
||||||
GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
|
GetUserNameW ( LPWSTR lpszName, LPDWORD lpSize )
|
||||||
{
|
{
|
||||||
// char name[] = { "Administrator" };
|
HANDLE hToken = INVALID_HANDLE_VALUE;
|
||||||
|
DWORD tu_len = 0;
|
||||||
|
char* tu_buf = NULL;
|
||||||
|
TOKEN_USER* token_user = NULL;
|
||||||
|
DWORD an_len = 0;
|
||||||
|
SID_NAME_USE snu = SidTypeUser;
|
||||||
|
WCHAR* domain_name = NULL;
|
||||||
|
DWORD dn_len = 0;
|
||||||
|
|
||||||
// DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
|
if ( !OpenThreadToken ( GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken ) )
|
||||||
|
{
|
||||||
|
DWORD dwLastError = GetLastError();
|
||||||
|
if ( dwLastError != ERROR_NO_TOKEN
|
||||||
|
&& dwLastError != ERROR_NO_IMPERSONATION_TOKEN )
|
||||||
|
{
|
||||||
|
// don't call SetLastError(),
|
||||||
|
// as OpenThreadToken() ought to have set one
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if ( !OpenProcessToken ( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
|
||||||
|
{
|
||||||
|
// don't call SetLastError(),
|
||||||
|
// as OpenProcessToken() ought to have set one
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tu_buf = LocalAlloc ( LMEM_FIXED, 36 );
|
||||||
|
if ( !tu_buf )
|
||||||
|
{
|
||||||
|
SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if ( !GetTokenInformation ( hToken, TokenUser, tu_buf, 36, &tu_len ) || tu_len > 36 )
|
||||||
|
{
|
||||||
|
LocalFree ( tu_buf );
|
||||||
|
tu_buf = LocalAlloc ( LMEM_FIXED, tu_len );
|
||||||
|
if ( !tu_buf )
|
||||||
|
{
|
||||||
|
SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if ( !GetTokenInformation ( hToken, TokenUser, tu_buf, tu_len, &tu_len ) )
|
||||||
|
{
|
||||||
|
// don't call SetLastError(),
|
||||||
|
// as GetTokenInformation() ought to have set one
|
||||||
|
LocalFree ( tu_buf );
|
||||||
|
CloseHandle ( hToken );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token_user = (TOKEN_USER*)tu_buf;
|
||||||
|
|
||||||
// if (len > *lpSize)
|
an_len = *lpSize;
|
||||||
// {
|
dn_len = 32;
|
||||||
// SetLastError(ERROR_MORE_DATA);
|
domain_name = LocalAlloc ( LMEM_FIXED, dn_len * sizeof(WCHAR) );
|
||||||
// *lpSize = len;
|
if ( !domain_name )
|
||||||
// return FALSE;
|
{
|
||||||
// }
|
LocalFree ( tu_buf );
|
||||||
|
SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
if ( !LookupAccountSidW ( NULL, token_user->User.Sid, lpszName, &an_len, domain_name, &dn_len, &snu )
|
||||||
|
|| dn_len > 32 )
|
||||||
|
{
|
||||||
|
if ( dn_len > 32 )
|
||||||
|
{
|
||||||
|
LocalFree ( domain_name );
|
||||||
|
domain_name = LocalAlloc ( LMEM_FIXED, dn_len * sizeof(WCHAR) );
|
||||||
|
if ( !domain_name )
|
||||||
|
{
|
||||||
|
LocalFree ( tu_buf );
|
||||||
|
SetLastError ( ERROR_NOT_ENOUGH_MEMORY );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( !LookupAccountSidW ( NULL, token_user->User.Sid, lpszName, &an_len, domain_name, &dn_len, &snu ) )
|
||||||
|
{
|
||||||
|
// don't call SetLastError(),
|
||||||
|
// as LookupAccountSid() ought to have set one
|
||||||
|
LocalFree ( domain_name );
|
||||||
|
CloseHandle ( hToken );
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// *lpSize = len;
|
LocalFree ( domain_name );
|
||||||
// MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
|
LocalFree ( tu_buf );
|
||||||
DPRINT1("GetUserNameW: stub\n");
|
CloseHandle ( hToken );
|
||||||
return TRUE;
|
|
||||||
|
if ( an_len > *lpSize )
|
||||||
|
{
|
||||||
|
*lpSize = an_len;
|
||||||
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue