Driver Installation Through a *.INF file [In VB.NET]

Driver Installation Through a *.INF file [In VB.NET]

post id: 67

post length: 12953

post datetime: 9/30/2010 3:49:00 PM

post ip: 95.72.231.126

Driver Installation Through a *.INF file [In VB.NET]

--------------------------------

corrected from October, 6th 2010

[1]

Простая установка драйвера:

Classes/Driver/clsDriverCtrl.vb:

http://pesap.com/source/DRINST.ZIP

[2]

Установка через INF-файл /особенно для компонентов сети и NDIS в частности/

По сути это - переработка под VB.NET примера на C++:

http://www.boudrand.net/index.php?option=com_content&view=article&id=5:filter-driver-installation&catid=3:dloppement-sous-windows&Itemid=3

[!]

Декларация интерфейсов [INetCfg], [INetCfgClass], [INetCfgClassSetup], [INetCfgComponent] и [INetCfgLock] взята с:

http://www.codeproject.com/KB/cs/adapterlist.aspx

/ram verma/

[должен признать, что сразу не вникся насколько

этот пример замечателен. В итоге на 90% взята

его основа!]

--{

[Should tell that at once it was not penetrated how much

this example is remarkable. As a result on 90 % it is

taken its basis!]

}--

[!]

Все описаны в DDKhttp://msdn.microsoft.com/en-us/library/ff559080(v=VS.85).aspx}

Что нужно:

индификаторы интерфейса:

// Reserved GUIDS for Microsoft use

//

// C0E8AE93-306E-11D1-AACF-00805FC1270E IID_INetCfg

// C0E8AE9D-306E-11D1-AACF-00805FC1270E IID_INetCfgClassSetup

// C0E8AE9E-306E-11D1-AACF-00805FC1270E IID_INetCfgComponentBindings

// C0E8AE9F-306E-11D1-AACF-00805FC1270E IID_INetCfgLock

+

Идентификатор класса:

const CLSID CLSID_CNetCfg

= {0x5B035261, 0x40F9, 0x11D1, {0xAA, 0xEC, 0x00, 0x80, 0x5F, 0xC1, 0x27, 0x0E}};

= 5B035261-40F9-11D1-AAEC-00805FC1270E

+

Others GUID:

NDIS miniport drivers /NDIS intermediate drivers/

= 4d36e972-e325-11ce-bfc1-08002be10318

Network Transport:

= 4d36e975-e325-11ce-bfc1-08002be10318

