Fixed Issue 387. The new implementation does not try to start a ring 0 driver that already exists, but could not be opened. It tries to delete the driver and install it new. The driver is now stored temporarily in the application folder. The driver is not correctly removed on system shutdown.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Runtime.InteropServices;
14 namespace OpenHardwareMonitor.Hardware.TBalancer {
16 internal enum FT_DEVICE : uint {
27 internal enum FT_STATUS {
33 FT_INSUFFICIENT_RESOURCES,
36 FT_DEVICE_NOT_OPENED_FOR_ERASE,
37 FT_DEVICE_NOT_OPENED_FOR_WRITE,
38 FT_FAILED_TO_WRITE_DEVICE,
39 FT_EEPROM_READ_FAILED,
40 FT_EEPROM_WRITE_FAILED,
41 FT_EEPROM_ERASE_FAILED,
42 FT_EEPROM_NOT_PRESENT,
43 FT_EEPROM_NOT_PROGRAMMED,
48 internal enum FT_FLOW_CONTROL : ushort {
49 FT_FLOW_DTR_DSR = 512,
51 FT_FLOW_RTS_CTS = 256,
52 FT_FLOW_XON_XOFF = 1024,
55 internal enum FT_PURGE : uint {
61 [StructLayout(LayoutKind.Sequential)]
62 internal struct FT_HANDLE {
63 private readonly uint handle;
66 [StructLayout(LayoutKind.Sequential)]
67 internal struct FT_DEVICE_INFO_NODE {
69 public FT_DEVICE Type;
72 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
73 public string SerialNumber;
74 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
75 public string Description;
76 public FT_HANDLE Handle;
79 internal class FTD2XX {
81 public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
83 public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
84 [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
85 public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
86 public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
87 public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
89 public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
90 FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
91 public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
92 FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
93 public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
94 uint readTimeout, uint writeTimeout);
95 public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
96 uint bytesToWrite, out uint bytesWritten);
97 public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
98 public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
99 out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
100 public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle,
101 [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
103 public static readonly FT_CreateDeviceInfoListDelegate
104 FT_CreateDeviceInfoList = CreateDelegate<
105 FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList");
106 public static readonly FT_GetDeviceInfoListDelegate
107 FT_GetDeviceInfoList = CreateDelegate<
108 FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList");
109 public static readonly FT_OpenDelegate
110 FT_Open = CreateDelegate<
111 FT_OpenDelegate>("FT_Open");
112 public static readonly FT_CloseDelegate
113 FT_Close = CreateDelegate<
114 FT_CloseDelegate>("FT_Close");
115 public static readonly FT_SetBaudRateDelegate
116 FT_SetBaudRate = CreateDelegate<
117 FT_SetBaudRateDelegate>("FT_SetBaudRate");
118 public static readonly FT_SetDataCharacteristicsDelegate
119 FT_SetDataCharacteristics = CreateDelegate<
120 FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics");
121 public static readonly FT_SetFlowControlDelegate
122 FT_SetFlowControl = CreateDelegate<
123 FT_SetFlowControlDelegate>("FT_SetFlowControl");
124 public static readonly FT_SetTimeoutsDelegate
125 FT_SetTimeouts = CreateDelegate<
126 FT_SetTimeoutsDelegate>("FT_SetTimeouts");
127 public static readonly FT_WriteDelegate
128 FT_Write = CreateDelegate<
129 FT_WriteDelegate>("FT_Write");
130 public static readonly FT_PurgeDelegate
131 FT_Purge = CreateDelegate<
132 FT_PurgeDelegate>("FT_Purge");
133 public static readonly FT_GetStatusDelegate
134 FT_GetStatus = CreateDelegate<
135 FT_GetStatusDelegate>("FT_GetStatus");
136 public static readonly FT_ReadDelegate
137 FT_Read = CreateDelegate<
138 FT_ReadDelegate>("FT_Read");
142 public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
144 FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length,
146 if (bytesWritten != buffer.Length)
147 return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
152 public static int BytesToRead(FT_HANDLE handle) {
153 uint amountInRxQueue;
154 uint amountInTxQueue;
156 if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
157 out eventStatus) == FT_STATUS.FT_OK) {
158 return (int)amountInRxQueue;
164 public static byte ReadByte(FT_HANDLE handle) {
165 byte[] buffer = new byte[1];
167 FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
168 if (status != FT_STATUS.FT_OK || bytesReturned != 1)
169 throw new InvalidOperationException();
173 private static string GetDllName() {
174 int p = (int)Environment.OSVersion.Platform;
175 if ((p == 4) || (p == 128))
176 return "libftd2xx.so";
181 private static T CreateDelegate<T>(string entryPoint)
183 DllImportAttribute attribute = new DllImportAttribute(GetDllName());
184 attribute.CallingConvention = CallingConvention.StdCall;
185 attribute.PreserveSig = true;
186 attribute.EntryPoint = entryPoint;
188 PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);