source/SProxy/sproxy.cpp
HRESULT LoadUILibrary(LPCTSTR szPath, LPCTSTR szDllName, DWORD dwExFlags, 
                      HINSTANCE *phinstOut, LPTSTR szFullPathOut,size_t sizeInCharacters,
                      LCID *plcidOut)
{
    TCHAR szPathTemp[_MAX_PATH + 1] = _T("");
    HRESULT hr = E_FAIL;
    LCID lcidFound = (LCID)-1;
    size_t nPathEnd = 0;

    // Gotta have this stuff!
	if (szPath==NULL || *szPath == '\0')	   
	{ 
		return E_POINTER; 
	}

    if (szDllName==NULL || *szDllName == '\0') 
	{ 
		return E_POINTER; 
	}

    if (!szPath || !*szPath || !szDllName || !*szDllName)
	{
        return E_INVALIDARG;
	}

    if (phinstOut != NULL)
    {
        *phinstOut = NULL;
    }

    szPathTemp[_MAX_PATH-1] = L'\0';

    // Add \ to the end if necessary
    _tcsncpy_s(szPathTemp,_countof(szPathTemp), szPath, _MAX_PATH-1);
    if (szPathTemp[_TCSNLEN(szPathTemp, _MAX_PATH-1) - 1] != L'\\')
    {
        _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
    }

    // Check if given path even exists
    if (GetFileAttributes(szPathTemp) == 0xFFFFFFFF)
	{
        return E_FAIL;
	}

    nPathEnd = _TCSNLEN(szPathTemp, _MAX_PATH-1);
    
    {	        
		LANGID langid=0;
		if (FAILED(GetUserDefaultUILanguageLegacyCompat(&langid)))
		{
			return E_UNEXPECTED;
		}
        const LCID lcidUser = MAKELCID(langid, SORT_DEFAULT);
        
        LCID rglcid[3];
        rglcid[0] = lcidUser;
        rglcid[1] = MAKELCID(MAKELANGID(PRIMARYLANGID(lcidUser), SUBLANG_DEFAULT), SORTIDFROMLCID(lcidUser));
        rglcid[2] = 0x409;
        for (int i = 0; i < _countof(rglcid); i++)
        {
            TCHAR szNumBuf[10];
            
            // Check if it's the same as any LCID already checked,
            // which is very possible
			int n = 0;
            for (n = 0; n < i; n++)
            {
                if (rglcid[n] == rglcid[i])
                    break;
            }

            if (n < i)
			{
                continue;
			}
            
            szPathTemp[nPathEnd] = L'\0';
			_itot_s(rglcid[i], szNumBuf,_countof(szNumBuf), 10);
            _tcsncat_s(szPathTemp, _countof(szPathTemp),szNumBuf , PATHLEFT(szPathTemp));
            _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
            _tcsncat_s(szPathTemp,_countof(szPathTemp), szDllName, PATHLEFT(szPathTemp));

            if (GetFileAttributes(szPathTemp) != 0xFFFFFFFF)
            {
                lcidFound = rglcid[i];

                hr = S_OK;
                goto Done;
            }
        }
    }

    // None of the default choices exists, so now look for the dll in a folder below
	//the given path (szPath)
    {        
        szPathTemp[nPathEnd] = L'\0';
        _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("*.*"), PATHLEFT(szPathTemp));

		WIN32_FIND_DATA wfdw;
        HANDLE hDirs = FindFirstFile(szPathTemp, &wfdw);
        nPathEnd = _TCSNLEN(szPathTemp, _MAX_PATH-1)-3;
        if (hDirs != INVALID_HANDLE_VALUE)
        {
            while (FindNextFile(hDirs, &wfdw))
            {
                // We are only interested in directories, since at this level, that should
                // be the only thing in this directory, i.e, LCID sub dirs
                if (wfdw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    // Skip current and previous dirs, "." and ".."
                    if (!_tcscmp(wfdw.cFileName, _T(".")) || !_tcscmp(wfdw.cFileName, _T("..")))
                        continue;

                    // Does this dir have a copy of the dll?
                    szPathTemp[nPathEnd] = L'\0';
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), wfdw.cFileName, PATHLEFT(szPathTemp));
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), szDllName, PATHLEFT(szPathTemp));

                    if (GetFileAttributes(szPathTemp) != 0xFFFFFFFF)
                    {
                        // Got it!
                        lcidFound = (LCID)_tstol(wfdw.cFileName);

                        hr = S_OK;
                        break;
                    }
                }
            }

            FindClose(hDirs);
        }
    }

