#include "stdafx.h"
#include <windows.h>
#define
WINNT 0x0500
#define INFO L"Ausgabe der SIDs zu einem Principal\n"
#define QUERYINPUT L"Principal (z.B. Benutzername): "
DWORD GetStrSID(LPCWSTR server, LPCWSTR principal, WCHAR StrSID[80])
{
PSID Sid = NULL;
DWORD cbSize = 0;
if (LookupAccountNameW(NULL, principal, NULL, &cbSize, NULL, NULL, NULL))
{
if(Sid = PSID(new BYTE[cbSize]))
{
LPWSTR lpwszTemp = NULL;
if(LookupAccountNameW(NULL, principal, Sid, &cbSize, NULL, NULL, NULL))
{
if(ConvertSidToStringSidW(Sid, &lpwszTemp))
{
// Fill with zeros
memset(StrSID, 0, sizeof(StrSID));
// Copy string into buffer
memcpy(StrSID, lpwszTemp, sizeof(StrSID) - sizeof(WCHAR));
// Free the string allocated by ConvertSidToStringSidW
LocalFree(HLOCAL(lpwszTemp));
SetLastError(ERROR_SUCCESS);
}
wprintf(L"%i", cbSize);
}
delete Sid;
}
else
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
}
}
return GetLastError();
}
int wmain(int argc, _TCHAR* argv[])
{
WCHAR principal[80] = {0};
DWORD LastError;
wprintf(INFO);
wprintf(QUERYINPUT);
wscanf(L"%S", principal);
if (ERROR_SUCCESS == (LastError = GetStrSID(NULL, principal, principal)))
{
wprintf(L"%s", principal);
}
else
{
wprintf(L"Error %d", LastError);
}
getchar();
return 0;
}