HRESULT DXUtil_GetDXSDKMediaPathCb( TCHAR* szDest, int cbDest ) { return DXUtil_GetDXSDKMediaPathCch( szDest, cbDest / sizeof(TCHAR) ); }
//----------------------------------------------------------------------------- // Name: DXUtil_FindMediaFileCch() // Desc: Trys to find the location of a SDK media file // cchDest is the size in TCHARs of strDestPath. Be careful not to // pass in sizeof(strDest) on UNICODE builds. //----------------------------------------------------------------------------- HRESULT DXUtil_FindMediaFileCch( TCHAR* strDestPath, int cchDest, LPCTSTR strFilename ) { bool bFound; TCHAR strSearchFor[MAX_PATH]; if( NULL==strFilename || strFilename[0] == 0 || NULL==strDestPath || cchDest < 10 ) return E_INVALIDARG; // Get the DirectX SDK's media dir if the SDK is installed TCHAR strMediaDir[MAX_PATH] = {0}; DXUtil_GetDXSDKMediaPathCch( strMediaDir, MAX_PATH ); // Get the exe name, and exe path TCHAR strExePath[MAX_PATH] = {0}; TCHAR strExeName[MAX_PATH] = {0}; TCHAR* strLastSlash = NULL; GetModuleFileName( NULL, strExePath, MAX_PATH ); strExePath[MAX_PATH-1]=0; strLastSlash = _tcsrchr( strExePath, TEXT('\\') ); if( strLastSlash ) { lstrcpyn( strExeName, &strLastSlash[1], MAX_PATH ); // Chop the exe name from the exe path *strLastSlash = 0; // Chop the .exe from the exe name strLastSlash = _tcsrchr( strExeName, TEXT('.') ); if( strLastSlash ) *strLastSlash = 0; } // Typical directories: // .\ // ..\ // ..\..\ // %EXE_DIR%\ // %EXE_DIR%\..\ // %EXE_DIR%\..\..\ // %EXE_DIR%\..\%EXE_NAME% // %EXE_DIR%\..\..\%EXE_NAME% // DXSDK media path // Typical directory search bFound = DXUtil_FindMediaSearchTypicalDirs( strDestPath, cchDest, strFilename, strExePath, strExeName, strMediaDir ); if( bFound ) return S_OK; // Typical directory search again, but also look in a subdir called "\media\" _sntprintf( strSearchFor, MAX_PATH, TEXT("media\\%s"), strFilename ); strSearchFor[MAX_PATH-1] = 0; bFound = DXUtil_FindMediaSearchTypicalDirs( strDestPath, cchDest, strSearchFor, strExePath, strExeName, strMediaDir ); if( bFound ) return S_OK; TCHAR strLeafName[MAX_PATH] = {0}; // Search all parent directories starting at .\ and using strFilename as the leaf name _tcsncpy( strLeafName, strFilename, MAX_PATH ); strLeafName[MAX_PATH-1] = 0; bFound = DXUtil_FindMediaSearchParentDirs( strDestPath, cchDest, TEXT("."), strLeafName ); if( bFound ) return S_OK; // Search all parent directories starting at the exe's dir and using strFilename as the leaf name bFound = DXUtil_FindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName ); if( bFound ) return S_OK; // Search all parent directories starting at .\ and using "media\strFilename" as the leaf name _sntprintf( strLeafName, MAX_PATH, TEXT("media\\%s"), strFilename ); strLeafName[MAX_PATH-1] = 0; bFound = DXUtil_FindMediaSearchParentDirs( strDestPath, cchDest, TEXT("."), strLeafName ); if( bFound ) return S_OK; // Search all parent directories starting at the exe's dir and using "media\strFilename" as the leaf name bFound = DXUtil_FindMediaSearchParentDirs( strDestPath, cchDest, strExePath, strLeafName ); if( bFound ) return S_OK; // On failure, return the file as the path but also return an error code _tcsncpy( strDestPath, strFilename, cchDest ); strDestPath[cchDest-1] = 0; return E_FAIL; }
//----------------------------------------------------------------------------- // Name: DXUtil_FindMediaFileCch() // Desc: Returns a valid path to a DXSDK media file // cchDest is the size in TCHARs of strDestPath. Be careful not to // pass in sizeof(strDest) on UNICODE builds. //----------------------------------------------------------------------------- HRESULT DXUtil_FindMediaFileCch( TCHAR* strDestPath, int cchDest, TCHAR* strFilename ) { HRESULT hr; HANDLE file; TCHAR* strShortNameTmp = NULL; TCHAR strShortName[MAX_PATH]; int cchPath; if( NULL==strFilename || NULL==strDestPath || cchDest < 1 ) return E_INVALIDARG; lstrcpy( strDestPath, TEXT("") ); lstrcpy( strShortName, TEXT("") ); // Build full path name from strFileName (strShortName will be just the leaf filename) cchPath = GetFullPathName(strFilename, cchDest, strDestPath, &strShortNameTmp); if ((cchPath == 0) || (cchDest <= cchPath)) return E_FAIL; if( strShortNameTmp ) lstrcpyn( strShortName, strShortNameTmp, MAX_PATH ); // first try to find the filename given a full path file = CreateFile( strDestPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( INVALID_HANDLE_VALUE != file ) { CloseHandle( file ); return S_OK; } // next try to find the filename in the current working directory (path stripped) file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( INVALID_HANDLE_VALUE != file ) { _tcsncpy( strDestPath, strShortName, cchDest ); strDestPath[cchDest-1] = 0; // _tcsncpy doesn't NULL term if it runs out of space CloseHandle( file ); return S_OK; } // last, check if the file exists in the media directory if( FAILED( hr = DXUtil_GetDXSDKMediaPathCch( strDestPath, cchDest ) ) ) return hr; if( lstrlen(strDestPath) + lstrlen(strShortName) < cchDest ) lstrcat( strDestPath, strShortName ); else return E_INVALIDARG; file = CreateFile( strDestPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if( INVALID_HANDLE_VALUE != file ) { CloseHandle( file ); return S_OK; } // On failure, just return the file as the path _tcsncpy( strDestPath, strFilename, cchDest ); strDestPath[cchDest-1] = 0; // _tcsncpy doesn't NULL term if it runs out of space return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND ); }