【问题标题】:WIN CE 5.0 ActiveSync Connect/Disconnect?WIN CE 5.0 ActiveSync 连接/断开连接?
【发布时间】:2011-09-07 20:10:13
【问题描述】:

我不得不将某些软件从 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;
                }
            }
        }

【问题讨论】:

  • 那么这里的问题是什么?您发布的代码会在设备处于支架状态时设置一个命名事件,因此您可能可以在应用程序的其他地方等待它。
  • 嗨,这就是问题所在,我不明白我找到的代码,我需要一个触发器,当设备放入底座和取出时触发,很像 WM6 中的 SystemState

标签: c# pinvoke windows-ce activesync


【解决方案1】:

Here's MSDN 上的 ActiveSync 文档。有点旧,但应该仍然相关。也可以看看this

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

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

您可以在hereMSDN 上找到 PInvode 资源

【讨论】:

  • 我已经走这条路了,我有代码来捕获断开连接,但问题是我不理解代码,我将用我发现的内容扩展上面的问题。跨度>
【解决方案2】:

您这里的代码正在等待系统通知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 有这个already done,所以在你的代码中应该是这样的:

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

【讨论】:

  • 很好的解释,我已经成功触发了连接事件。
猜你喜欢
  • 2011-11-30
  • 2016-12-25
  • 2012-09-20
  • 2014-01-31
  • 2011-02-27
  • 2012-11-08
  • 2016-11-13
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多