Created
December 7, 2011 08:59
-
-
Save Buthrakaur/1442069 to your computer and use it in GitHub Desktop.
.NNET CF OBEX Service control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class NativeMethods | |
{ | |
[DllImport("coredll")] | |
public extern static bool CloseHandle(IntPtr handle); | |
[DllImport("coredll", CharSet = CharSet.Auto, SetLastError = true)] | |
public static extern bool DeviceIoControl( | |
IntPtr hDevice, | |
int dwIoControlCode, | |
[In] int[] inBuffer, | |
int nInBufferSize, | |
[Out] int[] outBuffer, | |
int nOutBufferSize, | |
ref int pBytesReturned, | |
IntPtr lpOverlapped); | |
[DllImport("coredll")] | |
public extern static IntPtr CreateFile(string filename, int desiredAccess, int shareMode, IntPtr securityAttributes, ECreationDisposition creationDisposition, int flags, IntPtr template); | |
} | |
public class BtObexControl | |
{ | |
private enum ObexServiceState: uint | |
{ | |
SERVICE_STATE_OFF = 0, | |
SERVICE_STATE_ON = 1, | |
SERVICE_STATE_STARTING_UP = 2, | |
SERVICE_STATE_SHUTTING_DOWN = 3, | |
SERVICE_STATE_UNLOADING = 4, | |
SERVICE_STATE_UNINITIALIZED = 5, | |
SERVICE_STATE_UNKNOWN = 0xffffffff | |
} | |
private const int IOCTL_SERVICE_START = 0x41000004; | |
private const int IOCTL_SERVICE_STOP = 0x41000008; | |
private const int IOCTL_SERVICE_REFRESH = 0x4100000C; | |
private const int IOCTL_SERVICE_STATUS = 0x41000020; | |
private ObexServiceState QueryObexStatus() | |
{ | |
var file = NativeMethods.CreateFile("OBX0:", | |
0, | |
0, | |
IntPtr.Zero, | |
ECreationDisposition.OpenExisting, | |
0, | |
IntPtr.Zero); | |
if (file == (IntPtr)(-1)) | |
//throw new InvalidOperationException("Unable to Create File"); | |
return ObexServiceState.SERVICE_STATE_UNKNOWN; | |
try | |
{ | |
var bytesReturned = 0; | |
var outBuf = new int[1]; | |
var res = NativeMethods.DeviceIoControl(file, | |
IOCTL_SERVICE_STATUS, | |
null, | |
0, | |
outBuf, | |
sizeof(int) * outBuf.Length, | |
ref bytesReturned, | |
IntPtr.Zero); | |
if (!res) | |
{ | |
return ObexServiceState.SERVICE_STATE_UNKNOWN; | |
} | |
return (ObexServiceState) outBuf[0]; | |
} | |
finally | |
{ | |
NativeMethods.CloseHandle(file); | |
} | |
} | |
private bool StartObexService() | |
{ | |
return ObexIoCtl(IOCTL_SERVICE_START); | |
} | |
private bool StopObexService() | |
{ | |
return ObexIoCtl(IOCTL_SERVICE_STOP); | |
} | |
private bool ObexIoCtl(int ioCtl) | |
{ | |
var file = NativeMethods.CreateFile("OBX0:", 0, 0, IntPtr.Zero, ECreationDisposition.OpenExisting, 0, IntPtr.Zero); | |
if (file == (IntPtr)(-1)) return false; | |
try | |
{ | |
var bytesReturned = 0; | |
var res = NativeMethods.DeviceIoControl(file, | |
ioCtl, | |
null, | |
0, | |
null, | |
0, | |
ref bytesReturned, | |
IntPtr.Zero); | |
return res; | |
} | |
finally | |
{ | |
NativeMethods.CloseHandle(file); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment