Click to See Complete Forum and Search --> : ConvertSidToStringSid


ProgMaster
December 7th, 2005, 02:53 PM
#include <sddl.h>
//...
DWORD dwUserBuf = 256;
char chCurrentUser[256] = {0};
GetUserName(chCurrentUser, &dwUserBuf);
SID userSID;
DWORD dwSID;
char *chSID;
LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL);
ConvertSidToStringSid(&userSID, chSID);

Thats how im trying to convert variable SID to readable form. But i receive this error:

error C3861: 'ConvertSidToStringSid': identifier not found, even with argument-dependent lookup

Header file i've included. What else compiler wants ?

golanshahar
December 7th, 2005, 03:35 PM
Header file i've included. What else compiler wants ?

the latest sdk. you need to download the latest Platform SDK. ;)

however you can try use it with dynamic loading probably it will work:


typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,LPTSTR);

tConvertSidToStringSid pConvertSidToStringSid=0;

HINSTANCE handle = ::LoadLibrary(_T("Advapi32.dll"));
if (handle == NULL)
return;

pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, chSID);
}

::FreeLibrary(handle);



Cheers

NoHero
December 7th, 2005, 03:43 PM
Msdn says:

Client: Requires Windows XP or Windows 2000 Professional.
Server: Requires Windows Server 2003 or Windows 2000 Server.

Thus you need to redefine some macros before you can use this functions:

#define WIN32_WINNT 0x0501
#define WINVER 0x0501 // both combied stand for Windows XP

#include <sddl.h>


Not your program will fail when run under Win2k and lower. So golansahar's solution with proper error handling might be the better choice.

ProgMaster
December 7th, 2005, 05:54 PM
****...
The memory could not be "read"
That what i receive. How to solve this problem ??? Here is my code:

// Receiving currently logged user
DWORD dwUserBuf = 256;
char chCurrentUser[256] = {0};
GetUserName(chCurrentUser, &dwUserBuf);
SID userSID;
DWORD dwSID;
TCHAR chSID[256];
LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL);

// Loading ConvertSidToStringSid
typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,LPTSTR);

tConvertSidToStringSid pConvertSidToStringSid=0;

HINSTANCE handle = ::LoadLibrary("Advapi32.dll");
if (handle == NULL) return;

pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, chSID);
}
::FreeLibrary(handle);
MessageBox(NULL, chSID, "SID is:", MB_OK);

golanshahar
December 8th, 2005, 01:58 AM
****...

That what i receive. How to solve this problem ??? Here is my code:


after which call you getting that error message?

Cheers

AdaraCD
December 8th, 2005, 04:45 AM
try this:



LPSTR lpSid = NULL;

if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, &lpSid);
}

MessageBox(NULL, lpSid ? lpSid: "N/A", "SID is:", MB_OK);


dont forget to clean after with LocalFree

ProgMaster
December 9th, 2005, 01:07 AM
Here is my new code:

// Receiving currently loggged user name
DWORD dwUserBuf = 256;
char chCurrentUser[256] = {0};
GetUserName(chCurrentUser, &dwUserBuf);
SID userSID;
DWORD dwSID = sizeof(userSID);
TCHAR *chSID;
MessageBox(NULL, "step 1", "step 1", MB_OK);
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL))
{
MessageBox(NULL, "cant lookupacc", "error", MB_OK);
}

// Loading ConvertSidToStringSid
MessageBox(NULL, "step 2", "step 2", MB_OK);
typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,LPTSTR*);

tConvertSidToStringSid pConvertSidToStringSid=0;

HINSTANCE handle = ::LoadLibrary("Advapi32.dll");
if (handle == NULL) return;

MessageBox(NULL, "step 3", "step 3", MB_OK);
pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, &chSID);
}
::FreeLibrary(handle);
MessageBox(NULL, chSID, "SID", MB_OK);
LocalFree((HLOCAL)chSID);

I've tested this and what i found. Message "step 1" i receive, but second message i dont receive. Thats means that it crashes at:
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL))
But i cant understand why ???? What im doing wrong ?

golanshahar
December 9th, 2005, 04:42 AM
I've tested this and what i found. Message "step 1" i receive, but second message i dont receive. Thats means that it crashes at:

But i cant understand why ???? What im doing wrong ?

try this:

char szRef[MAX_PATH]={0};
DWORD cbRef = sizeof(szRef);
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, 0))
{
::MessageBox(NULL, "cant lookupacc", "error", MB_OK);
}


