VB and C# are not very different in general but when it comes to DLlImport they are. Anyway, WriteFile/ReadFile works so the registry settings are correct. Here is how I set them
reg open \drivers\builtin\digitalio
reg set val UseAsIO hex 00,00,00,00,00,C0,01
reg set val DataDir hex 00,00,00,00,00,C0,01
reg set val DataInit hex 00,00,00,00,00,00,01
reg save
I'm testing on pins 1, 2 and 13 on J10, all as output.
The essential bits of the code are (please note that port has already been set by call to SetFilePointer):
<DllImport("coredll.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal dwIoControlCode As UInteger, ByRef InBuffer As UInteger, _
ByVal nInBufferSize As Integer, ByRef OutBuffer As UInteger, ByVal nOutBufferSize As Integer, ByRef pBytesReturned As UInteger, _
ByVal lpOverlapped As IntPtr) As Boolean
End Function
Sub SetPin(ByVal APin As Integer)
Dim cnt As UInteger
Dim pin As UInteger
pin = APin
DeviceIoControl(hPort, IOCTL_DIO_SET_PIN, pin, 4, Nothing, 0, cnt, Nothing)
End Sub
Sub ClearPin(ByVal APin As Integer)
Dim cnt As UInteger
Dim pin As UInteger
pin = APin
DeviceIoControl(hPort, IOCTL_DIO_CLR_PIN, pin, 4, Nothing, 0, cnt, Nothing)
End Sub
where
IOCTL_DIO_SET_PIN = CTL_CODE(FILE_DEVICE_DIO, IOCTL_SET_PIN, METHOD_BUFFERED, FILE_ANY_ACCESS)
IOCTL_DIO_CLR_PIN = CTL_CODE(FILE_DEVICE_DIO, IOCTL_CLR_PIN, METHOD_BUFFERED, FILE_ANY_ACCESS)
and
Const IOCTL_SET_PIN As UInteger = 60
Const IOCTL_CLR_PIN As UInteger = 62