-
Notifications
You must be signed in to change notification settings - Fork 3
/
FindMedia.h
96 lines (79 loc) · 3.22 KB
/
FindMedia.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//--------------------------------------------------------------------------------------
// File: FindMedia.h
//
// Helper function to find the location of a media file for Windows desktop apps
// since they lack appx packaging support.
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//-------------------------------------------------------------------------------------
#pragma once
#include <cstddef>
#include <cstring>
#include <stdexcept>
namespace DX
{
inline void FindMediaFile(
_Out_writes_(cchDest) wchar_t* strDestPath,
_In_ int cchDest,
_In_z_ const wchar_t* strFilename)
{
bool bFound = false;
if (!strFilename || strFilename[0] == 0 || !strDestPath || cchDest < 10)
throw std::invalid_argument("FindMediaFile");
// Get the exe name, and exe path
wchar_t strExePath[MAX_PATH] = {};
wchar_t strExeName[MAX_PATH] = {};
wchar_t* strLastSlash = nullptr;
std::ignore = GetModuleFileNameW(nullptr, strExePath, MAX_PATH);
strExePath[MAX_PATH - 1] = 0;
strLastSlash = wcsrchr(strExePath, TEXT('\\'));
if (strLastSlash)
{
wcscpy_s(strExeName, MAX_PATH, &strLastSlash[1]);
// Chop the exe name from the exe path
*strLastSlash = 0;
// Chop the .exe from the exe name
strLastSlash = wcsrchr(strExeName, TEXT('.'));
if (strLastSlash)
*strLastSlash = 0;
}
wcscpy_s(strDestPath, cchDest, strFilename);
if (GetFileAttributesW(strDestPath) != 0xFFFFFFFF)
return;
// Search all parent directories starting at .\ and using strFilename as the leaf name
wchar_t strLeafName[MAX_PATH] = {};
wcscpy_s(strLeafName, MAX_PATH, strFilename);
wchar_t strFullPath[MAX_PATH] = {};
wchar_t strFullFileName[MAX_PATH] = {};
wchar_t strSearch[MAX_PATH] = {};
wchar_t* strFilePart = nullptr;
std::ignore = GetFullPathNameW(strExePath, MAX_PATH, strFullPath, &strFilePart);
if (!strFilePart)
throw std::runtime_error("FindMediaFile");
while (strFilePart && *strFilePart != '\0')
{
swprintf_s(strFullFileName, MAX_PATH, L"%ls\\%ls", strFullPath, strLeafName);
if (GetFileAttributesW(strFullFileName) != 0xFFFFFFFF)
{
wcscpy_s(strDestPath, cchDest, strFullFileName);
bFound = true;
break;
}
swprintf_s(strFullFileName, MAX_PATH, L"%ls\\%ls\\%ls", strFullPath, strExeName, strLeafName);
if (GetFileAttributesW(strFullFileName) != 0xFFFFFFFF)
{
wcscpy_s(strDestPath, cchDest, strFullFileName);
bFound = true;
break;
}
swprintf_s(strSearch, MAX_PATH, L"%ls\\..", strFullPath);
std::ignore = GetFullPathNameW(strSearch, MAX_PATH, strFullPath, &strFilePart);
}
if (bFound)
return;
// On failure, return the file as the path but also throw an error
wcscpy_s(strDestPath, size_t(cchDest), strFilename);
throw std::runtime_error("File not found");
}
}