/*++ Function : PAL_GetPALDirectoryW Returns the fully qualified path name where the PALL DLL was loaded from. On failure it returns FALSE and sets the proper LastError code. See rotor_pal.doc for more details. --*/ PALIMPORT BOOL PALAPI PAL_GetPALDirectoryW( OUT LPWSTR lpDirectoryName, IN OUT UINT* cchDirectoryName ) { PathWCharString directory; BOOL bRet; PERF_ENTRY(PAL_GetPALDirectoryW); ENTRY( "PAL_GetPALDirectoryW( %p, %d )\n", lpDirectoryName, *cchDirectoryName ); bRet = PAL_GetPALDirectoryW(directory); if (bRet) { if (directory.GetCount() > *cchDirectoryName) { SetLastError( ERROR_INSUFFICIENT_BUFFER ); bRet = FALSE; } else { PAL_wcscpy(lpDirectoryName, directory.GetString()); } *cchDirectoryName = directory.GetCount(); } LOGEXIT( "PAL_GetPALDirectoryW returns BOOL %d.\n", bRet); PERF_EXIT(PAL_GetPALDirectoryW); return bRet; }
// Try to load a message DLL from a location, in a subdirectory of mine given by the LANGID // passed in (or the same directory if -1). static HINSTANCE FindMessageDll(LANGID langid) { WCHAR path[MAX_PATH]; WCHAR * pEnd; if (!PAL_GetPALDirectoryW(path, lengthof(path))) return 0; pEnd = path + wcslen(path); // Append language ID if (langid != (LANGID)-1) { pEnd += _snwprintf(pEnd, lengthof(path) - (pEnd - path) - 1, L"%d\\", langid); } // Append message DLL name. if ((int) lengthof(MESSAGE_DLL) + pEnd - path > (int) lengthof(path) - 1) return 0; wcscpy(pEnd, MESSAGE_DLL); // Try to load it. return PAL_LoadSatelliteResourceW(path); }
BOOL PAL_GetPALDirectoryA(PathCharString& lpDirectoryName) { BOOL bRet; PathWCharString directory; PERF_ENTRY(PAL_GetPALDirectoryA); bRet = PAL_GetPALDirectoryW(directory); if (bRet) { int length = WideCharToMultiByte(CP_ACP, 0, directory.GetString(), -1, NULL, 0, NULL, 0); LPSTR DirectoryName = lpDirectoryName.OpenStringBuffer(length); if (NULL == DirectoryName) { SetLastError( ERROR_INSUFFICIENT_BUFFER ); bRet = FALSE; } length = WideCharToMultiByte(CP_ACP, 0, directory.GetString(), -1, DirectoryName, length, NULL, 0); if (0 == length) { bRet = FALSE; length++; } lpDirectoryName.CloseBuffer(length - 1); } PERF_EXIT(PAL_GetPALDirectoryA); return bRet; }
int __cdecl main(int argc, char *argv[]) { int err; BOOL bValue; DWORD dwFileAttribute; WCHAR *wpDirectoryName = NULL; char *pDirectoryName = NULL; /*Initialize the PAL environment*/ err = PAL_Initialize(argc, argv); if(0 != err) { return FAIL; } /*allocate momory to store the directory name*/ wpDirectoryName = malloc(MAX_PATH*sizeof(WCHAR)); if(NULL == wpDirectoryName) { Fail("\nFailed to allocate memory for storing directory name!\n"); } /*retrieve the machine configuration directory*/ bValue = PAL_GetPALDirectoryW(wpDirectoryName, MAX_PATH); if(FALSE == bValue) { free(wpDirectoryName); Fail("Failed to call PAL_GetPALDirectoryW API, " "error code =%u\n", GetLastError()); } /*convert wide char string to a standard one*/ pDirectoryName = convertC(wpDirectoryName); if(0 == strlen(pDirectoryName)) { free(wpDirectoryName); free(pDirectoryName); Fail("The retrieved directory name string is empty!\n"); } /*free the memory*/ free(pDirectoryName); /*retrieve the attribute of a file or directory*/ dwFileAttribute = GetFileAttributesW(wpDirectoryName); /*free the memory*/ free(wpDirectoryName); /*check if the attribute indicates a directory*/ if(FILE_ATTRIBUTE_DIRECTORY != (dwFileAttribute & FILE_ATTRIBUTE_DIRECTORY)) { Fail("The retrieved directory name is not a valid directory!\n"); } PAL_Terminate(); return PASS; }
PALIMPORT BOOL PALAPI PAL_GetPALDirectoryA( OUT LPSTR lpDirectoryName, IN UINT cchDirectoryName) { BOOL bRet; WCHAR PALDirW[_MAX_PATH]; PERF_ENTRY(PAL_GetPALDirectoryA); ENTRY( "PAL_GetPALDirectoryA( %p, %d )\n", lpDirectoryName, cchDirectoryName ); bRet = PAL_GetPALDirectoryW(PALDirW, _MAX_PATH); if (bRet) { if (WideCharToMultiByte(CP_ACP, 0, PALDirW, -1, lpDirectoryName, cchDirectoryName, NULL, 0)) { bRet = TRUE; } else { bRet = FALSE; } } LOGEXIT( "PAL_GetPALDirectoryW returns BOOL %d.\n", bRet); PERF_EXIT(PAL_GetPALDirectoryA); return bRet; }
/* PAL_GetMachineConfigurationDirectoryW * ------------------------------------- * Locates the machine configuration directory for the PAL. This * is a "rotor" subdirectory of the location of the PAL. The * directory is created the first time this function is called * if it doesn't currently exist. * * Parameters: * OUT LPWSTR lpDirectoryName: The path to the machine * configuration directory * IN UINT cbDirectoryName: The size in bytes of the buffer * pointed to by lpDirectoryName * * Returns: Whether lpDirectoryName was filled in with the * location of the machine configuration directory. */ EXTERN_C BOOL PALAPI PAL_GetMachineConfigurationDirectoryW( OUT LPWSTR lpDirectoryName, IN UINT cbDirectoryName) { static BOOL bCreated = FALSE; BOOL b = FALSE; DWORD dw; dw = PAL_GetPALDirectoryW(lpDirectoryName, cbDirectoryName); if (dw == 0 || dw >= cbDirectoryName) { goto LExit; } // Append "rotor" to the path, which already has a trailing // separator. if (wcslen(lpDirectoryName) + sizeof(CONFIG_DIRECTORY)/sizeof(WCHAR) >= cbDirectoryName) { goto LExit; } wcscat(lpDirectoryName, CONFIG_DIRECTORY); // Try to create the directory on the first call. if (!bCreated) { CreateDirectoryW(lpDirectoryName, NULL); bCreated = TRUE; } b = TRUE; LExit: return b; }
HRESULT PALAPI PAL_CoCreateInstance(REFCLSID rclsid, REFIID riid, void **ppv) { for (size_t i = 0; i < sizeof(g_CoClasses) / sizeof(g_CoClasses[0]); i++) { const CoClass *pCoClass = &g_CoClasses[i]; if (*pCoClass->pClsid == rclsid) { HRESULT hr; HINSTANCE dll; DLLGETCLASSOBJECT *dllGetClassObject; IClassFactory *classFactory; WCHAR FullPath[_MAX_PATH]; if (!PAL_GetPALDirectoryW(FullPath, _MAX_PATH)) { goto Win32Error; } if (wcslen(FullPath) + wcslen(pCoClass->dllName) >= _MAX_PATH) { SetLastError(ERROR_FILENAME_EXCED_RANGE); goto Win32Error; } wcsncat(FullPath, pCoClass->dllName, _MAX_PATH); dll = LoadLibraryW(FullPath); if (dll == NULL) goto Win32Error; dllGetClassObject = (DLLGETCLASSOBJECT*)GetProcAddress(dll, "DllGetClassObject"); if (dllGetClassObject == NULL) { // The CLR shim exports a DllGetClassObject which in turn decides which DLL to load and // call DllGetClassObjectInternal on. Without the shim, the PALRT must do the same // here. dllGetClassObject = (DLLGETCLASSOBJECT*)GetProcAddress(dll, "DllGetClassObjectInternal"); if (dllGetClassObject == NULL) { goto Win32Error; } } hr = (*dllGetClassObject)(rclsid, IID_IClassFactory, (void**)&classFactory); if (FAILED(hr)) return hr; hr = classFactory->CreateInstance(NULL, riid, ppv); classFactory->Release(); return hr; } } _ASSERTE(!"Unknown CLSID in PAL_CoCreateInstance"); return CLASS_E_CLASSNOTAVAILABLE; Win32Error: DWORD dwError = GetLastError(); return HRESULT_FROM_WIN32(dwError); }
/*++ Function : FMTMSG_FormatMessageInit Loads the dynamic library, resolves symbols. Loads the satellite file into memory. --*/ static HSATELLITE FMTMSG_FormatMessageInit( void ) { static const WCHAR ROTORPALSATFILE[] = { 'p','a','l','.','s','a','t','e','l','l','i','t','e', '\0' }; WCHAR SatPathAndFile[ MAX_PATH ]; HSATELLITE hSatellite; LPVOID lpLibRotorPalRt; TRACE( "Initilizing the dynamic library and the satellite files.\n" ); lpLibRotorPalRt = s_lpLibRotorPalRt; if ( !lpLibRotorPalRt ) { lpLibRotorPalRt = FMTMSG_LoadLibrary( ); if ( !lpLibRotorPalRt ) { ERROR( "Unable to load the shared library. Reason %s.\n", dlerror() ); goto error; } } /* Get the symbols. */ LoadSatelliteResource = reinterpret_cast<FnLoadSatelliteResource>( dlsym( lpLibRotorPalRt, "PAL_LoadSatelliteResourceW" )); FreeSatelliteResource = reinterpret_cast<FnFreeSatelliteResource>( dlsym( lpLibRotorPalRt, "PAL_FreeSatelliteResource" )); LoadSatelliteStringW = reinterpret_cast<FnLoadSatelliteStringW>( dlsym( lpLibRotorPalRt, "PAL_LoadSatelliteStringW" )); if ( !LoadSatelliteResource || !FreeSatelliteResource || !LoadSatelliteStringW ) { ERROR( "Unable to load the shared library symbols. " "Reason %s.\n", dlerror() ); goto error; } /* Load the satellite file. */ if ( !PAL_GetPALDirectoryW( SatPathAndFile, MAX_PATH ) ) { ERROR( "Unable to retrieve the path.\n" ); goto error; } PAL_wcsncat( SatPathAndFile, ROTORPALSATFILE, MAX_PATH ); hSatellite = ((*LoadSatelliteResource)( SatPathAndFile )); if ( !hSatellite ) { ERROR( "Unable to load the satellite file\n" ); goto error; } if ( InterlockedCompareExchangePointer(&s_hSatellite, hSatellite, NULL) != NULL ) { /* somebody beat us to it */ (*FreeSatelliteResource)(hSatellite); } error: return s_hSatellite; }