rentzsch/mach_inject

Inject code to deal with NSWindow

Noitidart opened this issue · 5 comments

Hi there,
I used to create Firefox addons and would run code from within the process to manipulate the NSWindow's. NSWindow's, NSAlert's, etc (UIKit stuff) can only be manipulated from the main thread. ( UIKit operations for OSX and iOS must be on main thread - http://stackoverflow.com/questions/18467114/why-must-uikit-operations-be-performed-on-the-main-thread _

However recently there was a change and all code is from another process (native messaging API), so I needed to inject into the target process (browser/Firefox).

I was researching mach_inject and it seems to create a new thread in the targeted process. I was thinking of doing dispatch_async like this:

dispatch_async(dispatch_get_main_queue(), ^{
	NSAlert *alert = [[[NSAlert alloc] init] autorelease];
	[alert setMessageText:@"Hi there from main thread"];
	[alert runModal];
});

Do you think this will work? I'm very new to writing C/C++/ObjC so your reply would help my morale a lot :)

The provided code snippet is a perfect way of scheduling something to be run on the main queue/thread.

Thank you so much!! I have been struggling with this for some time, I will give mach_inject a try and let you know how it goes! :)

Sure, but the problem with mach_inject is that it won't work under System Integrity Protection which is enabled by default on 10.11+

Ah thanks very much for that note sir, I'll try to find a way to turn it off and then back on after my injection is done.

Oops excuse this sir I forgot to close this. Thanks very much for leaving it open for me to close it. Closed :)