WIN CE 5.0 ActiveSync连接/断开连接

本文关键字:连接 断开 ActiveSync CE WIN | 更新日期: 2023-09-27 17:58:24

我必须将一些软件从Windows Mobile 6.5备份到Windows CE 5.0,该软件当前检测设备何时位于基本设备中(ActiveSync正在运行)。

我需要知道ActiveSync何时在设备上运行,以便为设备发送和接收文件做好准备。

我发现了一篇关于使用PINVOKE方法(如CeRunAppAtEvent)的文章,但我不知道它是如何工作的。

    bool terminateDeviceEventThreads = false;
    IntPtr handleActiveSyncEndEvent;
    while (!terminateDeviceEventThreads)
        {
        handleActiveSyncEndEvent = NativeMethods.CreateEvent (IntPtr.Zero,
                                            true, false, "EventActiveSync");
        if (IntPtr.Zero != handleActiveSyncEndEvent)
            {
            if (NativeMethods.CeRunAppAtEvent ("''''.''Notifications''NamedEvents''EventActiveSync",
                         (int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_RS232_DETECTED))
                {
                NativeMethods.WaitForSingleObject (handleActiveSyncEndEvent, 0);
                //
                NativeMethods.ResetEvent (handleActiveSyncEndEvent);
                if (!NativeMethods.CeRunAppAtEvent ("''''.''Notifications''NamedEvents''EventActiveSync",
                                 (int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_NONE))
                    {
                    break;
                    }
                handleActiveSyncEndEvent = IntPtr.Zero;
                }
            }
        }

WIN CE 5.0 ActiveSync连接/断开连接

这里的代码正在等待系统通知NOTIFICATION_EVENT_RS232_DETECTED。通过使用CeRunAppAtEvent(有点用词不当,因为它不会运行应用程序,而是设置事件),他们注册了一个命名系统事件,名称为"EventActiveSync",将在通知发生时设置。

本质上,当设备对接时,将设置命名的系统事件。

您的代码中有一些等待代码,但不是完全的-它调用了WaitForSingleObject,但从不查看结果,然后取消挂起事件。我想它看起来会更像这个

event EventHandler OnConnect = delegate{};
void ListenerThreadProc()
{
    var eventName = "OnConnect";
    // create an event to wait on
    IntPtr @event = NativeMethods.CreateEvent (IntPtr.Zero, true, false, eventName);
    // register for the notification
    NativeMethods.CeRunAppAtEvent (
           string.Format("''''.''Notifications''NamedEvents''{0}", eventName),
           (int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_RS232_DETECTED);
    while(!m_shutdown)
    {
        // wait for the event to be set
        // use a 1s timeout so we don't prevent thread shutdown
        if(NativeMethods.WaitForSingleObject(@event, 1000) == 0)
        {
            // raise an event
            OnConnect(this, EventArgs.Empty);
        }
    }
    // unregister the notification
    NativeMethods.CeRunAppAtEvent (
           string.Format("''''.''Notifications''NamedEvents''{0}", eventName),
           (int) NOTIFICATION_EVENT.NOTIFICATION_EVENT_NONE);
    // clean up the event handle
    NativeMethods.CloseHandle(@event);
}

您的应用程序将创建一个在启动时使用此进程的线程,并为OnConnect事件连接一个事件处理程序。

FWIW,SDF已经这样做了,所以在你的代码中会是这样的:

DeviceManagement.SerialDeviceDetected += DeviceConnected;
...
void DeviceConnected()
{
    // handle connection
}

这是MSDN上的ActiveSync文档。有点老了,但应该仍然有意义。还可以看看这个

至于CeRunAppAtEvent,您需要创建一个Native方法的包装器,如下所示

[DllImport("coredll.dll", EntryPoint="CeRunAppAtEvent", SetLastError=true)]  
private static extern bool CeRunAppAtEvent(string pwszAppName, int lWhichEvent);

您可以在此处和MSDN 上找到PInvode资源