Done:
    if (SUCCEEDED(hr))
    {
        // Set the default LCID
        if (plcidOut)
        {
			if (lcidFound == (LCID)-1) 
			{ 
				return E_UNEXPECTED; 
			}
            *plcidOut = lcidFound;
        }

        // Finally, attempt to load the library
        // Beware!  A dll loaded with LOAD_LIBRARY_AS_DATAFILE won't
        // let you use LoadIcon and things like that (only general calls like
        // FindResource and LoadResource).
        if (phinstOut != NULL)
        {
            *phinstOut = LoadLibraryEx(szPathTemp, NULL, dwExFlags);
            hr = (*phinstOut) ? S_OK : E_FAIL;
        }
        if ( szFullPathOut )
		{
            _tcsncpy_s(szFullPathOut,sizeInCharacters, szPathTemp, _MAX_PATH-1);
		}
    }
 
    return hr;
}
//////////////////////////////////////////////////////////////////////////
//Purpose: Searches for a resource dll in sub directories using a search order
//		   based on szPath - a directory to search res dll below.
//		   see example at .
//Input: szDllName - the string resource dll name to search. Ex: ToolUI.dll 
//Output: TCHAR *szPathOut - filled with absolute path to dll, if found.
//		  size_t sizeInCharacters - buffer size in characters
//Returns: Success (found dll) - S_OK , Failure - E_FAIL or E_UNEXPECTED
//////////////////////////////////////////////////////////////////////////
Searches for a resource dll in sub directories using a search order
based on szPath - a directory to search res dll below.
see example at .
Input: szDllName - the string resource dll name to search. Ex: ToolUI.dll
Output: TCHAR *szPathOut - filled with absolute path to dll, if found.
size_t sizeInCharacters - buffer size in characters
Returns: Success (found dll) - S_OK , Failure - E_FAIL or E_UNEXPECTED
Searches for a resource dll in sub directories using a search order based on szPath - a directory to search res dll below . see example at . Input : szDllName - the string resource dll name to search . Ex : ToolUI . dll Output : TCHAR * szPathOut - filled with absolute path to dll if found . size_t sizeInCharacters - buffer size in characters Returns : Success ( found dll ) - S_OK Failure - E_FAIL or E_UNEXPECTED
HRESULT LoadUILibrary(LPCTSTR szPath, LPCTSTR szDllName, DWORD dwExFlags, 
                      HINSTANCE *phinstOut, LPTSTR szFullPathOut,size_t sizeInCharacters,
                      LCID *plcidOut)
{
    TCHAR szPathTemp[_MAX_PATH + 1] = _T("");
    HRESULT hr = E_FAIL;
    LCID lcidFound = (LCID)-1;
    size_t nPathEnd = 0;
	if (szPath==NULL || *szPath == '\0')	   
	{ 
		return E_POINTER; 
	}
    if (szDllName==NULL || *szDllName == '\0') 
	{ 
		return E_POINTER; 
	}
    if (!szPath || !*szPath || !szDllName || !*szDllName)
	{
        return E_INVALIDARG;
	}
    if (phinstOut != NULL)
    {
        *phinstOut = NULL;
    }
    szPathTemp[_MAX_PATH-1] = L'\0';
    _tcsncpy_s(szPathTemp,_countof(szPathTemp), szPath, _MAX_PATH-1);
    if (szPathTemp[_TCSNLEN(szPathTemp, _MAX_PATH-1) - 1] != L'\\')
    {
        _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
    }
    if (GetFileAttributes(szPathTemp) == 0xFFFFFFFF)
	{
        return E_FAIL;
	}
    nPathEnd = _TCSNLEN(szPathTemp, _MAX_PATH-1);
    {	        
		LANGID langid=0;
		if (FAILED(GetUserDefaultUILanguageLegacyCompat(&langid)))
		{
			return E_UNEXPECTED;
		}
        const LCID lcidUser = MAKELCID(langid, SORT_DEFAULT);
        LCID rglcid[3];
        rglcid[0] = lcidUser;
        rglcid[1] = MAKELCID(MAKELANGID(PRIMARYLANGID(lcidUser), SUBLANG_DEFAULT), SORTIDFROMLCID(lcidUser));
        rglcid[2] = 0x409;
        for (int i = 0; i < _countof(rglcid); i++)
        {
            TCHAR szNumBuf[10];
			int n = 0;
            for (n = 0; n < i; n++)
            {
                if (rglcid[n] == rglcid[i])
                    break;
            }
            if (n < i)
			{
                continue;
			}
            szPathTemp[nPathEnd] = L'\0';
			_itot_s(rglcid[i], szNumBuf,_countof(szNumBuf), 10);
            _tcsncat_s(szPathTemp, _countof(szPathTemp),szNumBuf , PATHLEFT(szPathTemp));
            _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
            _tcsncat_s(szPathTemp,_countof(szPathTemp), szDllName, PATHLEFT(szPathTemp));
            if (GetFileAttributes(szPathTemp) != 0xFFFFFFFF)
            {
                lcidFound = rglcid[i];
                hr = S_OK;
                goto Done;
            }
        }
    }
    {        
        szPathTemp[nPathEnd] = L'\0';
        _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("*.*"), PATHLEFT(szPathTemp));
		WIN32_FIND_DATA wfdw;
        HANDLE hDirs = FindFirstFile(szPathTemp, &wfdw);
        nPathEnd = _TCSNLEN(szPathTemp, _MAX_PATH-1)-3;
        if (hDirs != INVALID_HANDLE_VALUE)
        {
            while (FindNextFile(hDirs, &wfdw))
            {
                if (wfdw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if (!_tcscmp(wfdw.cFileName, _T(".")) || !_tcscmp(wfdw.cFileName, _T("..")))
                        continue;
                    szPathTemp[nPathEnd] = L'\0';
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), wfdw.cFileName, PATHLEFT(szPathTemp));
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
                    _tcsncat_s(szPathTemp,_countof(szPathTemp), szDllName, PATHLEFT(szPathTemp));
                    if (GetFileAttributes(szPathTemp) != 0xFFFFFFFF)
                    {
                        lcidFound = (LCID)_tstol(wfdw.cFileName);
                        hr = S_OK;
                        break;
                    }
                }
            }
            FindClose(hDirs);
        }
    }
