SHResolveUserNames

Go to Home Page

Translates a delimited list of string sids to user names

Syntax

HRESULT WINAPI SHResolveUserNames (
    LPCWSTR pwszSids,
    LPWSTR pwszNames,
    UINT nameLen
)

Parameters

pwszSids
A list of string sids in the format of '<compName>:<sid>' delimited by semi-colons. <compname> must be the same for every entry in the list
pwszNames
Pointer to a buffer to store the translated names
nameLen
Length of the pwszNames buffer in characters

Return Value

S_OK on success or a standard COM error code or HRESULT_FROM_WIN32(GetLastError()) on failure

Remarks

The names returned in pwszNames are delimited by semi-colons.

No trailing semi-colon is allowed at the end of the pwszSids and the '<compname>' portion will be truncated to 15 characters if it is any longer than that.

CoInitialize(NULL)/CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) must be called before this function.

This function is also known as _SHResolveUserNames@12.

Example

#define _WIN32_DCOM
#include <windows.h>
#include <objbase.h>
#include <stdio.h>
typedef HRESULT (WINAPI*pfnResolve)(LPCWSTR, LPWSTR, UINT);

int main()
{
    HRESULT hr = S_OK;
    HMODULE hShell = LoadLibraryW(L"shell32.dll");
    pfnResolve shResolveUserNames = (pfnResolve)GetProcAddress(hShell, (LPCSTR)270);
    /* 'Laptop' is my computer name */
    LPCWSTR pwszSids = L"Laptop:S-1-1-0;Laptop:S-1-2-1;Laptop:S-1-5-11";
    WCHAR wszNameBuffer[500] = {0};
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    hr = shResolveUserNames(pwszSids, wszNameBuffer, ARRAYSIZE(wszNameBuffer));
    if(SUCCEEDED(hr))
    {
        LPCWSTR pwszNameBuffer = wszNameBuffer;
        LPCWSTR pwszSidsIter = pwszSids;
        LPCWSTR pwszNameBufferIter = wszNameBuffer;
        while(pwszSidsIter && pwszSids)
        {
            pwszSidsIter = wcschr(pwszSids, L';');
            pwszNameBufferIter = wcschr(pwszNameBuffer, L';');
            if(pwszSidsIter && pwszNameBufferIter)
            {
                wprintf(
                    L"%.*s = %.*s\n",
                    pwszSidsIter - pwszSids,
                    pwszSids,
                    pwszNameBufferIter - pwszNameBuffer,
                    pwszNameBuffer
                );
                ++pwszSidsIter; ++pwszNameBufferIter;
            }
            else
            {
                wprintf(L"%s = %s\n", pwszSids, pwszNameBuffer);;
            }
            pwszSids = pwszSidsIter;
            pwszNameBuffer = pwszNameBufferIter;
        }
    }
    FreeLibrary(hShell);
    CoUninitialize();
    return 0;
}
OUTPUT
------
Laptop:S-1-1-0 = Everyone
Laptop:S-1-2-1 = CONSOLE LOGON
Laptop:S-1-5-11 = Authenticated Users