您的位置:首页 > 其它

win mobile 启蓝牙 以及 重启系统 的API函数是什么?

2011-04-19 22:31 134 查看
希望实现两个功能:
1、 开启蓝牙,类似于进入bluetooth管理器去打开蓝牙。而我希望在程序中实现。
2、 系统重新启动功能,即重启win mobile。

C# code
public struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll",ExactSpelling=true)]
public static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll",ExactSpelling=true,SetLastError=true)]

public static extern bool OpenProcessToken(IntPtr h,int acc,ref IntPtr phtok );
[DllImport("advapi32.dll",SetLastError=true)]

public static extern bool LookupPrivilegeValue(string host,string name,ref long pluid );
[DllImport("advapi32.dll",ExactSpelling=true, SetLastError=true) ]

public static extern bool AdjustTokenPrivileges(IntPtr htok,bool disall,
ref TokPriv1Luid newst,int len,IntPtr prev, IntPtr relen);

[DllImport("user32.dll",ExactSpelling=true,SetLastError=true) ]

public static extern bool ExitWindowsEx(int flg,int rea);
public const int SE_PRIVILEGE_ENABLED = 0x00000002; //
public const int TOKEN_QUERY = 0x00000008; //
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; //
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";//
public const int EWX_LOGOFF = 0x00000000; // 注消
public const int EWX_SHUTDOWN = 0x00000001; // 先保存再关机
public const int EWX_REBOOT = 0x00000002; // 重启
public const int EWX_FORCE = 0x00000004;//终止没响应程序
public const int EWX_POWEROFF = 0x00000008;//强制关机
public const int EWX_FORCEIFHUNG = 0x00000010;//不保存就关机

/// <summary>
/// 关机,重启。。。。。
/// </summary>
/// <param name="flg"></param>
private void DoExitWin( int flg)
{

bool send;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
send = OpenProcessToken(hproc,TOKEN_ADJUST_PRIVILEGES |TOKEN_QUERY,ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
send = LookupPrivilegeValue(null,SE_SHUTDOWN_NAME,ref tp.Luid);
send= AdjustTokenPrivileges(htok,false,ref tp,0,IntPtr.Zero,IntPtr.Zero);
send = ExitWindowsEx(flg,0);
}
/// <summary>
/// 不保存关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem9_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_FORCEIFHUNG);
}
/// <summary>
/// 强制关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void menuItem8_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_POWEROFF);
}


[System.Runtime.InteropServices.DllImport("coredll")]
public extern static int KernelIoControl(int dwIoControlCode, IntPtr lpInBuf, int nInBufSize, IntPtr lpOutBuf, int nOutBufSize, ref int lpBytesReturned);[System.Runtime.InteropServices.DllImport("coredll")]
public extern static void keybd_event(byte bVK, byte bScan, byte dwFlags, byte dwExtraInfo);private void Shutdownbtn_Click()
{
int IOCTL_HAL_SHUTDOWN = 0x1012000;
int bytesReturned = 0;    byte VK_OFF = 0xdf;
byte KEYEVENTF_KEYUP = 2;    KernelIoControl(IOCTL_HAL_SHUTDOWN, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned);    keybd_event(VK_OFF, 0, 0, 0);
keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0);}

//软重启:

public const uint FILE_DEVICE_HAL = 0x00000101;
public const uint METHOD_BUFFERED = 0;
public const uint FILE_ANY_ACCESS = 0;
public static uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}

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

public static 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);
}
可以动态调用bthutil.Dll中的BthSetMode函数来找开启蓝牙

C/C++ code [code]enum BTH_RADIO_MODE
{
BTH_POWER_OFF,
BTH_CONNECTABLE,
BTH_DISCOVERABLE
};

typedef int(*pBthSetMode)(DWORD);
typedef int(*pBthGetMode)(DWORD*);

HINSTANCE  hModule = LoadLibrary (L"bthutil.Dll");
if(hModule != NULL)
{
pBthSetMode BthSetMode = (pBthSetMode) GetProcAddress(hModule, _T("BthSetMode"));
pBthGetMode BthGetMode = (pBthGetMode) GetProcAddress(hModule, _T("BthGetMode"));
if(BthSetMode != NULL && BthGetMode != NULL)
{
DWORD dwMode;
int nRet = BthGetMode(&dwMode);
if (dwMode == BTH_POWER_OFF || dwMode == BTH_CONNECTABLE)
{
BthSetMode(BTH_DISCOVERABLE);
FreeLibrary(hModule);
return TRUE;
}
}
FreeLibrary(hModule);


蓝牙有三种状态:
1.关闭,BTH_POWER_OFF
2.开启,不可见 BTH_CONNECTABLE
3.开启,可见 BTH_DISCOVERABLE

获取蓝牙当前模式调用函数:
int BthGetMode(DWORD *dwMode);
其中dwMode保存了蓝牙状态参数BTH_POWER_OFF,BTH_CONNECTABLE,BTH_DISCOVERABLE中的一个

设置蓝牙状态调用函数:int BthSetMode(DWORD dwMode);
比如开启蓝牙 BthSetMode(BTHCONNECTABLE);
}
return FALSE;


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