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

pdi*_*ddy 3 .net c# driver

我有一个驱动程序inf和目录,它是用我的USB设备用Libusbdotnet生成的.

一切都运行良好,除了整个驱动程序安装真的很麻烦.因为我的驱动程序没有经过数字签名而且不在WHQL中...所以需要很多用户交互才能为我的设备安装驱动程序.

所以我希望在没有签名和WHQL的情况下自动执行此操作.所以我做了一点研究,发现了SetupCopyOEMInf函数.

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

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

但我找不到任何示例如何使用SetupCopyOEMInf.

任何帮助表示赞赏.

谢谢,

小智 7

我不确定这是否有用,但这里有一些如何使用函数本身的示例代码.我几乎使用微软的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

假设你的INF文件是用指定正确的目标目录完全正确的,这个功能也将尝试,并且已经通过CopyFiles部分指令规定(INF文件)的任何文件复制到这些目标目录.如果文件不存在,该命令将失败.

另外一个问题,我是将函数的INF和CAT文件复制到指定的目标目录(我指定的12的ID,这是记录为%WINDIR%\ SYSTEM32\DRIVERS),而是它它复制到%WINDIR%\SYSTEM32\DriverStore\FileRepository文件\ source.inf_amd64_neutral_blah.这可能是因为我正在测试我手工创建的inf文件并且缺少必需的信息.

希望这对你有所帮助:)