简体   繁体   English

未安装驱动程序时如何禁止FTDI .NET DLL提醒用户

[英]How to prohibit FTDI .NET DLL from alerting user when drivers are not installed

I have a C# application that's using the FTD2XX.DLL from FTDI. 我有一个使用FTDI的FTD2XX.DLL的C#应用​​程序。 This application is used for multiple generations of a single product and abstracts the physical hardware. 该应用程序用于单个产品的多代产品,并抽象了物理硬件。 There's an FTDI and a HID implementation. 有一个FTDI和HID实现。

The application searches for both appropriate FTDI and HID devices, though it's likely that no FTDI drivers exist if the user has the HID generation. 该应用程序同时搜索适当的FTDI和HID设备,但是如果用户生成了HID,则很可能不存在FTDI驱动程序。

Background aside now. 除了背景。 When I instantiate the FTDI class I get a modal, not generated by my code about not finding the FTDI driver and asks the user if the drivers are installed. 当我实例化FTDI类时,我得到了一个模态代码,不是由我的代码生成的,关于找不到FTDI驱动程序,并询问用户是否安装了驱动程序。 I tried wrapping this in a TRY/CATCH block but no exception is thrown. 我尝试将其包装在TRY / CATCH块中,但没有引发异常。

1: Is there a way to determine if the FTDI drivers are installed before trying to instantiate the FTDI class? 1:有没有办法在尝试实例化FTDI类之前确定是否已安装FTDI驱动程序?

2: If not, is there a way to prohibit the FTDI dll from alerting the user when this happens? 2:如果没有,是否有一种方法可以阻止FTDI dll在发生这种情况时提醒用户?

I have the exact same requirements - in my case I'm enumerating the list of standard serial ports and appending this with the list of any attached FTDI devices. 我有完全相同的要求-就我而言,我枚举了标准串行端口列表,并将其附加到所有连接的FTDI设备的列表中。 If the driver isn't installed, then I'd like to not have those modal dialog boxes appear. 如果未安装驱动程序,那么我不想出现那些模式对话框。 One quick and dirty way I've figured out to do this is to check for the file FTD2XX.DLL being in c:\\windows\\system32 (or wherever windows is installed). 我发现执行此操作的一种快速而又肮脏的方法是检查c:\\ windows \\ system32(或任何Windows安装位置)中的文件FTD2XX.DLL。 The existence of this file basically means the driver is installed. 此文件的存在基本上意味着已安装驱动程序。

// c# example
string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
bool installed = File.Exists(path + Path.DirectorySeparatorChar + "FTD2XX.DLL");

Another way: 其它的办法:

[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);    

public bool IsDriverInstalled()
{
  //trying to load library
  IntPtr handler = LoadLibrary(@"FTD2XX.DLL");

  if (handler == IntPtr.Zero)
      return false;
  else
      return true; // Driver is installed

  //Don't forget to free .dll
  FreeLibrary(handler);
}

It sounds as though you are bundling FTD2XX.DLL with your application. 听起来好像您在将FTD2XX.DLL与应用程序捆绑在一起。

You shouldn't do this, you should use the latest DLL installed into the Windows system directory by the FTDI driver. 您不应该这样做,应该使用FTDI驱动程序安装到Windows系统目录中的最新DLL。 If you have an old version of the DLL in your app directory, and the user has newer drivers (possibly installed by some other FTDI-based device), you could have all kinds of trouble. 如果您的应用程序目录中具有旧版本的DLL,并且用户具有更新的驱动程序(可能由其他基于FTDI的设备安装),则可能会遇到各种麻烦。

As a bonus, this solves your problem in most cases: If FTD2XX.DLL isn't installed on the system, you'll get an exception trying to p/invoke, which you can catch. 另外,在大多数情况下,这可以解决您的问题:如果系统上未安装FTD2XX.DLL,则您将获得尝试p / invoke的异常,您可以捕获该异常。

To avoid the error perfectly, however, you'll need to do the same check that the FTD2XX.DLL does internally (since the DLL obviously can exist on the system without any driver). 但是,为完全避免该错误,您需要做同样的检查以确保FTD2XX.DLL在内部执行(因为DLL显然可以在没有任何驱动程序的系统上存在)。 For example, checking whether the driver is listed in the registry under HKLM\\System\\CurrentControlSet\\services would be a more robust check than the one you have. 例如,检查驱动程序是否在注册表中列出在HKLM\\System\\CurrentControlSet\\services ,比您拥有的检查更可靠。 Still not sure if it's equivalent to FTDI's own check. 仍不确定它是否等同于FTDI自己的支票。

I rewrote the FTDI library that was causing the error: it was caused by a MessageBox.Show in the constructor. 我重写了导致错误的FTDI库:它是由构造函数中的MessageBox.Show引起的。 I have replaced this with a normal Exception throw. 我已经用正常的异常抛出代替了它。

See my blog for the re-engineered wrapper and code: connecting to FTDI devices in Silverlight 5 RC 有关重新设计的包装和代码,请参见我的博客: 在Silverlight 5 RC中连接到FTDI设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM