Let’s Bind an IOKit Method by Hand

Chris Hamons
Mar 28, 2018 · 4 min read
io_service_t IOServiceGetMatchingService(mach_port_t masterPort, CFDictionaryRef matching);
@import IOKit;
[DllImport (“/System/Library/Frameworks/IOKit.framework/IOKit”)]
static extern blah IOServiceGetMatchingService (blah masterPort, blah matching);
typedef io_object_t io_service_t;
typedef mach_port_t io_object_t;
NSLog (@”%d”, sizeof (unsigned int));
[DllImport (“/System/Library/Frameworks/IOKit.framework/IOKit”)]
static extern int IOServiceGetMatchingService (blah masterPort, blah matching);
[DllImport (“/System/Library/Frameworks/IOKit.framework/IOKit”)]
static extern int IOServiceGetMatchingService (int masterPort, blah matching);
[DllImport (“/System/Library/Frameworks/IOKit.framework/IOKit”)]
static extern int IOServiceGetMatchingService (int masterPort, IntPtr matching);
/*! @const kIOMasterPortDefault
@abstract The default mach port used to initiate communication with IOKit.
@discussion When specifying a master port to IOKit functions, the NULL argument indicates “use the default”. This is a synonym for NULL, if you’d rather use a named constant.
*/
extern
const mach_port_t kIOMasterPortDefault;
int service = IOServiceGetMatchingService (0, some_pointer);
using (NSDictionary d = new NSDictionary ()) {
int service = IOServiceGetMatchingService (0, d.Handle);
}
CFMutableDictionaryRef
IOServiceMatching(
const char * name ) CF_RETURNS_RETAINED;
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
static extern IntPtr IOServiceMatching (string name);
[DllImport ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")]
extern static void CFRelease (IntPtr obj);
The dictionary is commonly passed to IOServiceGetMatchingServices or IOServiceAddNotification which will consume a reference, otherwise it should be released with CFRelease by the caller.
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
extern static int IOServiceClose (int service);
IntPtr serviceDescription = IOServiceMatching ("IOPCIDevice");
int service = IOServiceGetMatchingService (0, serviceDescription);
Console.WriteLine (service);
IOServiceClose (service);

Chris Hamons

Written by

C# Monkey, Xamarin.Mac Lead

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade