Re: ConvertSidToStringSid
Quote:
Originally Posted by ProgMaster
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:
Code:
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
Re: ConvertSidToStringSid
Msdn says:
Quote:
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:
Code:
#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.
Re: ConvertSidToStringSid
****...
Quote:
The memory could not be "read"
That what i receive. How to solve this problem ??? Here is my code:
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);
Re: ConvertSidToStringSid
Quote:
Originally Posted by ProgMaster
****...
That what i receive. How to solve this problem ??? Here is my code:
after which call you getting that error message?
Cheers
Re: ConvertSidToStringSid
try this:
Code:
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
Re: ConvertSidToStringSid
Here is my new code:
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:
Quote:
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL))
But i cant understand why ???? What im doing wrong ?
Re: ConvertSidToStringSid
Quote:
Originally Posted by ProgMaster
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:
Code:
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
Re: ConvertSidToStringSid
Applied all you tip me. But error didnt dissappear :(. Repeating: i receive exception with message:
Quote:
the memory cound bot be "read"
This error appears on line after first MessageBox (Step 1), i mean here:
Code:
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, NULL))
before step 2.
Here is once more UPDATED code:
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:
Re: ConvertSidToStringSid
Quote:
Originally Posted by ProgMaster
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:
Code:
DWORD dwSID = sizeof(userSID);
to:
Code:
DWORD dwSID = 1024*512;
which you shouldnt!
anyway here is the correct code:
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
Re: ConvertSidToStringSid
IVE MADE IT :))). Here is properly working code:
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 ?