<DllImport("SETUPAPI.DLL", Entrypoint:="SetupCopyOEMInfW", setlasterror:=True, _

CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SetupCopyOEMInf( _

<MarshalAs(UnmanagedType.VBByRefStr)> ByRef T0 As String, _

<MarshalAs(UnmanagedType.VBByRefStr)> ByRef OEMSouceMediaLocation As String, _

<[In]()> ByVal OEMSourceMediaType As Integer, _

<[In]()> ByVal CopyStyle As Integer, _

<[Out](), [Optional]()> ByRef DestinationInfFileName As String, _

<[In]()> ByVal DestinationInfFileNameSize As Integer, _

<[Out](), [Optional]()> ByRef RequiredSize As Integer, _

<[Out](), [Optional]()> ByRef DestinationInfFileNameComponent As String _

) As Boolean

End Function

<StructLayout(LayoutKind.Sequential)> _

Public Structure OBO_TOKEN

<MarshalAs(UnmanagedType.I4)> _

Public Type As Integer

<MarshalAs(UnmanagedType.IUnknown)> _

Public pncc As Object

<MarshalAs(UnmanagedType.LPWStr)> _

Public pszwManufacturer As String

<MarshalAs(UnmanagedType.LPWStr)> _

Public pszwProduct As String

<MarshalAs(UnmanagedType.LPWStr)> _

Public pszwDisplayName As String

<MarshalAs(UnmanagedType.Bool)> _

Public fRegistered As Boolean

End Structure

<DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> _

Public Shared Function CoInitializeEx( _

ByVal pvReserved As [IntPtr], _

ByVal dwCoInit As Integer _

) As Integer

End Function

Declare Auto Function CoCreateInstance Lib "Ole32.dll" ( _

<[In]()> ByRef clsid As Guid, _

<MarshalAs(UnmanagedType.IUnknown)> ByVal punkOuter As Object, _

ByVal context As Integer, _

<[In]()> ByRef iid As Guid, _

<MarshalAs(UnmanagedType.IUnknown)> ByRef o As Object) _

As Integer

Interface`s

'-------------------

http://www.codeproject.com/KB/cs/adapterlist.aspx

<Guid("C0E8AE93-306E-11D1-AACF-00805FC1270E"), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _

ComVisible(True)> _

Friend Interface INetCfg

Function Initialize(ByVal pvReserved As System.IntPtr) As Integer

Function Uninitialize() As Integer

Function Apply() As Integer

Function Cancel() As Integer

Function EnumComponents(ByVal pguidClass As System.IntPtr, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppenumComponent As Object) As Integer

Function FindComponent(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszwInfId As String, <MarshalAs(UnmanagedType.IUnknown)> ByRef pComponent As Object) As Integer

Function QueryNetCfgClass(ByRef pguidClass As System.Guid, ByRef riid As System.Guid, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppvObject As Object) As Integer

End Interface

<ComImport(), _

ComVisible(True), _

Guid("C0E8AE9F-306E-11D1-AACF-00805FC1270E"), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _

Public Interface INetCfgLock

Inherits IUnknown

Function AcquireWriteLock(<MarshalAs(UnmanagedType.I4)> ByVal cmsTimeout As System.Int32, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszwClientDescription As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal ppszwClientDescription As String) As Integer

Function ReleaseWriteLock() As Integer

Function IsWriteLocked(<MarshalAs(UnmanagedType.LPWStr)> ByVal ppszwClientDescription As String) AsInteger

End Interface

<Guid("C0E8AE97-306E-11D1-AACF-00805FC1270E"), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _

ComVisible(True)> _

Friend Interface INetCfgClass

Function FindComponent(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszwInfId As String, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppnccItem As Object) As Integer

Function EnumComponents(<MarshalAs(UnmanagedType.IUnknown)> ByRef ppenumComponent AsObject) As Integer

End Interface

<ComVisible(True), _

Guid("C0E8AE9D-306E-11D1-AACF-00805FC1270E"), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _

Friend Interface INetCfgClassSetup

Function SelectAndInstall(ByVal hwndParent As System.IntPtr, ByVal pOboToken As System.IntPtr, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppnccItem As Object) As Integer

Function Install(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszwInfId As String, ByVal pOboToken AsSystem.IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal dwSetupFlags As Integer, <MarshalAs(UnmanagedType.U4)> ByVal dwUpgradeFromBuildNo As Integer, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszwAnswerFile As String, <MarshalAs(UnmanagedType.LPWStr)> ByVal pszwAnswerSections As String, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppnccItem As Object) AsInteger

Function DeInstall(<MarshalAs(UnmanagedType.IUnknown)> ByVal pComponent As Object, ByVal pOboToken As System.IntPtr, <MarshalAs(UnmanagedType.LPWStr)> ByRef pmszwRefs As String) As Integer

End Interface

<ComVisible(True), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _

Guid("C0E8AE99-306E-11D1-AACF-00805FC1270E")> _

Friend Interface INetCfgComponent

Function GetDisplayName(<MarshalAs(UnmanagedType.LPWStr)> ByRef ppszwDisplayName As String) AsInteger

Function SetDisplayName(<MarshalAs(UnmanagedType.LPWStr)> ByVal pszwDisplayName As String) AsInteger

Function GetHelpText(<MarshalAs(UnmanagedType.LPWStr)> ByRef pszwHelpText As String) As Integer

Function GetId(<MarshalAs(UnmanagedType.LPWStr)> ByRef ppszwId As String) As Integer

Function GetCharacteristics(<MarshalAs(UnmanagedType.U4)> ByRef pdwCharacteristics As Integer) AsInteger

Function GetInstanceGuid(ByVal pGuid As System.Guid) As Integer

Function GetPnpDevNodeId(<MarshalAs(UnmanagedType.LPWStr)> ByRef ppszwDevNodeId As String) AsInteger

Function GetClassGuid(ByVal pGuid As System.Guid) As Integer

Function GetBindName(<MarshalAs(UnmanagedType.LPWStr)> ByRef ppszwBindName As String) AsInteger

Function GetDeviceStatus(<MarshalAs(UnmanagedType.U4)> ByRef pulStatus As Integer) As Integer

Function OpenParamKey(<MarshalAs(UnmanagedType.U4)> ByVal phkey As System.IntPtr) As Integer

Function RaisePropertyUi(ByVal hwndParent As System.IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal dwFlags As Integer, <MarshalAs(UnmanagedType.IUnknown)> ByVal punkContext As Object) As Integer

End Interface

<ComImport(), _

ComVisible(False), _

Guid("00000000-0000-0000-C000-000000000046"), _

InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _

Public Interface IUnknown

Function QueryInterface(ByRef pguidClass As System.Guid, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppvObject As Object) As Integer

Function AddRef() As Integer

Function Release() As Integer

End Interface

Примечание:

Для использования функции CoCreateInstance необходимо знать идентификаторы класса

объекта и интерфейса. Идентификатор класса с префиксом CLSID_, определяет класс

объекта, который вы создаете, а ссылка с префиксом IID_ — это

конкретный интерфейс, который вы ищете.

http://netlib.narod.ru/library/book0051/ch01_03.htm}

-----------------

Хотя в нашем случае будем использовать

маршализацию для COM-интерфейсов:

Public Shared Function Install(ByVal InfPath As String) As Boolean

'

'CoInitializeEx(Nothing, 6)

'

SetupCopyOEMInf(InfPath, Nothing, 1, 0, Nothing, 0, Nothing, Nothing)

'

Dim IID_INetCfg As Guid _

= New Guid("C0E8AE93-306E-11D1-AACF-00805FC1270E") _

, CLSID_CNetCfg As Guid _

= New Guid("5B035261-40F9-11D1-AAEC-00805FC1270E") _

, IID_INetCfgLock As Guid _

= New Guid("C0E8AE9F-306E-11D1-AACF-00805FC1270E") _

, IID_DevClassNet As Guid _

= New Guid("4D36E972-E325-11CE-BFC1-08002BE10318") _

, IID_INetCfgClass As Guid _

= New Guid("C0E8AE97-306E-11D1-AACF-00805FC1270E") _

, IID_INetCfgClassSetup As Guid _

= New Guid("C0E8AE9D-306E-11D1-AACF-00805FC1270E") _

, IPtr As [IntPtr] = Nothing

'

Dim NetCfg As INetCfg _

= Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_CNetCfg, True)) _

, NetCfgClassSetup As INetCfgClassSetup = Nothing _

, NetCfgComponent As INetCfgComponent = Nothing _

, NetCfgClass As INetCfgClass = Nothing _

, NetCfgLock As INetCfgLock = Nothing

'

If Marshal.IsComObject(NetCfg) Then

'

Marshal.QueryInterface( _

Marshal.GetIUnknownForObject(NetCfg) _

, IID_INetCfgLock, IPtr)

'

NetCfgLock = Marshal.GetObjectForIUnknown(IPtr)

'

'-----------------

'lock`ing network:

'-----------------

NetCfgLock.AcquireWriteLock(UInt64.Parse("15000"), "DRINST", Nothing)

'

NetCfg.Initialize(System.IntPtr.Zero)

'

NetCfg.QueryNetCfgClass(IID_DevClassNet, IID_INetCfgClass, NetCfgClass)

'

NetCfg.QueryNetCfgClass(IID_DevClassNet, IID_INetCfgClassSetup, NetCfgClassSetup)

'

Dim OboToken As New [OBO_TOKEN]

OboToken.Type = 1

IPtr = Marshal.AllocHGlobal(Marshal.SizeOf(OboToken))

Marshal.StructureToPtr(OboToken, IPtr, True)

NetCfgClassSetup.Install("ID", IPtr, 2, 0, Nothing, Nothing, NetCfgComponent)

'

NetCfg.Apply()

'

'-------------------

'Unlock`ing network:

'-------------------

NetCfgLock.ReleaseWriteLock()

'

End If

'

Marshal.FreeHGlobal(IPtr)

''

End Function

Вопрос

http://pesap.com/INetCfgLock_exept_4.html} от /Saturday, October 09, 2010/ снят...

ошибка найдена.

;)

THE END