|
-
December 7th, 2005, 03:53 PM
#1
ConvertSidToStringSid
Code:
#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 ?
If you lose a moment,
You may lose your life.
-
December 7th, 2005, 04:35 PM
#2
Re: ConvertSidToStringSid
 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
-
December 7th, 2005, 04:43 PM
#3
Re: ConvertSidToStringSid
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:
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.
-
December 7th, 2005, 06:54 PM
#4
Re: ConvertSidToStringSid
****...
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);
If you lose a moment,
You may lose your life.
-
December 8th, 2005, 02:58 AM
#5
Re: ConvertSidToStringSid
 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
-
December 8th, 2005, 05:45 AM
#6
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
-
December 9th, 2005, 02:07 AM
#7
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:
if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, NULL, NULL, NULL))
But i cant understand why ???? What im doing wrong ?
If you lose a moment,
You may lose your life.
-
December 9th, 2005, 05:42 AM
#8
Re: ConvertSidToStringSid
 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
-
December 9th, 2005, 08:03 AM
#9
Re: ConvertSidToStringSid
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:
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.
If you lose a moment,
You may lose your life.
-
December 9th, 2005, 09:35 AM
#10
Re: ConvertSidToStringSid
 Originally Posted by ProgMaster
Maybe just try it on your PC and u'll understand where is mistake and can correct me. 
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
-
December 9th, 2005, 05:08 PM
#11
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 ?
If you lose a moment,
You may lose your life.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|