手机软件开发中的P/Invoke

手机软件开发中的P/Invoke

 

手机软件开发中的P/Invoke

01/10/2006 01:03:00 PM - View : 107
.NET CF 中关闭手持设备(支持PPC和SMARTPHONE)
       
         
[DllImport( " Coredll.dll " )] extern static void PowerOffSystem(); private void mIShutDown_Click( object sender, System.EventArgs e) { PowerOffSystem(); }
.NET CF 中重启函数
       
         
using System.Runtime.InteropServices;

public const uint FILE_DEVICE_HAL = 0x00000101;
public const uint METHOD_BUFFERED = 0;
public const uint FILE_ANY_ACCESS = 0;

public uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
{
????return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}

[ DllImport("Coredll .dll")]
public extern static uint KernelIoControl
(
????uint dwIoControlCode,
????IntPtr lpInBuf,
????uint nInBufSize,
????IntPtr lpOutBuf,
????uint nOutBufSize,
????ref uint lpBytesReturned
);

uint ResetPocketPC()
{
????uint bytesReturned = 0;
????uint IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15,
??????METHOD_BUFFERED, FILE_ANY_ACCESS);
????return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0,
??????IntPtr.Zero, 0, ref bytesReturned);
}

private void Form1_Load(object sender, System.EventArgs e)
{
????DialogResult r = MessageBox.Show
????(
????????"Are you sure you want to reset?",
????????"Test",
????????MessageBoxButtons.YesNo,
????????MessageBoxIcon.Question,
????????MessageBoxDefaultButton.Button2
????);

????if (r == DialogResult.Yes)
????{
????????ResetPocketPC();
????}
}

.NET CF中的硬启函数

       
         
[DllImport( " Coredll.dll " )] extern static int KernelIoControl2( int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, ref int lpBytesReturned); [DllImport( " Coredll.dll " )] extern static void SetCleanRebootFlag(); public void HardReset() { int IOCTL_HAL_REBOOT = 0x101003C ; int bytesReturned = 0 ; SetCleanRebootFlag(); KernelIoControl2(IOCTL_HAL_REBOOT, IntPtr.Zero, 0 , IntPtr.Zero, 0 , ref bytesReturned); }

.NET CF 中切换输入法

       
         
private const uint EM_SETINPUTMODE = 0xDE ; private const uint EIM_SPELL = 0 ; private const uint EIM_AMBIG = 1 ; private const uint EIM_NUMBERS = 2 ; [DllImport( " coredll.dll " )] private static extern IntPtr GetFocus(); [DllImport( " coredll.dll " )] private static extern int SendMessage(IntPtr hWnd, uint Message, uint wParam, uint lParam); public static void txt_GotFocus( object sender, System.EventArgs e) { IntPtr hWnd; hWnd = GetFocus(); SendMessage(hWnd, EM_SETINPUTMODE, 0 , EIM_NUMBERS); (sender as System.Windows.Forms.TextBox).SelectAll(); }

鼠标状态变换

       
         
[DllImport( " coredll " , SetLastError = true )] internal static extern uint LoadCursor( uint zeroValue , uint cursorID ); [DllImport( " coredll " , SetLastError = true )] internal static extern uint SetCursor( uint cursorHandle ); public Cursor(){} public static void ShowWaitCursor( bool bShowCursor) { uint cursorHandle = 0 ; if (bShowCursor) { const uint hourGlassCursorID = 32514 ; cursorHandle = LoadCursor( 0 , hourGlassCursorID); } SetCursor(cursorHandle); }

获得设备ID号

       
         
#region 获得设备ID时所需要的变量及函数声明 internal static Int32 METHOD_BUFFERED = 0 ; internal static Int32 FILE_ANY_ACCESS = 0 ; internal static Int32 FILE_DEVICE_HAL = 0x00000101 ; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox tb_machineName; private System.Windows.Forms.Label label5; private System.Windows.Forms.PictureBox pictureBox1; internal static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16 ) | ((FILE_ANY_ACCESS) << 14 ) | (( 21 ) << 2 ) | (METHOD_BUFFERED); [DllImport( " coredll.dll " ,SetLastError = true )] private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte [] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned); #endregion #region 获得设备的ID号 private static string GetDeviceID() { int len = 256 ; int cb = 0 ; byte [] buffer = new byte [ 256 ]; // uint ret; StringBuilder sb = new StringBuilder(); buffer[ 0 ] = 0 ; buffer[ 1 ] = 1 ; KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0 , buffer, len, ref cb); Int32 dwPresetIDOffsset = BitConverter.ToInt32(buffer, 4 ); Int32 dwPlatformIDOffset = BitConverter.ToInt32(buffer, 0xc ); Int32 dwPlatformIDSize = BitConverter.ToInt32(buffer, 0x10 ); sb.Append(String.Format( " {0:X8}-{1:X4}-{2:X4}-{3:X4}- " , BitConverter.ToInt32(buffer, dwPresetIDOffsset), BitConverter.ToInt16(buffer, dwPresetIDOffsset + 4 ), BitConverter.ToInt16(buffer, dwPresetIDOffsset + 6 ), BitConverter.ToInt16(buffer, dwPresetIDOffsset + 8 ))); for ( int i = dwPlatformIDOffset ; i < dwPlatformIDOffset + dwPlatformIDSize ; i ++ ) { sb.Append( String.Format( " {0:X2} " , buffer[i] )); } return sb.ToString(); } #endregion

你可能感兴趣的:(软件开发)