Search code examples
c++objective-cosx-mavericksobjective-c-runtime

Objective C Runtime to access screen dimensions


I am working on a project where using Cocoa and Objective C is not desired.

I stumbled across the Objective C runtime library the other day and have been trying to use it to access the dimensions of the screen.

This is the code I have currently:

auto screen = objc_msgSend(objc_lookUpClass("NSScreen"),sel_getUid("mainScreen")); //Returns an NSScreen*
auto frame = objc_msgSend(screen, sel_getUid("frame")); // should return an NSRect but I get an EXC_BAD_ACCESS and the program quits

Using Cocoa isn't an option and Carbon isn't really supported anymore. I'm hoping that someone is familiar with the Objc Runtime and is able to solve this problem.


Solution

  • If you really want to avoid Objective-C in getting this information, you'd need to dive into low level Quartz (and good luck with that!).

    I think the commands that would help you would be:

    CGGetActiveDisplayList and CGDisplayScreenSize