rapistartup.cs

来自「Windows Mobile平台应用与开发 书中c#源码」· CS 代码 · 共 82 行

CS
82
字号
// RapiStartup.cs - Simple thread-free RAPI startup
// with all P/Invoke declarations included.
//
// Code from _Programming the .NET Compact Framework with C#_
// and _Programming the .NET Compact Framework with VB_
// (c) Copyright 2002-2004 Paul Yao and David Durant. 
// All rights reserved.

using System;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RapiStartup
{
   public class RapiStartup
   {
      const string m_strAppName = "RapiStartup";

      public RapiStartup()
      {
      }
      
      // -------------------------------------------------------
      // RAPI.DLL Definitions
      // -------------------------------------------------------
      public struct RAPIINIT
      {
         public int cbSize;
         public IntPtr heRapiInit;
         public int hrRapiInit;
      };

      [DllImport("rapi.DLL", CharSet=CharSet.Unicode)]
      public static extern int CeRapiInitEx (ref RAPIINIT p);
      [DllImport("rapi.DLL", CharSet=CharSet.Unicode)]
      public static extern int CeRapiUninit ();

      public const uint S_OK = 0;

      // -------------------------------------------------------
      // Main -- Program entry point
      // -------------------------------------------------------
      public static void Main()
      {
         // Allocate structure for call to CeRapiInitEx
         RAPIINIT ri = new RAPIINIT();
         ri.cbSize = Marshal.SizeOf(ri);

         // Call init function
         int hr = CeRapiInitEx(ref ri);

         // Wrap event handle in corresponding .NET object
         ManualResetEvent mrev = new ManualResetEvent(false);
         mrev.Handle = ri.heRapiInit;

         // Wait five seconds, then fail.
         if (mrev.WaitOne(5000, false) && ri.hrRapiInit == S_OK)
         {
            // Connection established.
            MessageBox.Show("Connection Established", m_strAppName);
         }
         else
         {
            // On failure, disconnect from RAPI.
            CeRapiUninit();

            MessageBox.Show("Timeout - No Device", m_strAppName);
            return;
         }

         // If we get here, we have established a RAPI connection

         // ToDo: Put your RAPI calls here...

         // Cleanup.
         CeRapiUninit();
      }

   } // class RapiStartup
} // namespace RapiStartup

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?