武安和CharSample样例实现

开发环境:vs2019 + 虚拟机WIN10 Professional 1903.
1.驱动程序。
主要移植消息队列CharSampleEvtIoDeviceControl部分。

2.测试程序:


#include <iostream>

#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <winioctl.h>

#include "public.h"
#pragma comment (lib, "setupapi.lib")

PCHAR
GetDevicePath(
    IN  LPGUID InterfaceGuid
    );


wchar_t* char2wchar(const char* cchar)
{
    wchar_t* m_wchar;
    size_t len = MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), NULL, 0);
    m_wchar = new wchar_t[len + 1];
    MultiByteToWideChar(CP_ACP, 0, cchar, strlen(cchar), m_wchar, len);
    m_wchar[len] = '\0';
    return m_wchar;
}
char* wchar2char(const wchar_t* wchar)
{
    char* m_char;
    size_t len = WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), NULL, 0, NULL, NULL);
    m_char = new char[len + 1];
    WideCharToMultiByte(CP_ACP, 0, wchar, wcslen(wchar), m_char, len, NULL, NULL);
    m_char[len] = '\0';
    return m_char;
}



int main(int argc, char* argv[])
{
    PCHAR  DevicePath;
    HANDLE hDevice = INVALID_HANDLE_VALUE;

    printf("Application Test_CharSample starting...\n");

    DevicePath = GetDevicePath((LPGUID)&CharSample_DEVINTERFACE_GUID);

    hDevice = CreateFile(char2wchar(DevicePath),
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if (hDevice == INVALID_HANDLE_VALUE) {
        printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError());
        return 0;
    }

    printf("OK.\n");

    CHAR	bufInput[1];	// Input to device
    CHAR	bufOutput[2];	// Output from device
    ULONG	nOutput;	// Count written to bufOutput

    printf("请输入数字(0-9)\n");
l0:	bufInput[0] = _getch();
    if ((bufInput[0] < '0') || (bufInput[0] > '9')) goto l0;
    _putch(bufInput[0]);

    // Call device IO Control interface (CharSample_IOCTL_800) in driver
    if (!DeviceIoControl(hDevice,
        CharSample_IOCTL_800,
        bufInput,
        1,
        bufOutput,
        2,
        &nOutput,
        NULL)
        )
    {
        printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
        goto exit;
    }
    printf(":");
   // _putch(bufOutput[0]);
   // _putch(bufOutput[1]);
    printf("%c%c", bufOutput[0], bufOutput[1]);
    printf("\n");

exit:

    if (hDevice != INVALID_HANDLE_VALUE) {
        CloseHandle(hDevice);
    }
    return 0;
}

PCHAR
GetDevicePath(
    IN  LPGUID InterfaceGuid
    )
{
    HDEVINFO HardwareDeviceInfo;
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData = NULL;
    ULONG Length, RequiredLength = 0;
    BOOL bResult;

    HardwareDeviceInfo = SetupDiGetClassDevs(
        InterfaceGuid,
        NULL,
        NULL,
        (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));

    if (HardwareDeviceInfo == INVALID_HANDLE_VALUE) {
        printf("SetupDiGetClassDevs failed!\n");
        exit(1);
    }

    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    bResult = SetupDiEnumDeviceInterfaces(HardwareDeviceInfo,
        0,
        InterfaceGuid,
        0,
        &DeviceInterfaceData);

    if (bResult == FALSE) {
        /*
                LPVOID lpMsgBuf;

                if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                  FORMAT_MESSAGE_FROM_SYSTEM |
                                  FORMAT_MESSAGE_IGNORE_INSERTS,
                                  NULL,
                                  GetLastError(),
                                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                  (LPSTR) &lpMsgBuf,
                                  0,
                                  NULL
                                  )) {

                    printf("Error: %s", (LPSTR)lpMsgBuf);
                    LocalFree(lpMsgBuf);
                }
        */
        printf("SetupDiEnumDeviceInterfaces failed.\n");

        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        exit(1);
    }

    SetupDiGetDeviceInterfaceDetail(
        HardwareDeviceInfo,
        &DeviceInterfaceData,
        NULL,
        0,
        &RequiredLength,
        NULL
        );

    DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, RequiredLength);

    if (DeviceInterfaceDetailData == NULL) {
        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

    Length = RequiredLength;

    bResult = SetupDiGetDeviceInterfaceDetail(
        HardwareDeviceInfo,
        &DeviceInterfaceData,
        DeviceInterfaceDetailData,
        Length,
        &RequiredLength,
        NULL);

    if (bResult == FALSE) {
        /*
                LPVOID lpMsgBuf;

                if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                              FORMAT_MESSAGE_FROM_SYSTEM |
                              FORMAT_MESSAGE_IGNORE_INSERTS,
                              NULL,
                              GetLastError(),
                              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                              (LPSTR) &lpMsgBuf,
                              0,
                              NULL
                              )) {

                    MessageBox(NULL, (LPCTSTR) lpMsgBuf, "Error", MB_OK);
                    LocalFree(lpMsgBuf);
                }
        */
        printf("Error in SetupDiGetDeviceInterfaceDetail\n");

        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        LocalFree(DeviceInterfaceDetailData);
        exit(1);
    }

    return wchar2char(DeviceInterfaceDetailData->DevicePath);

}

结果 :
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值