c# - 如何在安装程序期间使用 SetupCopyOEMInf

标签 c# .net driver

我有一个驱动程序信息和目录,是用 Libusbdotnet 为我的 USB 设备生成的。

一切正常,除了整个驱动程序安装真的很麻烦。因为我的驱动程序没有数字签名,也不在 WHQL 中……需要大量用户交互才能为我的设备安装驱动程序。

因此,如果可能的话,我希望在不签名和 WHQL 的情况下自动执行此操作。所以我做了一些研究并找到了 SetupCopyOEMInf 函数。

我有一个用 c# 和 .net 编写的应用程序,还有一个 MSI 程序包。

基本上,我希望能够在安装过程中使用 SetupCopyOEMInf,这样它就可以复制驱动程序,以便 Windows 可以在用户首次插入设备时自动检测驱动程序。

但是我找不到任何示例来说明如何使用 SetupCopyOEMInf。

感谢任何帮助。

谢谢,

最佳答案

我不确定这是否有帮助,但这里有一些示例代码,说明如何使用该函数本身。我几乎用过 Microsoft's MSDNPinvoke敲一些快速代码。

[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupCopyOEMInf(

    string SourceInfFileName,
    string OEMSourceMediaLocation,
    OemSourceMediaType OEMSourceMediaType,
    OemCopyStyle CopyStyle,
    string DestinationInfFileName,
    int DestinationInfFileNameSize,
    ref int RequiredSize,
    string DestinationInfFileNameComponent

);

/// <summary>
/// Driver media type
/// </summary>
internal enum OemSourceMediaType
{
    SPOST_NONE = 0,
    //Only use the following if you have a pnf file as well
    SPOST_PATH = 1,
    SPOST_URL = 2,
    SPOST_MAX = 3
}

internal enum OemCopyStyle
{
    SP_COPY_NEWER = 0x0000004,   // copy only if source newer than or same as target
    SP_COPY_NEWER_ONLY = 0x0010000,   // copy only if source file newer than target
    SP_COPY_OEMINF_CATALOG_ONLY = 0x0040000,   // (SetupCopyOEMInf only) don't copy INF--just catalog
}

static void Main(string[] args)
{
    //Not really needed but I couldn't figure out how to not specify a ref parameter
    int size = 0;
    bool success = SetupCopyOEMInf("source.inf", "", OemSourceMediaType.SPOST_NONE, OemCopyStyle.SP_COPY_NEWER, null, 0,
                    ref size, null);
    if(!success)
    {
        var errorCode = Marshal.GetLastWin32Error();
        var errorString = new Win32Exception(errorCode).Message;
        Console.WriteLine(errorString);
        Console.ReadLine();
    }
}

假设您的 INF 文件与指定的正确目标目录完全正确,此函数还将尝试将您通过 CopyFiles 指令(在 INF 文件中)指定的任何文件复制到这些目标目录。如果文件不存在,命令将失败。

我遇到的另一个问题是该函数应该将您的 INF 和 CAT 文件复制到指定的目标目录(我将 ID 指定为 12,即 documented 作为 %windir%\system32\drivers)但它却复制了它到 %windir%\system32\DriverStore\FileRepository\source.inf_amd64_neutral_blah。这可能是因为我正在使用我手动创建的 inf 文件进行测试,但缺少所需的信息。

希望对您有所帮助:)

关于c# - 如何在安装程序期间使用 SetupCopyOEMInf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18404660/

相关文章:

c# - 在 .NET 中使用 PerformanceCounter 测量 CPU 使用率的正确方法

c# - 如何使用配置管理器

c# - Visual Studio 2008 : Step to next line is very slow when debugging managed code

c# - 使用 ThreadPool 进行垃圾收集

java - oracle11g 和 JDBC 中的自动增量

linux - 使用 linux I2C 驱动程序

java - 为什么 postgresql 使用错误版本的驱动程序?

c# - 接受两个接口(interface)之一的方法

c# - 如何从 WCF 回调类更新 GUI 标签?

在将 HTML 字符串发送到客户端之前格式化 HTML 字符串的 C# 工具