// Generate a list of trusted platform assembiles. bool GetTrustedPlatformAssembliesList(WCHAR* szDirectory, bool bNative, LPWSTR pszTrustedPlatformAssemblies, size_t cchTrustedPlatformAssemblies) { bool ret = true; errno_t errno = 0; WIN32_FIND_DATA ffd = {}; size_t cTpaAssemblyNames = 0; LPWSTR* ppszTpaAssemblyNames = nullptr; // Build the list of the tpa assemblie CreateTpaBase(&ppszTpaAssemblyNames, &cTpaAssemblyNames, bNative); // Scan the directory to see if all the files in TPA list exist for (size_t i = 0; i < cTpaAssemblyNames; ++i) { WCHAR wszPattern[MAX_PATH]; wszPattern[0] = L'\0'; errno = wcscpy_s(wszPattern, _countof(wszPattern), szDirectory); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(wszPattern, _countof(wszPattern), ppszTpaAssemblyNames[i]); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); HANDLE findHandle = FindFirstFile(wszPattern, &ffd); if ((findHandle == INVALID_HANDLE_VALUE) || (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { // if file is missing or a directory is found, breaks the loop and // set the missing flag to true ret = false; goto Finished; } } for (size_t i = 0; i < cTpaAssemblyNames; ++i) { errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, szDirectory); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, ppszTpaAssemblyNames[i]); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, L";"); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); } Finished: if (ppszTpaAssemblyNames != nullptr) { FreeTpaBase(ppszTpaAssemblyNames, cTpaAssemblyNames); } return ret; }
bool ScanDirectory(WCHAR* szDirectory, WCHAR* szPattern, LPWSTR pszTrustedPlatformAssemblies, size_t cchTrustedPlatformAssemblies) { bool ret = true; errno_t errno = 0; WIN32_FIND_DATA ffd = {}; WCHAR wszPattern[MAX_PATH]; wszPattern[0] = L'\0'; errno = wcscpy_s(wszPattern, _countof(wszPattern), szDirectory); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(wszPattern, _countof(wszPattern), szPattern); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); HANDLE findHandle = FindFirstFile(wszPattern, &ffd); if (INVALID_HANDLE_VALUE == findHandle) { ret = false; goto Finished; } do { if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // Skip directories } else { if (wcscmp(ffd.cFileName, L"klr.host.dll") == 0 || wcscmp(ffd.cFileName, L"klr.host.ni.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.ApplicationHost.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.ApplicationHost.ni.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Runtime.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Runtime.ni.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Runtime.Roslyn.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Runtime.Roslyn.ni.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Project.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.Project.ni.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.DesignTimeHost.dll") == 0 || wcscmp(ffd.cFileName, L"Microsoft.Framework.DesignTimeHost.ni.dll") == 0) { // Exclude these assemblies from the TPA list since they need to // be handled by the loader since they depend on assembly neutral // interfaces continue; } errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, szDirectory); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, ffd.cFileName); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); errno = wcscat_s(pszTrustedPlatformAssemblies, cchTrustedPlatformAssemblies, L";"); CHECK_RETURN_VALUE_FAIL_EXIT_VIA_FINISHED_SETSTATE(errno, ret = false); } } while (FindNextFile(findHandle, &ffd) != 0); Finished: return ret; }