Done:
    if (SUCCEEDED(hr))
    {
        if (plcidOut)
        {
			if (lcidFound == (LCID)-1) 
			{ 
				return E_UNEXPECTED; 
			}
            *plcidOut = lcidFound;
        }
        if (phinstOut != NULL)
        {
            *phinstOut = LoadLibraryEx(szPathTemp, NULL, dwExFlags);
            hr = (*phinstOut) ? S_OK : E_FAIL;
        }
        if ( szFullPathOut )
		{
            _tcsncpy_s(szFullPathOut,sizeInCharacters, szPathTemp, _MAX_PATH-1);
		}
    }
    return hr;
}
HRESULT LoadUILibrary ( LPCTSTR szPath , LPCTSTR szDllName , DWORD dwExFlags , HINSTANCE * phinstOut , LPTSTR szFullPathOut , size_t sizeInCharacters , LCID * plcidOut ) { TCHAR szPathTemp [ _MAX_PATH + 1 ] = _T ( " " ) ; HRESULT hr = E_FAIL ; LCID lcidFound = ( LCID ) - 1 ; size_t nPathEnd = 0 ; if ( szPath == NULL || * szPath == ' \0 ' ) { return E_POINTER ; } if ( szDllName == NULL || * szDllName == ' \0 ' ) { return E_POINTER ; } if ( ! szPath || ! * szPath || ! szDllName || ! * szDllName ) { return E_INVALIDARG ; } if ( phinstOut != NULL ) { * phinstOut = NULL ; } szPathTemp [ _MAX_PATH - 1 ] = L' \0 ' ; _tcsncpy_s ( szPathTemp , _countof ( szPathTemp ) , szPath , _MAX_PATH - 1 ) ; if ( szPathTemp [ _TCSNLEN ( szPathTemp , _MAX_PATH - 1 ) - 1 ] != L' \\ ' ) { _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , _T ( " \\ " ) , PATHLEFT ( szPathTemp ) ) ; } if ( GetFileAttributes ( szPathTemp ) == 0xFFFFFFFF ) { return E_FAIL ; } nPathEnd = _TCSNLEN ( szPathTemp , _MAX_PATH - 1 ) ; { LANGID langid = 0 ; if ( FAILED ( GetUserDefaultUILanguageLegacyCompat ( & langid ) ) ) { return E_UNEXPECTED ; } const LCID lcidUser = MAKELCID ( langid , SORT_DEFAULT ) ; LCID rglcid [ 3 ] ; rglcid [ 0 ] = lcidUser ; rglcid [ 1 ] = MAKELCID ( MAKELANGID ( PRIMARYLANGID ( lcidUser ) , SUBLANG_DEFAULT ) , SORTIDFROMLCID ( lcidUser ) ) ; rglcid [ 2 ] = 0x409 ; for ( int i = 0 ; i < _countof ( rglcid ) ; i ++ ) { TCHAR szNumBuf [ 10 ] ; int n = 0 ; for ( n = 0 ; n < i ; n ++ ) { if ( rglcid [ n ] == rglcid [ i ] ) break ; } if ( n < i ) { continue ; } szPathTemp [ nPathEnd ] = L' \0 ' ; _itot_s ( rglcid [ i ] , szNumBuf , _countof ( szNumBuf ) , 10 ) ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , szNumBuf , PATHLEFT ( szPathTemp ) ) ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , _T ( " \\ " ) , PATHLEFT ( szPathTemp ) ) ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , szDllName , PATHLEFT ( szPathTemp ) ) ; if ( GetFileAttributes ( szPathTemp ) != 0xFFFFFFFF ) { lcidFound = rglcid [ i ] ; hr = S_OK ; goto Done ; } } } { szPathTemp [ nPathEnd ] = L' \0 ' ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , _T ( " " ) , PATHLEFT ( szPathTemp ) ) ; WIN32_FIND_DATA wfdw ; HANDLE hDirs = FindFirstFile ( szPathTemp , & wfdw ) ; nPathEnd = _TCSNLEN ( szPathTemp , _MAX_PATH - 1 ) - 3 ; if ( hDirs != INVALID_HANDLE_VALUE ) { while ( FindNextFile ( hDirs , & wfdw ) ) { if ( wfdw . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { if ( ! _tcscmp ( wfdw . cFileName , _T ( " " ) ) || ! _tcscmp ( wfdw . cFileName , _T ( " " ) ) ) continue ; szPathTemp [ nPathEnd ] = L' \0 ' ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , wfdw . cFileName , PATHLEFT ( szPathTemp ) ) ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , _T ( " \\ " ) , PATHLEFT ( szPathTemp ) ) ; _tcsncat_s ( szPathTemp , _countof ( szPathTemp ) , szDllName , PATHLEFT ( szPathTemp ) ) ; if ( GetFileAttributes ( szPathTemp ) != 0xFFFFFFFF ) { lcidFound = ( LCID ) _tstol ( wfdw . cFileName ) ; hr = S_OK ; break ; } } } FindClose ( hDirs ) ; } } Done : if ( SUCCEEDED ( hr ) ) { if ( plcidOut ) { if ( lcidFound == ( LCID ) - 1 ) { return E_UNEXPECTED ; } * plcidOut = lcidFound ; } if ( phinstOut != NULL ) { * phinstOut = LoadLibraryEx ( szPathTemp , NULL , dwExFlags ) ; hr = ( * phinstOut ) ? S_OK : E_FAIL ; } if ( szFullPathOut ) { _tcsncpy_s ( szFullPathOut , sizeInCharacters , szPathTemp , _MAX_PATH - 1 ) ; } } return hr ; }
Purpose: Searches for a resource dll in sub directories using a search order
based on szPath - a directory to search res dll below.
Purpose : Searches for a resource dll in sub directories using a search order based on szPath - a directory to search res dll below .
// Gotta have this stuff! // Add \ to the end if necessary // Check if given path even exists // Check if it's the same as any LCID already checked, // which is very possible // None of the default choices exists, so now look for the dll in a folder below //the given path (szPath) // We are only interested in directories, since at this level, that should // be the only thing in this directory, i.e, LCID sub dirs // Skip current and previous dirs, "." and ".." // Does this dir have a copy of the dll? // Got it! // Set the default LCID // Finally, attempt to load the library // Beware!  A dll loaded with LOAD_LIBRARY_AS_DATAFILE won't // let you use LoadIcon and things like that (only general calls like // FindResource and LoadResource).