Install KMDF programatically using DiInstallDriver

I want to install a kmdf driver on Windows 7 32bit machine. At first I used hdwwiz (the add hardware tool) and it worked correctly.

Now I want to install it programatically using a software. After some research I found DiInstallDriver I used the following code

int _tmain(int argc, _TCHAR* argv)
{
std::wstring FilePath = L".inf";
bool result = DiInstallDriver(NULL, FilePath.c_str(), DIIRFLAG_FORCE_INF, false);
DWORD error = GetLastError();
if (!result){
printf(“The driver is not installed \r\n”);
printf("The error is %x ", error);
}else
{
printf(“The driver is installed correctly !”);
}

getchar();
return 0;
}

After running the software I get “The driver is installed correctly !”, that means the functions returns with success but I cannot find it in the Device Manager?

Any idea what is going wrong ?

xxxxx@gmail.com wrote:

I want to install a kmdf driver on Windows 7 32bit machine. At first I used hdwwiz (the add hardware tool) and it worked correctly.

What kind of driver? Is this a driver for a real piece of hardware, or
is it software-enumerated? The fact that you were using hdwwiz suggests
that you are creating your own fake device to drive. What is the
hardware ID in your INF file?

Now I want to install it programatically using a software. After some research I found DiInstallDriver I used the following code

If it is a fake device, then that’s the problem. DiInstallDriver only
installs drivers for real hardware, or at least for devices that already
exist. You need to have someone create a new device for your driver to
drive. That’s what hdwwiz and “devcon install” both do. You can see
how in the devcon source, which is part of the WDK.

After running the software I get “The driver is installed correctly !”, that means the functions returns with success but I cannot find it in the Device Manager?

Right. Your driver was installed correctly. There’s simply no device
for it to drive.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

There are 2 separate steps:

  • install a driver package to the machine. Look at the source of “devcon dp_add” for this. IIRC SetupCopyOEMInf is the call, not DiInstallDriver, though probably under the hood they do the same.
  • create a root-enumerated virtual device in the Device Manager. Look at the source of “devcon install” for this.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

wrote in message news:xxxxx@ntdev…
>I want to install a kmdf driver on Windows 7 32bit machine. At first I used hdwwiz (the add hardware tool) and it worked correctly.
>
> Now I want to install it programatically using a software. After some research I found DiInstallDriver I used the following code
>
> int _tmain(int argc, _TCHAR* argv)
> {
> std::wstring FilePath = L".inf";
> bool result = DiInstallDriver(NULL, FilePath.c_str(), DIIRFLAG_FORCE_INF, false);
> DWORD error = GetLastError();
> if (!result){
> printf(“The driver is not installed \r\n”);
> printf("The error is %x ", error);
> }else
> {
> printf(“The driver is installed correctly !”);
> }
>
> getchar();
> return 0;
> }
>
> After running the software I get “The driver is installed correctly !”, that means the functions returns with success but I cannot find it in the Device Manager?
>
> Any idea what is going wrong ?
>
>
>

Hey Tom thanks for your help.

What kind of driver? Is this a driver for a real piece of hardware, or
is it software-enumerated? The fact that you were using hdwwiz suggests
that you are creating your own fake device to drive. What is the
hardware ID in your INF file?

It is not hardware related. I am trying to make to implement a hook for the SSDT table in windows. Here is the article http://resources.infosecinstitute.com/hooking-system-service-dispatch-table-ssdt/
I am a beginner at this field. Here is the id {78A1C341-4539-11d3-B88D-00C04FAD5171}.

If it is a fake device, then that’s the problem. DiInstallDriver only
installs drivers for real hardware, or at least for devices that already
exist. You need to have someone create a new device for your driver to
drive. That’s what hdwwiz and “devcon install” both do. You can see
how in the devcon source, which is part of the WDK.

That seems a complicated work for me. Furthermore, when I use devcon install .inf I get devcon failed with no extra information about the root of the error. Is devcon supposed to work like hdwwiz ?

Hey Maxim, I appreciate your reply!

There are 2 separate steps:

  • install a driver package to the machine. Look at the source of “devcon
    dp_add” for this. IIRC SetupCopyOEMInf is the call, not DiInstallDriver, though
    probably under the hood they do the same.
  • create a root-enumerated virtual device in the Device Manager. Look at the
    source of “devcon install” for this.

So I have to replicate what devcon do. Can’t I use it directly from my software?

You can’t redistribute Devcon, but it is easy enough to pull out the code
you need to do the work. I’ve done what Max is suggesting for multiple
situations, though I used the DifxAPI’s for the install.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Tuesday, July 12, 2016 11:28 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Install KMDF programatically using DiInstallDriver

Hey Maxim, I appreciate your reply!

>There are 2 separate steps:

- install a driver package to the machine. Look at the source of “devcon
dp_add” for this. IIRC SetupCopyOEMInf is the call, not DiInstallDriver,
though probably under the hood they do the same.
- create a root-enumerated virtual device in the Device Manager. Look at
the source of “devcon install” for this.

So I have to replicate what devcon do. Can’t I use it directly from my
software?


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:>

Hey Don, thanks for your reply

You can’t redistribute Devcon, but it is easy enough to pull out the code
you need to do the work. I’ve done what Max is suggesting for multiple
situations, though I used the DifxAPI’s for the install.

Ok this is good to know. So you have already used the code to install a software related KMDF ?

Yes, take devcon and work out the procedure to install your device, then
basically use the devcon source as a template for the operations you need to
do to support this. I used SetupDiGetINFClass to get the information I
needed from the INF file, then SetupDiCreateDeviceInfo,
SetupDiSetDeviceRegistryProperty, SetupDiCallClassInstaller to create the
DevNode for the package to load onto.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Tuesday, July 12, 2016 11:43 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Install KMDF programatically using DiInstallDriver

Hey Don, thanks for your reply

> You can’t redistribute Devcon, but it is easy enough to pull out the code
you need to do the work. I’ve done what Max is suggesting for multiple
situations, though I used the DifxAPI’s for the install.

Ok this is good to know. So you have already used the code to install a
software related KMDF ?


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:>

xxxxx@gmail.com wrote:

Hey Tom thanks for your help.

Tim.

> If it is a fake device, then that’s the problem. DiInstallDriver only installs drivers for real hardware…

That seems a complicated work for me. Furthermore, when I use devcon install .inf I get devcon failed with no extra information about the root of the error. Is devcon supposed to work like hdwwiz ?

Do you have a [DefaultInstall] section? Or are you actually a PnP
driver package?

It may be you don’t really need a PnP driver with an INF file at all.
Just do an old-fashioned “legacy” driver and start it with the Service
Manager.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Hey Tim, sorry for misspelling your name.

Do you have a [DefaultInstall] section? Or are you actually a PnP
driver package?

It may be you don’t really need a PnP driver with an INF file at all.
Just do an old-fashioned “legacy” driver and start it with the Service
Manager.

My inf file doesn’t have [DefaultInstall] section. I don’t know its purpose, I just compiled the driver using visual studio.