-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioRouter.cpp
More file actions
167 lines (140 loc) · 4.55 KB
/
Copy pathAudioRouter.cpp
File metadata and controls
167 lines (140 loc) · 4.55 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
159
160
161
162
163
164
165
166
167
// ApplicationLoopback.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <sstream>
#include "LoopbackCapture.h"
#include <vector>
#include <map>
#include <psapi.h>
std::map<DWORD, std::wstring> pid_to_image;
void UpdatePIDMap() {
static std::vector<DWORD> pids(1024, 0);
pids.resize(pids.capacity());
didResize:
DWORD pidsSizeNeeded = 0;
EnumProcesses(pids.data(), (DWORD) (pids.size() * sizeof(DWORD)), &pidsSizeNeeded);
size_t targetSize = pidsSizeNeeded / sizeof(DWORD);
if (targetSize == pids.size()) {
pids.resize(pids.size() * 2);
goto didResize;
}
pids.resize(targetSize);
// populate a new map so we can drop out PIDs that have disappeared
std::map<DWORD, std::wstring> pid_to_image_new;
for (DWORD pid : pids) {
if (pid == 0)
continue; // System Idle Process
{
auto it = pid_to_image.find(pid);
if (it != pid_to_image.end()) {
pid_to_image_new.insert(std::make_pair(it->first, it->second));
continue; // already tracking this one
}
}
wil::unique_handle hprocess(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, /*inheritHandle=*/ false, pid));
if (!hprocess)
continue;
WCHAR imageFilename[1024];
GetProcessImageFileName(hprocess.get(), imageFilename, 1023);
imageFilename[1023] = 0;
pid_to_image_new.insert(std::make_pair(pid, std::wstring(imageFilename)));
}
pid_to_image_new.swap(pid_to_image);
}
DWORD FindPID(const wchar_t* wImageName) {
for (const auto& it : pid_to_image) {
// search from the last trailing slash to find the actual filename (we get fully-qualified path)
size_t off = it.second.rfind(L'\\');
if (off == std::string::npos)
off = 0; // start from the beginning
else
off += 1; // start after the slash
if (!lstrcmpiW(wImageName, it.second.data() + off))
return it.first;
}
return 0;
}
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved) // reserved
{
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
extern "C" __declspec(dllexport) DWORD __stdcall RouterThread(LPWSTR sourceSpecifier) {
try {
THROW_IF_FAILED(Windows::Foundation::Initialize(RO_INIT_MULTITHREADED));
//OutputDebugStringA("RouterThread");
//OutputDebugStringW(sourceSpecifier);
DWORD pid = 0;
bool attachByName = false;
{
wchar_t* endptr = nullptr;
pid = wcstoul(sourceSpecifier, &endptr, 10);
if (*endptr != 0) { // conversion failed
pid = 0;
attachByName = true;
}
}
CLoopbackCapture loopbackCapture;
if (attachByName) {
while (true) {
wil::unique_handle hProcess;
while (!pid && !hProcess) {
UpdatePIDMap();
pid = FindPID(sourceSpecifier);
if (pid == 0) {
Sleep(1000);
continue;
}
hProcess.reset(OpenProcess(SYNCHRONIZE, false, pid));
if (!hProcess) {
std::wstringstream ss;
ss << L"AudioRouter: OpenProcess() failed for PID " << pid;
OutputDebugStringW(ss.str().c_str());
pid = 0;
continue;
}
}
{
std::wstringstream ss;
ss << L"AudioRouter: Attached to PID " << pid;
OutputDebugStringW(ss.str().c_str());
}
loopbackCapture.StartCaptureAsync(pid);
WaitForSingleObject(hProcess.get(), INFINITE);
OutputDebugStringW(L"AudioRouter: Attached process terminated.");
loopbackCapture.StopCaptureAsync();
hProcess.reset();
pid = 0;
}
} else {
// One-shot, by PID
wil::unique_handle hProcess(OpenProcess(SYNCHRONIZE, false, pid));
THROW_LAST_ERROR_IF_NULL(hProcess);
loopbackCapture.StartCaptureAsync(pid);
WaitForSingleObject(hProcess.get(), INFINITE);
OutputDebugStringW(L"AudioRouter: Attached process terminated.");
loopbackCapture.StopCaptureAsync();
}
} catch (const std::exception& ex) {
OutputDebugStringA(ex.what());
}
return 0;
}