CBHacking
CBHacking

Reputation: 2122

Share named mutex between UWP and desktop app

Is there any way to share a mutex between a UWP app and a desktop bridge app in the same package? They seem to have different namespaces; using the same name doesn't yield the same object between the processes. The UWP app's objects are, according to WinObj, stored in a special namespace under AppContainerNamedObjects\{APP_SID} rather than under BaseNamedObjects like usual. However, the desktop app - despite running from the same app bundle - does use the BaseNamedObjects namespace, and consequently the two processes can't share synchronization objects.

Is there any way around this? The best I've thought of involves silly things like checking for files to exist or not, but this is both overly complicated and underperformant. Mutexes are simple, fast, and designed for exactly this use case (synchronization across processes); can they just not be used here?

Upvotes: 6

Views: 971

Answers (1)

eric frazer
eric frazer

Reputation: 1648

You can share a mutex between UWP and desktop, though I don't know if it's a hack. I know, I just tried it. On the C++ side, you call CreateMutex, then (somehow), you get told via interprocess communication the PID of the UWP app. Then, you call DuplicateHandle with SYNCHRONIZE acess on the mutex you created. Then, you pass these handle ID's to the UWP app which now owns the mutex and can wait on it. In the UWP app, you need to create the mutex, then call SetSafeWaitHandle( ) on it with the mutex ID you passed it. it's hacky, but it seems to work. What I can't figure out is why it won't work for AutoResetEvents. Crazy.

Upvotes: 2

Related Questions