test123423
test123423

Reputation: 91

How to Send IOCTL's to all drivers on windows in C

Can someone provide me with a sample C code that list´s all device Names that i can open with Createfile()? i always get error code 3 : path does not exist

sample code that doesnt works:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>
#include <regstr.h>
#include <devioctl.h>
#include <usb.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <usbprint.h>
#include <setupapi.h>
#include <devguid.h>
#include <wdmguid.h>

#pragma comment(lib, "Setupapi.lib")
int main(void){
    HDEVINFO deviceInfoList;
    deviceInfoList = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);

    if (deviceInfoList != INVALID_HANDLE_VALUE)
    {
        SP_DEVINFO_DATA deviceInfoData;
        deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
        for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoList, i, &deviceInfoData); i++)
        {
            LPTSTR buffer = NULL;
            DWORD buffersize = 0;
            while (!SetupDiGetDeviceInstanceIdA(deviceInfoList, &deviceInfoData, buffer, buffersize, &buffersize))
            {
                if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                {
                    if (buffer) delete buffer;
                    buffer = new TCHAR[buffersize];
                }
                else
                {
                    printf("%ls\n", "error");
                    break;
                }
            }
            HANDLE hFile = CreateFileA(buffer,
                GENERIC_READ,
                0,
                NULL,
                OPEN_EXISTING,
                0,
                NULL);

            if (hFile == INVALID_HANDLE_VALUE) {
                printf("InvalidHandle, error code: %d\n", GetLastError());
            }
            CloseHandle(hFile);

            printf("%s\n", buffer);
            if (buffer) { delete buffer; buffer = NULL; }
        }
    }
    getchar();
}

my Goal is to print all valid device Names and try to get a valid handle on it that i can later user for sending ioctl`s

thx

EDIT: ok abhineet so thats what i got now :

DWORD EnumerateDevices(){
    DWORD dwResult = 0;

    HDEVINFO hdev = SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);// DIGCF_ALLCLASSES
    /*HDEVINFO hdev =SetupDiGetClassDevs(NULL,
        0, // Enumerator
        0,
        DIGCF_PRESENT | DIGCF_ALLCLASSES); */
    if (INVALID_HANDLE_VALUE != hdev) {
        for (int idev = 0; idev < 100; idev++)
        {
            SP_DEVICE_INTERFACE_DATA did = { 0 };
            did.cbSize = sizeof(did);

            if (SetupDiEnumDeviceInterfaces(hdev, NULL, &GUID_DEVCLASS_BATTERY, idev, &did))
            {
                DWORD cbRequired = 0;

                SetupDiGetDeviceInterfaceDetail(hdev,
                    &did,
                    NULL,
                    0,
                    &cbRequired,
                    0);
                if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
                {
                    PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, cbRequired);
                    if (pdidd) {
                        pdidd->cbSize = sizeof(*pdidd);
                        if (SetupDiGetDeviceInterfaceDetail(hdev, &did, pdidd, cbRequired, &cbRequired, 0)) {
                            wprintf(L"%s\n", pdidd->DevicePath);
                            HANDLE hBattery = CreateFile(pdidd->DevicePath,
                                GENERIC_READ | GENERIC_WRITE,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                FILE_ATTRIBUTE_NORMAL,
                                NULL);
                            if (INVALID_HANDLE_VALUE != hBattery)
                            {
                                printf("Successfully opened Handle\n");
                                CloseHandle(hBattery);
                            }
                            else{
                                wprintf(L"CreateFile(%s) failed %d\n", pdidd->DevicePath, GetLastError());
                            }
                        }
                        else{
                            printf("SetupDiGetDeviceInterfaceDetail() failed %d\n", GetLastError());
                        }
                        LocalFree(pdidd);
                    }
                }
                else{
                    printf("SetupDiGetDeviceInterfaceDetail() failed %d\n", GetLastError());
                }
            }
            else  if (ERROR_NO_MORE_ITEMS == GetLastError())
            {
                printf("-NoMoreItems-");
                break;  // Enumeration failed - perhaps we're out of items
            }
        }
        SetupDiDestroyDeviceInfoList(hdev);
    }
    else{
        printf("SetupDiGetClassDevs() failed %d\n", GetLastError());
    }
    return dwResult;
}

i ripped the most from here : https://msdn.microsoft.com/en-us/library/windows/desktop/bb204769(v=vs.85).aspx

and my Output is :

\\?\acpi#pnp0c0a#1#{72631e54-78a4-11d0-bcf7-00aa00b7b32a}
Successfully opened Handle
-NoMoreItems-

at least i got a valid handle! so i wanna do this an all devices avaible on the System , how to do that?

Upvotes: 2

Views: 1129

Answers (1)

Abhineet
Abhineet

Reputation: 5389

IMHO, I don't think, you can do a CreateFile on InstanceID. To do a CreateFile, you need the symbolic name of the device. You can use the following SetupAPIs,

The Remark section of both APIs state that,

SetupDiEnumDeviceInterfaces:: DeviceInterfaceData points to a structure that identifies a requested device interface. To get detailed information about an interface, call SetupDiGetDeviceInterfaceDetail. The detailed information includes the name of the device interface that can be passed to a Win32 function such as CreateFile (described in Microsoft Windows SDK documentation) to get a handle to the interface.

SetupDiGetDeviceInterfaceDetail:: The interface detail returned by this function consists of a device path that can be passed to Win32 functions such as CreateFile. Do not attempt to parse the device path symbolic name. The device path can be reused across system starts.

This might be of your use, how to get DevicePath from DeviceID

Upvotes: 1

Related Questions