CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2005
    Location
    Estonia
    Posts
    35

    Question 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.

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    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.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    May 2005
    Location
    Estonia
    Posts
    35

    Question 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.

  5. #5
    Join Date
    May 2005
    Posts
    4,954

    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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  6. #6
    Join Date
    Mar 2002
    Posts
    350

    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

  7. #7
    Join Date
    May 2005
    Location
    Estonia
    Posts
    35

    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.

  8. #8
    Join Date
    May 2005
    Posts
    4,954

    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&#091;MAX_PATH&#093;={0};
    DWORD cbRef = sizeof(szRef);
    if(!LookupAccountName(NULL, chCurrentUser, &userSID, &dwSID, szRef, &cbRef, 0)) 
    { 
      ::MessageBox(NULL, "cant lookupacc", "error", MB_OK); 
    }
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  9. #9
    Join Date
    May 2005
    Location
    Estonia
    Posts
    35

    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.

  10. #10
    Join Date
    May 2005
    Posts
    4,954

    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.
    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&#091;256&#093; = {0}; 
    GetUserName(chCurrentUser, &dwUserBuf); 
    SID userSID; 
    DWORD dwSID = sizeof(userSID); 
    TCHAR *chSID; 
    char szRef&#091;MAX_PATH&#093;={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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  11. #11
    Join Date
    May 2005
    Location
    Estonia
    Posts
    35

    Exclamation 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
  •  





Click Here to Expand Forum to Full Width

Featured