iohidmanageropen返回错误代码-536870201

发布于 2025-02-11 09:39:46 字数 1245 浏览 2 评论 0 原文

我在Mac上的应用程序中使用USB的新手。当我记录iohidmanageropen(x,y)函数的结果时,请致电我的应用程序,我将获得错误代码-536870201。我在以下帖子中发现()-536870207被转化为kioreturnnotprivileged。如何翻译错误代码并了解错误的原因?

我正在运行的代码:

  manager = std::make_unique<CoreFoundationReference<IOHIDManagerRef>>(
      IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone));

  IOHIDManagerSetDeviceMatching(manager->Get(), nullptr);
  IOHIDManagerRegisterDeviceMatchingCallback(
      manager->Get(), &MacHidDeviceManager::OnDeviceMatched, this);
  IOHIDManagerRegisterDeviceRemovalCallback(
      manager->Get(), &MacHidDeviceManager::OnDeviceRemoved, this);
  IOHIDManagerScheduleWithRunLoop(
      manager->Get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

  auto ioResult = IOHIDManagerOpen(manager->Get(), kIOHIDOptionsTypeNone);
  if (ioResult != kIOReturnSuccess) {
    throw UsbInterfaceException("DeviceManager::DeviceManager", "Cannot open IO device manager, error: {}", ioResult);
  }

I am new to USB usage in my application on Mac. When I log the result of IOHIDManagerOpen(x,y) function call on my application I get error code -536870201. I have found in the following post (Keystrokes are not blocked when using kIOHIDOptionsTypeSeizeDevice and are still passed to the OS) that -536870207 is translated to kIOReturnNotPrivileged. How can I translate my error code and understand the cause of an error?

Code that I am running:

  manager = std::make_unique<CoreFoundationReference<IOHIDManagerRef>>(
      IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone));

  IOHIDManagerSetDeviceMatching(manager->Get(), nullptr);
  IOHIDManagerRegisterDeviceMatchingCallback(
      manager->Get(), &MacHidDeviceManager::OnDeviceMatched, this);
  IOHIDManagerRegisterDeviceRemovalCallback(
      manager->Get(), &MacHidDeviceManager::OnDeviceRemoved, this);
  IOHIDManagerScheduleWithRunLoop(
      manager->Get(), CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

  auto ioResult = IOHIDManagerOpen(manager->Get(), kIOHIDOptionsTypeNone);
  if (ioResult != kIOReturnSuccess) {
    throw UsbInterfaceException("DeviceManager::DeviceManager", "Cannot open IO device manager, error: {}", ioResult);
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-02-18 09:39:46

对于iokit返回值( ioreturn ),将它们打印成十六进制,(“ 0x%x”)通常更有帮助,因为这是在< /a>。

因此,例如,在十六进制中以 0xe00002c7 表示-536870201。 0xe0000000 部分表示它是一个iokit常数, 2C7 将其标识为 kioreturnunsununsununsupported

您尚未发布任何有关该升级或参数的代码或其他详细信息,请访问您的 iohidmanageropen()呼叫,所以我不能真正说出为什么它应该返回 kioreturnunsunsupported < /code>没有更多详细信息。

基于您添加到问题中的代码,我尝试提出最小可重复的示例:

#include <IOKit/hid/IOHIDManager.h>

struct MacHidDeviceManager
{
    static void OnDeviceMatched(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) {}
    static void OnDeviceRemoved(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) {}
    void Start()
    {
        IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
        IOHIDManagerSetDeviceMatching(manager, nullptr);
        IOHIDManagerRegisterDeviceMatchingCallback(
            manager, &MacHidDeviceManager::OnDeviceMatched, this);
        IOHIDManagerRegisterDeviceRemovalCallback(
            manager, &MacHidDeviceManager::OnDeviceRemoved, this);
        IOHIDManagerScheduleWithRunLoop(
            manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

        auto ioResult = IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone);
        if (ioResult != kIOReturnSuccess) {
            fprintf(stderr, "MacHidDeviceManager -> 0x%x\n", ioResult);
        }
    }
};

int main()
{
    MacHidDeviceManager m;
    m.Start();
}

最初失败( 0xe0000002e2 /code>)由于没有安全&amp;隐私,但是一旦我授予系统偏好的许可,就可以成功。我已经在MacOS 11和13上对此进行了测试,结果相同。我怀疑您尚未发布的一些代码( corefoundationReference ?)可能是问题所在。还是一些沙箱问题?

顺便说一句,我维护着一个小型iokit助手实用程序库,其中之一是函数 djt_ioreturn_string ioreturn 值转换为这些常数的字符串表示。 (您只需要在项目中包含ioreturn_strings.cpp和ioreturn_strings.h文件,ioreturn_strings.h可以是 #include / #import #import ed pure c或objective -c,只有.cpp使用C ++。)

For IOKit return values (IOReturn) it's typically much more helpful to print them as hexadecimal, ("0x%x") as that's how the constants are defined in <IOKit/IOReturn.h>.

So for example, -536870201 is represented in hexadecimal as 0xE00002C7. The 0xE0000000 part means it's an IOKit constant, the last few digits of 2C7 identify it as kIOReturnUnsupported.

You haven't posted any code or other details about the run-up or arguments to your IOHIDManagerOpen() call, so I can't really say why it should return kIOReturnUnsupported without more details.

Based on the code you added to your question, I tried to come up with the minimum reproducible sample:

#include <IOKit/hid/IOHIDManager.h>

struct MacHidDeviceManager
{
    static void OnDeviceMatched(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) {}
    static void OnDeviceRemoved(void *context, IOReturn result, void *sender, IOHIDDeviceRef device) {}
    void Start()
    {
        IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
        IOHIDManagerSetDeviceMatching(manager, nullptr);
        IOHIDManagerRegisterDeviceMatchingCallback(
            manager, &MacHidDeviceManager::OnDeviceMatched, this);
        IOHIDManagerRegisterDeviceRemovalCallback(
            manager, &MacHidDeviceManager::OnDeviceRemoved, this);
        IOHIDManagerScheduleWithRunLoop(
            manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

        auto ioResult = IOHIDManagerOpen(manager, kIOHIDOptionsTypeNone);
        if (ioResult != kIOReturnSuccess) {
            fprintf(stderr, "MacHidDeviceManager -> 0x%x\n", ioResult);
        }
    }
};

int main()
{
    MacHidDeviceManager m;
    m.Start();
}

This initially fails (0xe00002e2, kIOReturnNotPermitted) due to missing consent from Security & Privacy, but succeeds once I've granted that permission in System Preferences. I've tested this on macOS 11 and 13 with the same result. I suspect some of the code you haven't posted (CoreFoundationReference?) might be where the problem lies. Or perhaps some sandboxing issue?

Incidentally, I maintain a small library of IOKit helper utilities, and one of those things is a function djt_ioreturn_string which converts IOReturn values into string representations of those constants. (You just need to include the ioreturn_strings.cpp and ioreturn_strings.h files in your project, the ioreturn_strings.h can be #include/#imported from pure C or Objective-C, just the .cpp uses C++.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文