//IOKitTest.h

#include <IOKit/IOService.h>

class com_osxkernel_driver_IOKitTest : public IOService
{
	OSDeclareDefaultStructors(com_osxkernel_driver_IOKitTest)
	
public:	
	virtual bool	init (OSDictionary* dictionary = NULL);
	virtual void	free (void);
	
	virtual IOService*	probe (IOService* provider, SInt32* score);
	virtual bool	start (IOService* provider);
	virtual void	stop (IOService* provider);
};

//IOKitTest.cpp

#include "IOKitTest.h"
#include <IOKit/IOLib.h>

//定义超类
#define super IOService
//表示提供构造与析构函数的声明 以及元数据
OSDefineMetaClassAndStructors(com_osxkernel_driver_IOKitTest, IOService)

//构造函数
bool com_osxkernel_driver_IOKitTest::init (OSDictionary* dict)
{
	bool res = super::init(dict);
	IOLog("IOKitTest::init\n");
	return res;
}

void com_osxkernel_driver_IOKitTest::free (void)
{
	IOLog("IOKitTest::free\n");
	super::free();
}
//检查硬件设备
IOService* com_osxkernel_driver_IOKitTest::probe (IOService* provider, SInt32* score)
{
	IOService *res = super::probe(provider, score);
	IOLog("IOKitTest::probe\n");
	return res;
}

bool com_osxkernel_driver_IOKitTest::start (IOService *provider)
{
	bool res = super::start(provider);
	IOLog("IOKitTest::start\n");
	return res;
}

void com_osxkernel_driver_IOKitTest::stop (IOService *provider)
{
	IOLog("IOKitTest::stop\n");
	super::stop(provider);
}

注意点: 内核版本 编译器版本 安装:

liuhailong:~ liuhailong$ sudo chown -R root:wheel /Users/liuhailong/Library/Developer/Xcode/DerivedData/IOKitTest-grjzacxpxpyxiidhzzobgihdxfqm/Build/Products/Debug/IOKitTest.kext 
liuhailong:~ liuhailong$ sudo kextload /Users/liuhailong/Library/Developer/Xcode/DerivedData/IOKitTest-grjzacxpxpyxiidhzzobgihdxfqm/Build/Products/Debug/IOKitTest.kext 
liuhailong:~ liuhailong$ sudo kextunload /Users/liuhailong/Library/Developer/Xcode/DerivedData/IOKitTest-grjzacxpxpyxiidhzzobgihdxfqm/Build/Products/Debug/IOKitTest.kext 
liuhailong:~ liuhailong$ 

可以安装个IORegistryExplorer 工具查看: