#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#ifndef ConvertSidToStringSid
#ifdef __cplusplus
extern "C" {
#endif
BOOL
WINAPI
ConvertSidToStringSidA(
IN PSID Sid,
OUT LPSTR *StringSid
);
BOOL
WINAPI
ConvertSidToStringSidW(
IN PSID Sid,
OUT LPWSTR *StringSid
);
#ifdef
UNICODE
#define ConvertSidToStringSid ConvertSidToStringSidW
#else
#define ConvertSidToStringSid ConvertSidToStringSidA
#endif // !
UNICODE
#ifdef __cplusplus
};
#endif
#endif // ConvertSidToStringSid
#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);
}
}
delete Sid;
}
else
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
}
}
return GetLastError();
}
int __cdecl _tmain(int argc, _TCHAR* argv[])
{
WCHAR principal[80] = {0};
DWORD LastError;
wprintf(INFO);
wprintf(QUERYINPUT);
wscanf(L"%S", principal);
wprintf(L"%S\n", principal);
if (ERROR_SUCCESS == (LastError = GetStrSID(NULL, principal, principal)))
{
wprintf(L"%s", principal);
}
else
{
wprintf(L"Error %d", LastError);
}
getchar();
return 0;
}