Cheers

ProgMaster
December 9th, 2005, 07:03 AM
Applied all you tip me. But error didnt dissappear :(. Repeating: i receive exception with message:
the memory cound bot be "read"
This error appears on line after first MessageBox (Step 1), i mean here:
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, NULL))
before step 2.

Here is once more UPDATED code:

// Retrieving currently logged user name
DWORD dwUserBuf = 256;
char chCurrentUser[256] = {0};

GetUserName(chCurrentUser, &dwUserBuf);

PSID userSID;
DWORD dwSID = 1024*512;
TCHAR *chSID;
char szRef[MAX_PATH]={0};
DWORD cbRef = sizeof(szRef);

MessageBox(NULL, "step 1", "step 1", MB_OK);

if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, NULL))
{
MessageBox(NULL, "cant lookupaccountname", "error", MB_OK);
}

// Loading ConvertSidToStringSid
MessageBox(NULL, "step 2", "step 2", MB_OK);
typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,LPTSTR*);

tConvertSidToStringSid pConvertSidToStringSid=0;

HINSTANCE handle = ::LoadLibrary("Advapi32.dll");
if (handle == NULL) return;

MessageBox(NULL, "step 3", "step 3", MB_OK);

pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, &chSID);
}
::FreeLibrary(handle);

MessageBox(NULL, chSID, "SID", MB_OK);
LocalFree((HLOCAL)chSID);


Maybe just try it on your PC and u'll understand where is mistake and can correct me. :cry:

golanshahar
December 9th, 2005, 08:35 AM
Maybe just try it on your PC and u'll understand where is mistake and can correct me. :cry:

i did check it on my computer before posting! your first code really crashed and when i called the function properly like i posted you it didnt crash.

now from some odd reason you decided to change the code and changed the:

DWORD dwSID = sizeof(userSID);


to:

DWORD dwSID = 1024*512;


which you shouldnt!

anyway here is the correct code:


DWORD dwUserBuf = 256;
char chCurrentUser[256] = {0};
GetUserName(chCurrentUser, &dwUserBuf);
SID userSID;
DWORD dwSID = sizeof(userSID);
TCHAR *chSID;
char szRef[MAX_PATH]={0};
DWORD cbRef = sizeof(szRef);
::MessageBox(NULL, "step 1", "step 1", MB_OK);
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, NULL))
{
::MessageBox(NULL, "cant lookupacc", "error", MB_OK);
}



Cheers

ProgMaster
December 9th, 2005, 04:08 PM
IVE MADE IT :))). Here is properly working code:


// Retrieving currently logged user name
DWORD dwUserBuf = 256;
char chCurrentUser[256];

GetUserName(chCurrentUser, &dwUserBuf);
MessageBox(NULL, chCurrentUser, "User name", MB_OK);

PSID userSID = NULL;
DWORD dwSID, dwDomainNameSize = 0;
BYTE bySidBuffer[1024];
char chSID[256], chDomainName[1024];
SID_NAME_USE snu;

userSID = (PSID)bySidBuffer;
dwSID = sizeof(bySidBuffer);
dwDomainNameSize = sizeof(chDomainName);

if(!LookupAccountName(NULL, (LPCSTR)chCurrentUser, (PSID)userSID, (LPDWORD)&dwSID, (LPTSTR)chDomainName, (LPDWORD)&dwDomainNameSize, (PSID_NAME_USE)&snu))
{
MessageBox(NULL, "can't LookupAccountName", "error", MB_OK);
}

// Loading ConvertSidToStringSid
typedef BOOL (WINAPI *tConvertSidToStringSid)(PSID,char*);

tConvertSidToStringSid pConvertSidToStringSid=0;

HINSTANCE handle = ::LoadLibrary("Advapi32.dll");
if (handle == NULL)
return;

pConvertSidToStringSid = (tConvertSidToStringSid) ::GetProcAddress(handle, "ConvertSidToStringSidA");
if (pConvertSidToStringSid)
{
/// call the fucntion with your parameter
pConvertSidToStringSid(&userSID, chSID);
}
::FreeLibrary(handle);
MessageBox(NULL, chSID, "SID", MB_OK);
LocalFree((HLOCAL)chSID);


But now there is ANOTHER PROBLEM !!! Because im service, GetUserName returns me "SYSTEM" :(. How can i now to know what is REAL CURRENT LY LOGGED USER name ?