HRESULT FileOrDirectoryExists(PathString &path) { HRESULT hr = S_FALSE; DWORD dwFileAttributes = WszGetFileAttributes(path.GetUnicode()); if (dwFileAttributes == INVALID_FILE_ATTRIBUTES) { hr = HRESULT_FROM_GetLastError(); if ((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))) { hr = S_FALSE; } } else { hr = S_TRUE; } return hr; }
//***************************************************************************** // This is used in conjunction with GetClassFromCORPath. See it for details // of the algorithm. One thing to note is that the dir passed here must be // _MAX_PATH size and will be written to by this routine. This routine will // frequently leave junk at the end of the directory string and dir[iLen] may // not be '\0' on return. //***************************************************************************** HRESULT CORPATHService::GetClassFromDir( __in __in_z LPWSTR wzClassname, // Fully qualified class name. __in __in_z LPWSTR dir, // Directory to try. int iLen, // Length of the directory. mdTypeRef tr, // TypeRef to resolve. IMetaModelCommon *pCommon, // Scope in which the TypeRef is defined. REFIID riid, IUnknown **ppIScope, mdTypeDef *ptd) // [OUT] typedef { WCHAR *temp; // Used as a parsing temp. int iTmp; bool bContinue; // Flag to check if the for loop should end. LPWSTR wzSaveClassname = NULL; // Saved offset into the class name string. int iSaveLen = 0; // Saved length of the dir string. PREFIX_ASSUME(iLen >= 0); // Process the class name appending each segment of the name to the // directory until we find a DLL. for(;;) { bContinue = false; if ((temp = wcschr(wzClassname, NAMESPACE_SEPARATOR_WCHAR)) != NULL) { iTmp = (int) (temp - wzClassname); // Check for buffer overflow with correct integer overflow check. // "if (iLen + 5 + iTmp >= _MAX_PATH)" if (ovadd_ge(iLen, iTmp, (_MAX_PATH - 5))) break; // Append the next segment from the class spec to the directory. dir[iLen++] = W('\\'); wcsncpy_s(dir+iLen, iTmp+1, wzClassname, iTmp); iLen += iTmp; dir[iLen] = W('\0'); wzClassname = temp+1; // Check if a directory by this name exists. DWORD iAttrs = WszGetFileAttributes(dir); if (iAttrs != 0xffffffff && (iAttrs & FILE_ATTRIBUTE_DIRECTORY)) { // Next element in the class spec. bContinue = true; iSaveLen = iLen; wzSaveClassname = wzClassname; } } else { iTmp = (int)wcslen(wzClassname); // Check for buffer overflow with correct integer overflow check. // "if (iLen + 5 + iTmp >= _MAX_PATH)" if (ovadd_ge(iLen, iTmp, (_MAX_PATH - 5))) break; dir[iLen++] = W('\\'); wcscpy_s(dir+iLen, iTmp+1, wzClassname); // Advance past the class name. iLen += iTmp; wzClassname += iTmp; } // Try to load the image. wcscpy_s(dir+iLen, 5, W(".dll")); // OpenScope given the dll name and make sure that the class is defined in the module. if ( SUCCEEDED( CORPATHService::FindTypeDef(dir, tr, pCommon, riid, ppIScope, ptd) ) ) { return (S_OK); } // If we didn't find the dll, try some more. while (*wzClassname != W('\0')) { // Find the length of the next class name element. if ((temp = wcschr(wzClassname, NAMESPACE_SEPARATOR_WCHAR)) == NULL) temp = wzClassname + wcslen(wzClassname); iTmp = (int) (temp - wzClassname); // Check for buffer overflow. if ((iLen + 5 + iTmp) >= _MAX_PATH) break; // Tack on ".element.dll" dir[iLen++] = W('.'); wcsncpy_s(dir+iLen, iTmp+1, wzClassname, iTmp); iLen += iTmp; // Try to load the image. wcscpy_s(dir+iLen, 5, W(".dll")); // OpenScope given the dll name and make sure that the class is defined in the module. if ( SUCCEEDED( CORPATHService::FindTypeDef(dir, tr, pCommon, riid, ppIScope, ptd) ) ) { return (S_OK); } // Advance to the next class name element. wzClassname = temp; if (*wzClassname != '\0') ++wzClassname; } if (bContinue) { iLen = iSaveLen; wzClassname = wzSaveClassname; } else { break; } } return S_FALSE; } // CORPATHService::GetClassFromDir
//***************************************************************************** // This is used in conjunction with GetClassFromCORPath. See it for details // of the algorithm. //***************************************************************************** HRESULT CORPATHService::GetClassFromDir( __in __in_z LPWSTR wzClassname, // Fully qualified class name. __in SString& directory, // Directory to try. at most appended with a '\\' mdTypeRef tr, // TypeRef to resolve. IMetaModelCommon *pCommon, // Scope in which the TypeRef is defined. REFIID riid, IUnknown **ppIScope, mdTypeDef *ptd) // [OUT] typedef { WCHAR *temp; // Used as a parsing temp. int iTmp; bool bContinue; // Flag to check if the for loop should end. LPWSTR wzSaveClassname = NULL; // Saved offset into the class name string. // Process the class name appending each segment of the name to the // directory until we find a DLL. PathString dir; if (!directory.EndsWith(DIRECTORY_SEPARATOR_CHAR_W)) { directory.Append(DIRECTORY_SEPARATOR_CHAR_W); } for(;;) { bContinue = false; dir.Set(directory); if ((temp = wcschr(wzClassname, NAMESPACE_SEPARATOR_WCHAR)) != NULL) { *temp = W('\0'); //terminate with null so that it can be appended dir.Append(wzClassname); *temp = NAMESPACE_SEPARATOR_WCHAR; //recover the '.' wzClassname = temp+1; // Check if a directory by this name exists. DWORD iAttrs = WszGetFileAttributes(dir); if (iAttrs != 0xffffffff && (iAttrs & FILE_ATTRIBUTE_DIRECTORY)) { // Next element in the class spec. bContinue = true; wzSaveClassname = wzClassname; } } else { dir.Append(wzClassname); // Advance past the class name. iTmp = (int)wcslen(wzClassname); wzClassname += iTmp; } // Try to load the image. dir.Append(W(".dll")); // OpenScope given the dll name and make sure that the class is defined in the module. if ( SUCCEEDED( CORPATHService::FindTypeDef(dir, tr, pCommon, riid, ppIScope, ptd) ) ) { return (S_OK); } // If we didn't find the dll, try some more. while (*wzClassname != W('\0')) { // Find the length of the next class name element. if ((temp = wcschr(wzClassname, NAMESPACE_SEPARATOR_WCHAR)) == NULL) { temp = wzClassname + wcslen(wzClassname); } // Tack on ".element.dll" SString::Iterator iter = dir.End(); BOOL findperiod = dir.FindBack(iter, NAMESPACE_SEPARATOR_WCHAR); _ASSERTE(findperiod); iter++; dir.Truncate(iter); WCHAR save = *temp; *temp = W('\0'); dir.Append(wzClassname); //element *temp = save; // Try to load the image. dir.Append(W(".dll")); // OpenScope given the dll name and make sure that the class is defined in the module. if ( SUCCEEDED( CORPATHService::FindTypeDef(dir, tr, pCommon, riid, ppIScope, ptd) ) ) { return (S_OK); } // Advance to the next class name element. wzClassname = temp; if (*wzClassname != '\0') ++wzClassname; } if (bContinue) { wzClassname = wzSaveClassname; } else { break; } } return S_FALSE; } // CORPATHService::GetClassFromDir