IOCreatePlugInInterfaceForService 返回神秘错误

use*_*571 6 macos usb xcode iokit swift

我正在尝试在新的 Swift 4.0 Mac 应用程序(不是 iOS)中使用一些旧的 IOKit 功能。我创建了一个桥接头来使用现有的 Objective C 第三方框架 DDHidLib,我目前在 Xcode 9 中工作。

尝试为 USB 游戏手柄创建插件接口的代码在 IOCreatePlugInInterfaceForService 上失败,返回非零错误。

真正奇怪的是我在以前版本的 Xcode 中创建了一个旧的应用程序,它使用相同的框架,并且在新的 Xcode 9 中打开后可以正常工作。 这个以前的项目仍然是 Swift,使用相同 Obj-C 框架的桥接头. 我检查了构建设置并尝试使所有内容匹配,但得到相同的结果;旧应用程序有效,但任何新应用程序都无效。

有没有办法:找出构建设置/编译器中的确切差异以查看难以捉摸的差异,或者进入 IOCreatePlugInInterfaceForService IOKit 方法以查看可能导致错误在一个项目中返回但不是其他?

编辑:这是失败的方法:

- (BOOL) createDeviceInterfaceWithError: (NSError **) error_; {
io_name_t className;
IOCFPlugInInterface ** plugInInterface = NULL;
SInt32 score = 0;
NSError * error = nil;
BOOL result = NO;

mDeviceInterface = NULL;

NSXReturnError(IOObjectGetClass(mHidDevice, className));
if (error)
    goto done;

NSXReturnError(IOCreatePlugInInterfaceForService(mHidDevice, kIOHIDDeviceUserClientTypeID,kIOCFPlugInInterfaceID,&plugInInterface,&score));
if (error)
    goto done;

//Call a method of the intermediate plug-in to create the device interface
NSXReturnError((*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &mDeviceInterface));
if (error)
    goto done;

result = YES;

done:
    if (plugInInterface != NULL)
    {
        (*plugInInterface)->Release(plugInInterface);
    }
    if (error_)
        *error_ = error;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

在旧版本中,IOCreatePlugInInterfaceForService 总是返回值 0。在所有不起作用的版本中,返回值似乎总是 -536870210。该函数中的 mHidDevice 是设备的 io_object_t 句柄。

EDIT2:这是设备的 IORegistryExplorer 页面

这是设备的 IORegistryExplorer 页面

use*_*571 8

经过数周的挠头,终于设法解决了这个问题。新的 Xcode 9 使用应用沙盒,基本上可以防止在新应用中默认访问 USB、蓝牙、摄像头和麦克风等。一旦我关闭它,它就会恢复到预期的行为。

很高兴最终得到了如此简单的答案,但令人失望的是 Xcode 没有提供更多描述性的错误消息或响应,让用户知道他们实际上是在阻止自己访问他们需要的系统部分。