How to install the CDC_Driver from a c# application

My EMX board is connected to a computer, it shows various data through a GUI written in C#. The connection between the board and the computer is the CDC_Driver provided byy GHI. The main problem, is that I would like to include the device driver in my GUI in such a way that if the driver is not install, the GUI will propose to install it. I am using click once to publish my application.

For now I have a look on the DIFXApi.

       [DllImport("DIFXApi.dll", CharSet = CharSet.Unicode)]
        public static extern Int32 DriverPackagePreinstall(string DriverPackageInfPath, Int32 Flags);

        static void driverinstall()
        {
            DirectoryInfo assemblyDir = new DirectoryInfo(Application.ExecutablePath);
            DirectoryInfo installDir = assemblyDir.Parent;
            int result = DriverPackagePreinstall(installDir.FullName + @ "..\\Driver\\CDC_Driver.inf", 0);
            if (result != 0)
                MessageBox.Show("Driver installation failed. " + result);
        }

but this returns the error 3 (invalid path). If I run

 int result = DriverPackagePreinstall(@ "C:\Driver\CDC_Driver.inf", 0);

I get ACCESS_DENIED

I have also see the SetupCopyOEMInf method but I don’t know how to import the setup.dll and where to find it.

Any help would be appreciated

Ok so any idea on how to achive that?

I will try to read the file and will let U know

This is very interesting. Please share results if you have it worked out.

Yep I can access the file on c:/Driver/ directory as you can see in the following log:

'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\clement\Desktop\USU\CC\USUCC_SRC\bin\Debug\UPSIDEUP.exe', Symbols loaded.
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\clement\Desktop\USU\CC\USUCC_SRC\bin\Debug\UPSIDEUP_LIBRARY_PROJ.dll', Symbols loaded.
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
'UPexe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'
'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
; Installation inf for CDC Interface
;
; (c) Copyright 2009 GHI Electronics, LLC
;


[Version] 
Signature="$Windows NT$" 
Class=Ports
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318} 
Provider=%GHI% 
LayoutFile=layout.inf
DriverVer=2/1/2009,1.0.0000.0

[Manufacturer] 
%GHI%=GHI,NTamd64

[DestinationDirs] 
DefaultDestDir=12 

[SourceDisksFiles]

[SourceDisksNames]

[GHI] 
%CDC%=DDInstall, USB\Vid_1B9F&Pid_F001

[GHI.NTamd64]
%CDC%=DDInstall, USB\Vid_1B9F&Pid_F001




[DDInstall]
include=mdmcpq.inf 
CopyFiles=DCopyFiles
AddReg=DDInstall.AddReg 

[DCopyFiles]
usbser.sys,,,0x20

[DDinstall.AddReg] 
HKR,,DevLoader,,*ntkern 
HKR,,NTMPDriver,,usbser.sys 
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider" 

[DDInstall.Services] 
AddService=usbser, 0x00000002, CDC_DriverService


[CDC_DriverService] 
DisplayName=%GHI_SERVICE% 
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%\usbser.sys 


[Strings] 
GHI="GHI Electronics, LLC"
CDC = "CDC Interface"
GHI_SERVICE="CDC Interface Service"

'UP.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_fr_b77a5c561934e089\mscorlib.resources.dll'
09:44:57System.IO.IOException: Descripteur non valide

   à System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   à System.IO.__ConsoleStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   à System.IO.StreamReader.ReadBuffer()
   à System.IO.StreamReader.ReadLine()
   à System.IO.TextReader.SyncTextReader.ReadLine()
   à System.Console.ReadLine()
   à UP.Program.readdriver() dans C:\Users\clement\Desktop\USU\CC\USUCC_SRC\Program.cs:ligne 50
   à UP.Program.Main() dans C:\Users\clement\Desktop\USU\CC\USUCC_SRC\Program.cs:ligne 68
The program '[1852] UPSIDEUP.exe: Program Trace' has exited with code 0 (0x0).
The program '[1852] UPSIDEUP.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

There’s some exceptions but file can be access. Therefore I think that the problem is due to security property

I am just trying to use:

[DllImport("Setupapi.dll", EntryPoint="InstallHinfSection", CallingConvention=CallingConvention.StdCall)]
public static extern void InstallHinfSection(
    [In] IntPtr hwnd,
    [In] IntPtr ModuleHandle,
    [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer,
    int nCmdShow);

found here: http://www.techques.com/question/1-2032493/install-uninstall-an-.inf-driver-programmatically-using-C

At least I have no errors but I am not sure if it works. If I uninstall the driver in Windows peripherals manager and unplug/ plug the EMX board it still succeed to find the correct driver

In fact the driver was still present on the system. After deleting the driver properly using the device manager, it does not find it anymore.
Unfortunately the InstallHinfSection seems to not install the driver. But it does not print errors.

Argh!!! This sounds to me as a foreign language… I will try to make it compile first.

Driver PackageInstall returns 5 with a correct path and 2 with a uncorrect one. Where can I found the error corresponding to 5. The c++ msdn doc on it specifies return code as DWORD…

from visual. I didn’t believe it will make any difference.

I’ve just tried from the gui app, app starts but when I plug the emx it still ask for the cdc Vcom driver. Therefore it has not been added to the driverstore… Windows is not an easy os to know what can be wrong.

Do you know if there’s a log file that I can check to have more details on what is going wrong.

i will check tomorrow.

Did you have any success with driver installation from C# application?