-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (117 loc) · 3.75 KB
/
main.cpp
File metadata and controls
158 lines (117 loc) · 3.75 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#define UNICODE
#include <format>
#include <windows.h>
#include <shobjidl.h>
#include <comdef.h>
#include <condition_variable>
#include <thread>
std::atomic dialogThreadCount { 0 };
std::condition_variable threadZeroCond {};
std::mutex threadZeroMutex {};
void QuickDialogFail(const wchar_t *msg) {
MessageBoxW(
nullptr,
msg,
L"fuck",
MB_ICONERROR | MB_OK
);
}
void QuickDialog(const wchar_t *msg) {
MessageBoxW(
nullptr,
msg,
L"log",
MB_ICONINFORMATION | MB_OK
);
}
void QuickDialogAsync(const wchar_t *msg) {
dialogThreadCount.fetch_add(1);
std::thread {
[msg] {
QuickDialog(msg);
// checking if the old value is 1
if (dialogThreadCount.fetch_sub(1) == 1) {
std::lock_guard lock(threadZeroMutex); // locks mutex until destructor is called
threadZeroCond.notify_one();
}
}
}.detach();
};
void ResultFail(HRESULT hr, const wchar_t *msg) {
if (FAILED(hr)) {
_com_error err{hr};
// tchar is a wchar_t because UNICODE is defined
auto *errMsg = (wchar_t *) err.ErrorMessage();
QuickDialogFail(std::format(L"{}, HRESULT: {}", msg, errMsg).c_str());
exit(1);
}
}
class DialogEventHandler : public IFileDialogEvents {
ULONG ref = 1;
HRESULT showExitCode = 0;
public:
virtual ~DialogEventHandler() = default;
HRESULT QueryInterface(const IID &riid, void **ppvObject) override {
if (!ppvObject) {
return E_POINTER;
}
if (riid == _uuidof(IFileDialogEvents) || riid == _uuidof(IUnknown)) {
*ppvObject = (IFileDialogEvents *) this;
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
ULONG AddRef() override { return ++ref; }
ULONG Release() override {
ULONG count = --ref;
if (count == 0) { delete this; }
return count;
}
HRESULT OnFileOk(IFileDialog *pfd) override {
wchar_t *wMsg = nullptr;
const HRESULT hr = pfd->GetFileName(&wMsg);
ResultFail(hr, L"could not extract file name from file dialog");
QuickDialogAsync(wMsg);
CoTaskMemFree(wMsg);
return S_OK;
}
HRESULT OnFolderChanging(IFileDialog *pfd, IShellItem *psiFolder) override { return S_OK; }
HRESULT OnFolderChange(IFileDialog *pfd) override { return S_OK; }
HRESULT OnSelectionChange(IFileDialog *pfd) override { return S_OK; }
HRESULT OnShareViolation(IFileDialog *pfd, IShellItem *psi, FDE_SHAREVIOLATION_RESPONSE *pResponse) override {
return S_OK;
}
HRESULT OnTypeChange(IFileDialog *pfd) override { return S_OK; }
HRESULT OnOverwrite(IFileDialog *pfd, IShellItem *psi, FDE_OVERWRITE_RESPONSE *pResponse) override { return S_OK; }
};
int WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
HRESULT hr = 0;
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
ResultFail(hr, L"could not init com server");
IFileDialog *dialog = nullptr;
hr = CoCreateInstance(
CLSID_FileOpenDialog,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IFileDialog,
(void **) &dialog
);
ResultFail(hr, L"could not create file dialog object");
// event handling object
IFileDialogEvents *events = new DialogEventHandler{};
DWORD cookie = 0;
hr = dialog->Advise(events, &cookie);
ResultFail(hr, L"advise");
hr = dialog->Show(nullptr);
ResultFail(hr, L"show");
std::unique_lock lk (threadZeroMutex);
threadZeroCond.wait(lk, [] {
return dialogThreadCount.load() == 0;
});
// cleanup
hr = dialog->Unadvise(cookie);
ResultFail(hr, L"unadvise");
dialog->Release();
CoUninitialize();
}