[Windows Mobile Software Development] harddisk_wp:
Finally finished

FINALLY!

I am not sure about the meaning of the data returned however.

Imports System.Runtime.InteropServices

Public Class clsCompass
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Shared Function CreateFile _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function RegisterService _
(ByVal lpszType As String, _
ByVal dwIndex As Integer, _
ByVal lpszLib As String, _
ByVal dwInfo As Integer) As Integer
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function LocalAlloc _
(ByVal uFlags As Integer, _
ByVal uBytes As Integer) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Shared Function LocalFree _
(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
Public Shared Function DeviceIoControl _
(<[In]()> ByVal hDevice As IntPtr, _
<[In]()> ByVal dwIoControlCode As Integer, _
<[In]()> ByVal lpInputBuffer As IntPtr, _
<[In]()> ByVal nInBufferSize As Integer, _
<Out()> ByVal lpOutBuffer As IntPtr, _
<[In]()> ByVal nOutBufferSize As Integer, _
<Out()> ByRef lpBytesReturned As Integer, _
<[In]()> ByVal lpOverlapped As IntPtr) As Integer
End Function

Const DIO_INIT As Int32 = &H3000001
Const DIO_READ As Int32 = &H3000003
Const DIO_QUIT As Int32 = &H3000002
Const INVALID_HANDLE_VALUE As Int64 = -1

Const LMEM_FIXED = &H0
Const LMEM_ZEROINIT = &H40
Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)

Structure compass_data
Public x As Short
Public y As Short
Public z As Short
Public angle As Short
Public d As Short
Public dummy As String
End Structure
Public rdBuf(8) As Byte
Overrides Function toString() As String
Return _
"X: " + rdBuf(0).ToString + vbNewLine _
+ "Y: " + rdBuf(1).ToString + vbNewLine _
+ "Z: " + rdBuf(2).ToString + vbNewLine _
+ "a: " + rdBuf(3).ToString + vbNewLine _
+ "d: " + rdBuf(4).ToString + vbNewLine
End Function

Private devHandle As IntPtr
Private srvHandle As Integer
Public compassData As New compass_data

Public Function initCompass()
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle.ToInt32 = INVALID_HANDLE_VALUE Or devHandle.ToInt32 = 0 Then
devHandle = IntPtr.Zero
srvHandle = RegisterService("SEN", 0, "HTCSensorService.dll", 1)
If srvHandle = 0 Then Return False
devHandle = CreateFile("SEN0:", 0, 0, 0, 3, &H80, 0)
If devHandle.ToInt32 = INVALID_HANDLE_VALUE Or devHandle.ToInt32 = 0 Then Return False
End If
If DeviceIoControl(devHandle, DIO_INIT, 0, 0, 0, 0, 0, 0) = 0 Then Return False
Return True
End Function
Public Function updateData()
' Dim readBytes As Integer
Dim buf(8) As Byte
Dim result As Integer

Dim getMemory As New IntPtr()
If devHandle = 0 Or devHandle = IntPtr.Zero Then Return False

getMemory = LocalAlloc(LPTR, buf.Length)
Marshal.Copy(buf, 0, getMemory, buf.Length)
result = DeviceIoControl(devHandle, DIO_READ, IntPtr.Zero, 0, getMemory, buf.Length, 0, IntPtr.Zero)
Marshal.Copy(getMemory, buf, 0, buf.Length)
If getMemory <> IntPtr.Zero Then LocalFree(getMemory)
If result = 0 Then
Return False
Else
rdBuf = buf
Return True
End If
End Function
Public Function shutdownCompass()
If devHandle <> 0 Then
If DeviceIoControl(devHandle, DIO_QUIT, 0, 0, 0, 0, 0, 0) = 0 Then
Return False
Else
Return True
End If
End If
Return True
End Function
End Class