WIN CE 5.0 ActiveSync连接/断开连接?

时间:2011-06-03 13:38:45

标签: c# pinvoke windows-ce activesync

我不得不将某些软件从Windows Mobile 6.5移植到Windows CE 5.0,该软件当前检测到该设备何时位于基本单元中(ActiveSync正在运行)。

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

我发现了一篇关于使用PINROKE方法的文章,例如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;
                }
            }
        }

2 个答案:

答案 0 :(得分:4)

您在此处的代码正在等待系统通知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);
}

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

FWIW,SDF有这个already done,所以在你的代码中它会是这样的:

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

答案 1 :(得分:1)

Here's MSDN上的ActiveSync文档。有点旧但应该仍然相关。另请查看this

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

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

您可以找到PInvode资源hereMSDN