1
0
Эх сурвалжийг харах

win-capture: Use full app obj name for keepalive mutex

Ensures that the UWP program can open the keepalive mutex to check to
see whether OBS is still alive.  Fixes a bug where UWP programs wouldn't
capture.
jp9000 5 жил өмнө
parent
commit
c76426c5bd

+ 8 - 0
plugins/win-capture/app-helpers.c

@@ -53,6 +53,14 @@ wchar_t *get_app_sid(HANDLE process)
 static const wchar_t *path_format =
 	L"\\Sessions\\%lu\\AppContainerNamedObjects\\%s\\%s";
 
+HANDLE create_app_mutex(const wchar_t *sid, const wchar_t *name)
+{
+	wchar_t path[MAX_PATH];
+	DWORD session_id = WTSGetActiveConsoleSessionId();
+	_snwprintf(path, MAX_PATH, path_format, session_id, sid, name);
+	return nt_create_mutex(path);
+}
+
 HANDLE open_app_mutex(const wchar_t *sid, const wchar_t *name)
 {
 	wchar_t path[MAX_PATH];

+ 1 - 0
plugins/win-capture/app-helpers.h

@@ -4,6 +4,7 @@
 
 extern bool is_app(HANDLE process);
 extern wchar_t *get_app_sid(HANDLE process);
+extern HANDLE create_app_mutex(const wchar_t *sid, const wchar_t *name);
 extern HANDLE open_app_mutex(const wchar_t *sid, const wchar_t *name);
 extern HANDLE open_app_event(const wchar_t *sid, const wchar_t *name);
 extern HANDLE open_app_map(const wchar_t *sid, const wchar_t *name);

+ 3 - 1
plugins/win-capture/game-capture.c

@@ -676,7 +676,9 @@ static inline bool init_keepalive(struct game_capture *gc)
 	wchar_t new_name[64];
 	swprintf(new_name, 64, WINDOW_HOOK_KEEPALIVE L"%lu", gc->process_id);
 
-	gc->keepalive_mutex = CreateMutexW(NULL, false, new_name);
+	gc->keepalive_mutex = gc->is_app
+				      ? create_app_mutex(gc->app_sid, new_name)
+				      : CreateMutexW(NULL, false, new_name);
 	if (!gc->keepalive_mutex) {
 		warn("Failed to create keepalive mutex: %lu", GetLastError());
 		return false;

+ 30 - 0
plugins/win-capture/nt-stuff.h

@@ -56,6 +56,9 @@ typedef void(WINAPI *RTLINITUNICODESTRINGFUNC)(PCUNICODE_STRING pstr,
 					       const wchar_t *lpstrName);
 typedef NTSTATUS(WINAPI *NTOPENFUNC)(PHANDLE phandle, ACCESS_MASK access,
 				     POBJECT_ATTRIBUTES objattr);
+typedef NTSTATUS(WINAPI *NTCREATEMUTANT)(PHANDLE phandle, ACCESS_MASK access,
+					 POBJECT_ATTRIBUTES objattr,
+					 BOOLEAN isowner);
 typedef ULONG(WINAPI *RTLNTSTATUSTODOSERRORFUNC)(NTSTATUS status);
 typedef NTSTATUS(WINAPI *NTQUERYSYSTEMINFORMATIONFUNC)(SYSTEM_INFORMATION_CLASS,
 						       PVOID, ULONG, PULONG);
@@ -206,3 +209,30 @@ fail:
 MAKE_NT_OPEN_FUNC(nt_open_mutex, NtOpenMutant, SYNCHRONIZE)
 MAKE_NT_OPEN_FUNC(nt_open_event, NtOpenEvent, EVENT_MODIFY_STATE | SYNCHRONIZE)
 MAKE_NT_OPEN_FUNC(nt_open_map, NtOpenSection, FILE_MAP_READ | FILE_MAP_WRITE)
+
+static HANDLE nt_create_mutex(const wchar_t *name)
+{
+	static bool initialized = false;
+	static NTCREATEMUTANT create = NULL;
+	HANDLE handle;
+	NTSTATUS status;
+	UNICODE_STRING unistr;
+	OBJECT_ATTRIBUTES attr;
+
+	if (!initialized) {
+		create = (NTCREATEMUTANT)get_nt_func("NtCreateMutant");
+		initialized = true;
+	}
+
+	if (!create)
+		return NULL;
+
+	rtl_init_str(&unistr, name);
+	init_named_attribs(&attr, &unistr);
+
+	status = create(&handle, SYNCHRONIZE, &attr, FALSE);
+	if (NT_SUCCESS(status))
+		return handle;
+	nt_set_last_error(status);
+	return NULL;
+}