How to use SetupCopyOEMInf during installer

.net, c#, driver

Solution

I am not sure if this will help, but here is some example code of how to use the function itself. I pretty much used Microsoft's MSDN and Pinvoke to knock up some quick code.

[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();
    }
}

Assuming your INF file is completely correct with the proper destination directories specified, this function will also try and copy any files that you have specified via the CopyFiles directive (in the INF file) to those destination directories. If the file doesnt exist, the command will fail.

One other problem I had was that the function should copy your INF and CAT files to the destination directory specified (I specified an ID of 12, which is documented as %windir%\system32\drivers) but instead it copied it to %windir%\system32\DriverStore\FileRepository\source.inf_amd64_neutral_blah. This was probably due to the fact I was testing with an inf file I created by hand and was missing required information.

Hope this helps you a bit :)

Problem

I have a driver inf and catalog that was generated with Libusbdotnet for my USB device. Everything works well, except the whole driver installation that is really cumbersome. Because my driver is not digitally signed and it's not in WHQL ... it takes alot of user interaction to install the driver for my device. So I'm looking to do this automatically still without signing and WHQL if possible. So I did a little researched and found SetupCopyOEMInf function. I have an application written in c# and .net and also have a MSI package. Basically I would like to be able to use SetupCopyOEMInf during the installation so it can copy the drivers so that windows can detect automatically the driver when the user first plugs the device in. But I can't find any exemple showing how to use SetupCopyOEMInf. Any help is appreciated. Thank you,

Original source