//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar and Future Technology Devices International Limited. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Code is based on FTDI code, rewritten by Nils Hammar.
*/
namespace FtdiApi.Native
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using global::FtdiApi.Constants;
using global::FtdiApi.Enums;
using global::FtdiApi.Structs;
///
/// FTDI API Native Methods.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1516:ElementsMustBeSeparatedByBlankLine", Justification = "Reviewed.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:ElementsMustAppearInTheCorrectOrder", Justification = "Reviewed.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1121:UseBuiltInTypeAlias", Justification = "Reviewed.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Reviewed.")]
public class NativeMethods : IDisposable
{
#region FIELDS
///
/// To handle a singleton.
///
private static NativeMethods instance;
// Handle to our DLL - used with GetProcAddress to load all of our functions
private IntPtr hFTD2XXDLL = IntPtr.Zero;
// Declare pointers to each of the functions we are going to use in FT2DXX.DLL
// These are assigned in our constructor and freed in our destructor.
private IntPtr pFT_CreateDeviceInfoList = IntPtr.Zero;
private IntPtr pFT_GetDeviceInfoDetail = IntPtr.Zero;
private IntPtr pFT_Rescan = IntPtr.Zero;
private IntPtr pFT_Reload = IntPtr.Zero;
private IntPtr pFT_Open = IntPtr.Zero;
private IntPtr pFT_OpenEx = IntPtr.Zero;
private IntPtr pFT_OpenExLoc = IntPtr.Zero;
private IntPtr pFT_Close = IntPtr.Zero;
private IntPtr pFT_Read = IntPtr.Zero;
private IntPtr pFT_Write = IntPtr.Zero;
private IntPtr pFT_GetQueueStatus = IntPtr.Zero;
private IntPtr pFT_GetModemStatus = IntPtr.Zero;
private IntPtr pFT_GetStatus = IntPtr.Zero;
private IntPtr pFT_SetBaudRate = IntPtr.Zero;
private IntPtr pFT_SetDivisor = IntPtr.Zero;
private IntPtr pFT_SetDataCharacteristics = IntPtr.Zero;
private IntPtr pFT_SetFlowControl = IntPtr.Zero;
private IntPtr pFT_SetDtr = IntPtr.Zero;
private IntPtr pFT_ClrDtr = IntPtr.Zero;
private IntPtr pFT_SetRts = IntPtr.Zero;
private IntPtr pFT_ClrRts = IntPtr.Zero;
private IntPtr pFT_ResetDevice = IntPtr.Zero;
private IntPtr pFT_ResetPort = IntPtr.Zero;
private IntPtr pFT_CyclePort = IntPtr.Zero;
private IntPtr pFT_Purge = IntPtr.Zero;
private IntPtr pFT_SetTimeouts = IntPtr.Zero;
private IntPtr pFT_SetBreakOn = IntPtr.Zero;
private IntPtr pFT_SetBreakOff = IntPtr.Zero;
private IntPtr pFT_GetDeviceInfo = IntPtr.Zero;
private IntPtr pFT_SetResetPipeRetryCount = IntPtr.Zero;
private IntPtr pFT_StopInTask = IntPtr.Zero;
private IntPtr pFT_RestartInTask = IntPtr.Zero;
private IntPtr pFT_GetDriverVersion = IntPtr.Zero;
private IntPtr pFT_GetLibraryVersion = IntPtr.Zero;
private IntPtr pFT_SetDeadmanTimeout = IntPtr.Zero;
private IntPtr pFT_SetChars = IntPtr.Zero;
private IntPtr pFT_SetEventNotification = IntPtr.Zero;
private IntPtr pFT_GetComPortNumber = IntPtr.Zero;
private IntPtr pFT_SetLatencyTimer = IntPtr.Zero;
private IntPtr pFT_GetLatencyTimer = IntPtr.Zero;
private IntPtr pFT_SetBitMode = IntPtr.Zero;
private IntPtr pFT_GetBitMode = IntPtr.Zero;
private IntPtr pFT_SetUSBParameters = IntPtr.Zero;
private IntPtr pFT_ReadEE = IntPtr.Zero;
private IntPtr pFT_WriteEE = IntPtr.Zero;
private IntPtr pFT_EraseEE = IntPtr.Zero;
private IntPtr pFT_EE_UASize = IntPtr.Zero;
private IntPtr pFT_EE_UARead = IntPtr.Zero;
private IntPtr pFT_EE_UAWrite = IntPtr.Zero;
private IntPtr pFT_EE_Read = IntPtr.Zero;
private IntPtr pFT_EE_Program = IntPtr.Zero;
private IntPtr pFT_EEPROM_Read = IntPtr.Zero;
private IntPtr pFT_EEPROM_Program = IntPtr.Zero;
#endregion
#region DELEGATES
// Definitions for FTD2XX functions
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_CreateDeviceInfoList(ref uint numdevs);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetDeviceInfoDetail(uint index, ref uint flags, ref FT_DEVICE chiptype, ref uint id, ref uint locid, byte[] serialnumber, byte[] description, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Open(uint index, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_OpenEx(string devstring, uint dwFlags, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_OpenExLoc(uint devloc, uint dwFlags, ref IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Close(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Read(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToRead, ref uint lpdwBytesReturned);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Write(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToWrite, ref uint lpdwBytesWritten);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetQueueStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetModemStatus(IntPtr ftHandle, ref uint lpdwModemStatus);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue, ref uint lpdwAmountInTxQueue, ref uint lpdwEventStatus);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetBaudRate(IntPtr ftHandle, uint dwBaudRate);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetDivisor(IntPtr ftHandle, ushort usDivisor);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetDataCharacteristics(IntPtr ftHandle, byte uWordLength, byte uStopBits, byte uParity);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetFlowControl(IntPtr ftHandle, ushort usFlowControl, byte uXon, byte uXoff);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetDtr(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_ClrDtr(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetRts(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_ClrRts(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_ResetDevice(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_ResetPort(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_CyclePort(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Rescan();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Reload(ushort wVID, ushort wPID);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_Purge(IntPtr ftHandle, uint dwMask);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetTimeouts(IntPtr ftHandle, uint dwReadTimeout, uint dwWriteTimeout);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetBreakOn(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetBreakOff(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetDeviceInfo(IntPtr ftHandle, ref FT_DEVICE pftType, ref uint lpdwID, byte[] pcSerialNumber, byte[] pcDescription, IntPtr pvDummy);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetResetPipeRetryCount(IntPtr ftHandle, uint dwCount);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_StopInTask(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_RestartInTask(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetDriverVersion(IntPtr ftHandle, ref uint lpdwDriverVersion);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetLibraryVersion(ref uint lpdwLibraryVersion);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetDeadmanTimeout(IntPtr ftHandle, uint dwDeadmanTimeout);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetChars(IntPtr ftHandle, byte uEventCh, byte uEventChEn, byte uErrorCh, byte uErrorChEn);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetEventNotification(IntPtr ftHandle, uint dwEventMask, SafeHandle hEvent);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetComPortNumber(IntPtr ftHandle, ref Int32 dwComPortNumber);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetLatencyTimer(IntPtr ftHandle, byte ucLatency);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetLatencyTimer(IntPtr ftHandle, ref byte ucLatency);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetBitMode(IntPtr ftHandle, byte ucMask, byte ucMode);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_GetBitMode(IntPtr ftHandle, ref byte ucMode);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_SetUSBParameters(IntPtr ftHandle, uint dwInTransferSize, uint dwOutTransferSize);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_ReadEE(IntPtr ftHandle, uint dwWordOffset, ref ushort lpwValue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_WriteEE(IntPtr ftHandle, uint dwWordOffset, ushort wValue);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EraseEE(IntPtr ftHandle);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EE_UASize(IntPtr ftHandle, ref uint dwSize);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EE_UARead(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen, ref uint lpdwDataRead);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EE_UAWrite(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EE_Read(IntPtr ftHandle, FT_PROGRAM_DATA pData);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EE_Program(IntPtr ftHandle, FT_PROGRAM_DATA pData);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EEPROM_Read(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
internal delegate FT_STATUS tFT_EEPROM_Program(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber);
#endregion
#region PROPERTIES
///
/// Gets singleton of NativeMethods.
///
internal static NativeMethods Instance
{
get
{
if (instance == null)
{
instance = new NativeMethods();
}
return instance;
}
}
///
/// Gets a value indicating whether DLL is loaded.
///
internal bool DllLoaded
{
get
{
return this.hFTD2XXDLL != IntPtr.Zero;
}
}
internal NativeMethods.tFT_CreateDeviceInfoList FT_CreateDeviceInfoList { get; private set; }
internal NativeMethods.tFT_GetDeviceInfoDetail FT_GetDeviceInfoDetail { get; private set; }
internal NativeMethods.tFT_Rescan FT_Rescan { get; private set; }
internal NativeMethods.tFT_Reload FT_Reload { get; private set; }
internal NativeMethods.tFT_Open FT_Open { get; private set; }
internal NativeMethods.tFT_OpenEx FT_OpenEx { get; private set; }
internal NativeMethods.tFT_OpenExLoc FT_OpenExLoc { get; private set; }
internal NativeMethods.tFT_Close FT_Close { get; private set; }
internal NativeMethods.tFT_Read FT_Read { get; private set; }
internal NativeMethods.tFT_Write FT_Write { get; private set; }
internal NativeMethods.tFT_GetQueueStatus FT_GetQueueStatus { get; private set; }
internal NativeMethods.tFT_GetModemStatus FT_GetModemStatus { get; private set; }
internal NativeMethods.tFT_GetStatus FT_GetStatus { get; private set; }
internal NativeMethods.tFT_SetBaudRate FT_SetBaudRate { get; private set; }
internal NativeMethods.tFT_SetDivisor FT_SetDivisor { get; private set; }
internal NativeMethods.tFT_SetDataCharacteristics FT_SetDataCharacteristics { get; private set; }
internal NativeMethods.tFT_SetFlowControl FT_SetFlowControl { get; private set; }
internal NativeMethods.tFT_SetDtr FT_SetDtr { get; private set; }
internal NativeMethods.tFT_ClrDtr FT_ClrDtr { get; private set; }
internal NativeMethods.tFT_SetRts FT_SetRts { get; private set; }
internal NativeMethods.tFT_ClrRts FT_ClrRts { get; private set; }
internal NativeMethods.tFT_ResetDevice FT_ResetDevice { get; private set; }
internal NativeMethods.tFT_ResetPort FT_ResetPort { get; private set; }
internal NativeMethods.tFT_CyclePort FT_CyclePort { get; private set; }
internal NativeMethods.tFT_Purge FT_Purge { get; private set; }
internal NativeMethods.tFT_SetTimeouts FT_SetTimeouts { get; private set; }
internal NativeMethods.tFT_SetBreakOn FT_SetBreakOn { get; private set; }
internal NativeMethods.tFT_SetBreakOff FT_SetBreakOff { get; private set; }
internal NativeMethods.tFT_GetDeviceInfo FT_GetDeviceInfo { get; private set; }
internal NativeMethods.tFT_SetResetPipeRetryCount FT_SetResetPipeRetryCount { get; private set; }
internal NativeMethods.tFT_StopInTask FT_StopInTask { get; private set; }
internal NativeMethods.tFT_RestartInTask FT_RestartInTask { get; private set; }
internal NativeMethods.tFT_GetDriverVersion FT_GetDriverVersion { get; private set; }
internal NativeMethods.tFT_GetLibraryVersion FT_GetLibraryVersion { get; private set; }
internal NativeMethods.tFT_SetDeadmanTimeout FT_SetDeadmanTimeout { get; private set; }
internal NativeMethods.tFT_SetChars FT_SetChars { get; private set; }
internal NativeMethods.tFT_SetEventNotification FT_SetEventNotification { get; private set; }
internal NativeMethods.tFT_GetComPortNumber FT_GetComPortNumber { get; private set; }
internal NativeMethods.tFT_SetLatencyTimer FT_SetLatencyTimer { get; private set; }
internal NativeMethods.tFT_GetLatencyTimer FT_GetLatencyTimer { get; private set; }
internal NativeMethods.tFT_SetBitMode FT_SetBitMode { get; private set; }
internal NativeMethods.tFT_GetBitMode FT_GetBitMode { get; private set; }
internal NativeMethods.tFT_SetUSBParameters FT_SetUSBParameters { get; private set; }
internal NativeMethods.tFT_ReadEE FT_ReadEE { get; private set; }
internal NativeMethods.tFT_WriteEE FT_WriteEE { get; private set; }
internal NativeMethods.tFT_EraseEE FT_EraseEE { get; private set; }
internal NativeMethods.tFT_EE_UASize FT_EE_UASize { get; private set; }
internal NativeMethods.tFT_EE_UARead FT_EE_UARead { get; private set; }
internal NativeMethods.tFT_EE_UAWrite FT_EE_UAWrite { get; private set; }
internal NativeMethods.tFT_EE_Read FT_EE_Read { get; private set; }
internal NativeMethods.tFT_EE_Program FT_EE_Program { get; private set; }
internal NativeMethods.tFT_EEPROM_Read FT_EEPROM_Read { get; private set; }
internal NativeMethods.tFT_EEPROM_Program FT_EEPROM_Program { get; private set; }
#endregion
///
/// Built-in Windows API functions to allow us to dynamically load our own DLL.
/// Will allow us to use old versions of the DLL that do not have all of these functions available.
///
#region LOAD_LIBRARIES
///
/// Load unmanaged DLL library.
///
/// Name and path of DLL to load.
/// Handle to loaded library.
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadLibraryW(string dllToLoad);
///
/// Get address of procedure in library.
///
/// Handle to library.
/// Name of procedure to get address for.
/// Address to procedure.
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
private static extern IntPtr GetProcAddress([In] IntPtr hModule, [In, MarshalAs(UnmanagedType.LPStr)] string procedureName);
///
/// Free a loaded library from memory.
///
/// Handle to library.
/// false = fail, true = success.
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
#endregion
#region CONSTRUCTOR_DESTRUCTOR
private NativeMethods()
{
// If FTD2XX.DLL is NOT loaded already, load it
if (this.hFTD2XXDLL == IntPtr.Zero)
{
// Load our FTD2XX.DLL library
this.hFTD2XXDLL = LoadLibraryW("FTD2XX.DLL");
if (this.hFTD2XXDLL == IntPtr.Zero)
{
// Failed to load our FTD2XX.DLL library from System32 or the application directory
// Try the same directory that this FTD2XX_NET DLL is in
// MessageBox.Show("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location));
this.hFTD2XXDLL = LoadLibraryW(Path.GetDirectoryName(GetType().Assembly.Location) + "\\FTD2XX.DLL");
if (this.hFTD2XXDLL == IntPtr.Zero)
{
FTDI.ErrorHandler("init", FT_STATUS.FT_OTHER_ERROR, FT_ERROR.FT_DLL_NOT_FOUND);
}
}
}
// At this point the library shall be loaded, otherwise an exception shall have
// taken care of the missing library.
// Set up our function pointers for use through our exported methods
this.pFT_CreateDeviceInfoList = GetProcAddress(this.hFTD2XXDLL, "FT_CreateDeviceInfoList");
this.pFT_GetDeviceInfoDetail = GetProcAddress(this.hFTD2XXDLL, "FT_GetDeviceInfoDetail");
this.pFT_Rescan = GetProcAddress(this.hFTD2XXDLL, "FT_Rescan");
this.pFT_Reload = GetProcAddress(this.hFTD2XXDLL, "FT_Reload");
this.pFT_Open = GetProcAddress(this.hFTD2XXDLL, "FT_Open");
this.pFT_OpenEx = GetProcAddress(this.hFTD2XXDLL, "FT_OpenEx");
this.pFT_OpenExLoc = GetProcAddress(this.hFTD2XXDLL, "FT_OpenEx");
this.pFT_Close = GetProcAddress(this.hFTD2XXDLL, "FT_Close");
this.pFT_Read = GetProcAddress(this.hFTD2XXDLL, "FT_Read");
this.pFT_Write = GetProcAddress(this.hFTD2XXDLL, "FT_Write");
this.pFT_GetQueueStatus = GetProcAddress(this.hFTD2XXDLL, "FT_GetQueueStatus");
this.pFT_GetModemStatus = GetProcAddress(this.hFTD2XXDLL, "FT_GetModemStatus");
this.pFT_GetStatus = GetProcAddress(this.hFTD2XXDLL, "FT_GetStatus");
this.pFT_SetBaudRate = GetProcAddress(this.hFTD2XXDLL, "FT_SetBaudRate");
this.pFT_SetDivisor = GetProcAddress(this.hFTD2XXDLL, "FT_SetDivisor");
this.pFT_SetDataCharacteristics = GetProcAddress(this.hFTD2XXDLL, "FT_SetDataCharacteristics");
this.pFT_SetFlowControl = GetProcAddress(this.hFTD2XXDLL, "FT_SetFlowControl");
this.pFT_SetDtr = GetProcAddress(this.hFTD2XXDLL, "FT_SetDtr");
this.pFT_ClrDtr = GetProcAddress(this.hFTD2XXDLL, "FT_ClrDtr");
this.pFT_SetRts = GetProcAddress(this.hFTD2XXDLL, "FT_SetRts");
this.pFT_ClrRts = GetProcAddress(this.hFTD2XXDLL, "FT_ClrRts");
this.pFT_ResetDevice = GetProcAddress(this.hFTD2XXDLL, "FT_ResetDevice");
this.pFT_ResetPort = GetProcAddress(this.hFTD2XXDLL, "FT_ResetPort");
this.pFT_CyclePort = GetProcAddress(this.hFTD2XXDLL, "FT_CyclePort");
this.pFT_Purge = GetProcAddress(this.hFTD2XXDLL, "FT_Purge");
this.pFT_SetTimeouts = GetProcAddress(this.hFTD2XXDLL, "FT_SetTimeouts");
this.pFT_SetBreakOn = GetProcAddress(this.hFTD2XXDLL, "FT_SetBreakOn");
this.pFT_SetBreakOff = GetProcAddress(this.hFTD2XXDLL, "FT_SetBreakOff");
this.pFT_GetDeviceInfo = GetProcAddress(this.hFTD2XXDLL, "FT_GetDeviceInfo");
this.pFT_SetResetPipeRetryCount = GetProcAddress(this.hFTD2XXDLL, "FT_SetResetPipeRetryCount");
this.pFT_StopInTask = GetProcAddress(this.hFTD2XXDLL, "FT_StopInTask");
this.pFT_RestartInTask = GetProcAddress(this.hFTD2XXDLL, "FT_RestartInTask");
this.pFT_GetDriverVersion = GetProcAddress(this.hFTD2XXDLL, "FT_GetDriverVersion");
this.pFT_GetLibraryVersion = GetProcAddress(this.hFTD2XXDLL, "FT_GetLibraryVersion");
this.pFT_SetDeadmanTimeout = GetProcAddress(this.hFTD2XXDLL, "FT_SetDeadmanTimeout");
this.pFT_SetChars = GetProcAddress(this.hFTD2XXDLL, "FT_SetChars");
this.pFT_SetEventNotification = GetProcAddress(this.hFTD2XXDLL, "FT_SetEventNotification");
this.pFT_GetComPortNumber = GetProcAddress(this.hFTD2XXDLL, "FT_GetComPortNumber");
this.pFT_SetLatencyTimer = GetProcAddress(this.hFTD2XXDLL, "FT_SetLatencyTimer");
this.pFT_GetLatencyTimer = GetProcAddress(this.hFTD2XXDLL, "FT_GetLatencyTimer");
this.pFT_SetBitMode = GetProcAddress(this.hFTD2XXDLL, "FT_SetBitMode");
this.pFT_GetBitMode = GetProcAddress(this.hFTD2XXDLL, "FT_GetBitMode");
this.pFT_SetUSBParameters = GetProcAddress(this.hFTD2XXDLL, "FT_SetUSBParameters");
this.pFT_ReadEE = GetProcAddress(this.hFTD2XXDLL, "FT_ReadEE");
this.pFT_WriteEE = GetProcAddress(this.hFTD2XXDLL, "FT_WriteEE");
this.pFT_EraseEE = GetProcAddress(this.hFTD2XXDLL, "FT_EraseEE");
this.pFT_EE_UASize = GetProcAddress(this.hFTD2XXDLL, "FT_EE_UASize");
this.pFT_EE_UARead = GetProcAddress(this.hFTD2XXDLL, "FT_EE_UARead");
this.pFT_EE_UAWrite = GetProcAddress(this.hFTD2XXDLL, "FT_EE_UAWrite");
this.pFT_EE_Read = GetProcAddress(this.hFTD2XXDLL, "FT_EE_Read");
this.pFT_EE_Program = GetProcAddress(this.hFTD2XXDLL, "FT_EE_Program");
this.pFT_EEPROM_Read = GetProcAddress(this.hFTD2XXDLL, "FT_EEPROM_Read");
this.pFT_EEPROM_Program = GetProcAddress(this.hFTD2XXDLL, "FT_EEPROM_Program");
this.FT_CreateDeviceInfoList = safeGetDelegate(this.pFT_CreateDeviceInfoList);
this.FT_GetDeviceInfoDetail = safeGetDelegate(this.pFT_GetDeviceInfoDetail);
this.FT_Rescan = safeGetDelegate(this.pFT_Rescan);
this.FT_Reload = safeGetDelegate(this.pFT_Reload);
this.FT_Open = safeGetDelegate(this.pFT_Open);
this.FT_OpenEx = safeGetDelegate(this.pFT_OpenEx);
this.FT_OpenExLoc = safeGetDelegate(this.pFT_OpenExLoc);
this.FT_Close = safeGetDelegate(this.pFT_Close);
this.FT_Read = safeGetDelegate(this.pFT_Read);
this.FT_Write = safeGetDelegate(this.pFT_Write);
this.FT_GetQueueStatus = safeGetDelegate(this.pFT_GetQueueStatus);
this.FT_GetModemStatus = safeGetDelegate(this.pFT_GetModemStatus);
this.FT_GetStatus = safeGetDelegate(this.pFT_GetStatus);
this.FT_SetBaudRate = safeGetDelegate(this.pFT_SetBaudRate);
this.FT_SetDivisor = safeGetDelegate(this.pFT_SetDivisor);
this.FT_SetDataCharacteristics = safeGetDelegate(this.pFT_SetDataCharacteristics);
this.FT_SetFlowControl = safeGetDelegate(this.pFT_SetFlowControl);
this.FT_SetDtr = safeGetDelegate(this.pFT_SetDtr);
this.FT_ClrDtr = safeGetDelegate(this.pFT_ClrDtr);
this.FT_SetRts = safeGetDelegate(this.pFT_SetRts);
this.FT_ClrRts = safeGetDelegate(this.pFT_ClrRts);
this.FT_ResetDevice = safeGetDelegate(this.pFT_ResetDevice);
this.FT_ResetPort = safeGetDelegate(this.pFT_ResetPort);
this.FT_CyclePort = safeGetDelegate(this.pFT_CyclePort);
this.FT_Purge = safeGetDelegate(this.pFT_Purge);
this.FT_SetTimeouts = safeGetDelegate(this.pFT_SetTimeouts);
this.FT_SetBreakOn = safeGetDelegate(this.pFT_SetBreakOn);
this.FT_SetBreakOff = safeGetDelegate(this.pFT_SetBreakOff);
this.FT_GetDeviceInfo = safeGetDelegate(this.pFT_GetDeviceInfo);
this.FT_SetResetPipeRetryCount = safeGetDelegate(this.pFT_SetResetPipeRetryCount);
this.FT_StopInTask = safeGetDelegate(this.pFT_StopInTask);
this.FT_RestartInTask = safeGetDelegate(this.pFT_RestartInTask);
this.FT_GetDriverVersion = safeGetDelegate(this.pFT_GetDriverVersion);
this.FT_GetLibraryVersion = safeGetDelegate(this.pFT_GetLibraryVersion);
this.FT_SetDeadmanTimeout = safeGetDelegate(this.pFT_SetDeadmanTimeout);
this.FT_SetChars = safeGetDelegate(this.pFT_SetChars);
this.FT_SetEventNotification = safeGetDelegate(this.pFT_SetEventNotification);
this.FT_GetComPortNumber = safeGetDelegate(this.pFT_GetComPortNumber);
this.FT_SetLatencyTimer = safeGetDelegate(this.pFT_SetLatencyTimer);
this.FT_GetLatencyTimer = safeGetDelegate(this.pFT_GetLatencyTimer);
this.FT_SetBitMode = safeGetDelegate(this.pFT_SetBitMode);
this.FT_GetBitMode = safeGetDelegate(this.pFT_GetBitMode);
this.FT_SetUSBParameters = safeGetDelegate(this.pFT_SetUSBParameters);
this.FT_ReadEE = safeGetDelegate(this.pFT_ReadEE);
this.FT_WriteEE = safeGetDelegate(this.pFT_WriteEE);
this.FT_EraseEE = safeGetDelegate(this.pFT_EraseEE);
this.FT_EE_UASize = safeGetDelegate(this.pFT_EE_UASize);
this.FT_EE_UARead = safeGetDelegate(this.pFT_EE_UARead);
this.FT_EE_UAWrite = safeGetDelegate(this.pFT_EE_UAWrite);
this.FT_EE_Read = safeGetDelegate(this.pFT_EE_Read);
this.FT_EE_Program = safeGetDelegate(this.pFT_EE_Program);
this.FT_EEPROM_Read = safeGetDelegate(this.pFT_EEPROM_Read);
this.FT_EEPROM_Program = safeGetDelegate(this.pFT_EEPROM_Program);
}
~NativeMethods()
{
this.Dispose(false);
}
#endregion
#region METHODS
///
/// Dispose the object.
///
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose the object.
///
/// 'true' if Dispose() was called, 'false' if finalized.
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.FT_CreateDeviceInfoList = null;
this.FT_GetDeviceInfoDetail = null;
this.FT_Rescan = null;
this.FT_Reload = null;
this.FT_Open = null;
this.FT_OpenEx = null;
this.FT_OpenExLoc = null;
this.FT_Close = null;
this.FT_Read = null;
this.FT_Write = null;
this.FT_GetQueueStatus = null;
this.FT_GetModemStatus = null;
this.FT_GetStatus = null;
this.FT_SetBaudRate = null;
this.FT_SetDivisor = null;
this.FT_SetDataCharacteristics = null;
this.FT_SetFlowControl = null;
this.FT_SetDtr = null;
this.FT_ClrDtr = null;
this.FT_SetRts = null;
this.FT_ClrRts = null;
this.FT_ResetDevice = null;
this.FT_ResetPort = null;
this.FT_CyclePort = null;
this.FT_Purge = null;
this.FT_SetTimeouts = null;
this.FT_SetBreakOn = null;
this.FT_SetBreakOff = null;
this.FT_GetDeviceInfo = null;
this.FT_SetResetPipeRetryCount = null;
this.FT_StopInTask = null;
this.FT_RestartInTask = null;
this.FT_GetDriverVersion = null;
this.FT_GetLibraryVersion = null;
this.FT_SetDeadmanTimeout = null;
this.FT_SetChars = null;
this.FT_SetEventNotification = null;
this.FT_GetComPortNumber = null;
this.FT_SetLatencyTimer = null;
this.FT_GetLatencyTimer = null;
this.FT_SetBitMode = null;
this.FT_GetBitMode = null;
this.FT_SetUSBParameters = null;
this.FT_ReadEE = null;
this.FT_WriteEE = null;
this.FT_EraseEE = null;
this.FT_EE_UASize = null;
this.FT_EE_UARead = null;
this.FT_EE_UAWrite = null;
this.FT_EE_Read = null;
this.FT_EE_Program = null;
this.FT_EEPROM_Read = null;
this.FT_EEPROM_Program = null;
this.pFT_CreateDeviceInfoList = IntPtr.Zero;
this.pFT_GetDeviceInfoDetail = IntPtr.Zero;
this.pFT_Rescan = IntPtr.Zero;
this.pFT_Reload = IntPtr.Zero;
this.pFT_Open = IntPtr.Zero;
this.pFT_OpenEx = IntPtr.Zero;
this.pFT_OpenExLoc = IntPtr.Zero;
this.pFT_Close = IntPtr.Zero;
this.pFT_Read = IntPtr.Zero;
this.pFT_Write = IntPtr.Zero;
this.pFT_GetQueueStatus = IntPtr.Zero;
this.pFT_GetModemStatus = IntPtr.Zero;
this.pFT_GetStatus = IntPtr.Zero;
this.pFT_SetBaudRate = IntPtr.Zero;
this.pFT_SetDivisor = IntPtr.Zero;
this.pFT_SetDataCharacteristics = IntPtr.Zero;
this.pFT_SetFlowControl = IntPtr.Zero;
this.pFT_SetDtr = IntPtr.Zero;
this.pFT_ClrDtr = IntPtr.Zero;
this.pFT_SetRts = IntPtr.Zero;
this.pFT_ClrRts = IntPtr.Zero;
this.pFT_ResetDevice = IntPtr.Zero;
this.pFT_ResetPort = IntPtr.Zero;
this.pFT_CyclePort = IntPtr.Zero;
this.pFT_Purge = IntPtr.Zero;
this.pFT_SetTimeouts = IntPtr.Zero;
this.pFT_SetBreakOn = IntPtr.Zero;
this.pFT_SetBreakOff = IntPtr.Zero;
this.pFT_GetDeviceInfo = IntPtr.Zero;
this.pFT_SetResetPipeRetryCount = IntPtr.Zero;
this.pFT_StopInTask = IntPtr.Zero;
this.pFT_RestartInTask = IntPtr.Zero;
this.pFT_GetDriverVersion = IntPtr.Zero;
this.pFT_GetLibraryVersion = IntPtr.Zero;
this.pFT_SetDeadmanTimeout = IntPtr.Zero;
this.pFT_SetChars = IntPtr.Zero;
this.pFT_SetEventNotification = IntPtr.Zero;
this.pFT_GetComPortNumber = IntPtr.Zero;
this.pFT_SetLatencyTimer = IntPtr.Zero;
this.pFT_GetLatencyTimer = IntPtr.Zero;
this.pFT_SetBitMode = IntPtr.Zero;
this.pFT_GetBitMode = IntPtr.Zero;
this.pFT_SetUSBParameters = IntPtr.Zero;
this.pFT_ReadEE = IntPtr.Zero;
this.pFT_WriteEE = IntPtr.Zero;
this.pFT_EraseEE = IntPtr.Zero;
this.pFT_EE_UASize = IntPtr.Zero;
this.pFT_EE_UARead = IntPtr.Zero;
this.pFT_EE_UAWrite = IntPtr.Zero;
this.pFT_EE_Read = IntPtr.Zero;
this.pFT_EE_Program = IntPtr.Zero;
this.pFT_EEPROM_Read = IntPtr.Zero;
this.pFT_EEPROM_Program = IntPtr.Zero;
}
// FreeLibrary here - we should only do this if we are completely finished
if (this.hFTD2XXDLL != IntPtr.Zero && this.hFTD2XXDLL != null)
{
FreeLibrary(this.hFTD2XXDLL);
this.hFTD2XXDLL = IntPtr.Zero;
}
}
///
/// Get delegate in a safe manner returning a null object if the pointer is null.
///
/// Delegate type.
/// Pointer to delegate procedure.
/// Delegate method instance.
private static T safeGetDelegate(IntPtr ptr)
{
if (ptr == null || ptr == IntPtr.Zero)
{
return default(T);
}
return (T)(object)Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));
}
#endregion
}
}