网络更改事件注册在 MAC OS X 10.7 (Lion) 中失败

Network Change event registration failed in MAC OS X 10.7 (Lion)

本文关键字:Lion 失败 OS 事件 注册 MAC 网络      更新时间:2023-10-16

我试图通过以下代码在MAC OS X 10.7(狮子)中注册network_change事件:

#define kNotifySCNetworkChange "com.apple.system.config.network_change"
void init()
{
   status = notify_register_check(kNotifySCNetworkChange, &token);
   if (status != NOTIFY_STATUS_OK)
   {
       cout << "Event Registration failed" <<endl;
   }
   cout << "Event Registration Success" << endl;
}

.....

如果我在示例程序中使用此代码,它已成功注册。如果我在我的应用程序中使用它,notify_register_check() 函数返回状态为 1000000 (NOTIFY_STATUS_FAILED)。此外,返回的 errno 为 0。

注意:我的应用程序在豹和雪豹中工作,没有任何问题(注册成功)

我在谷歌上搜索了这个状态,但找不到相关信息。谁能告诉我在哪些情况下返回此NOTIFY_STATUS_FAILED?


我最近观察到的另一件事是:我们在我的应用程序中使用了fork()系统调用。当我尝试在父流程中注册时,事件注册成功。但事件注册在子进程中失败。为什么它在子进程 ??? 中失败 有什么想法吗!!

errno为0,因为通知API不是系统调用,而是Mach API调用。 有关更多详细信息,请使用mach_error ()。分叉应该不是问题(在大多数情况下,它还克隆了马赫端口)。事实上,以下代码在 Mountain Lion 上运行良好:

#include <notify.h>
#include <iostream>
#define kNotifySCNetworkChange "com.apple.system.config.network_change"
void init()
{
   int token;
   int status = notify_register_check(kNotifySCNetworkChange, &token);
   if (status != NOTIFY_STATUS_OK)
   {
       std::cout << "Event Registration failed" <<std::endl;
   }
   std::cout << "Event Registration Success" << std::endl;
}
int main()
{
 int rc = fork();
 init();

}