2 bai030811 bai030811 于 2016.01.28 12:48 提问

求大神帮忙! WriteFile 写入长度只能为9 什么7啊 8啊 10啊都报错!

之前下资料币用完了 ! 好人全家幸福 一生平安
DevHID::DevHID(WORD vid, WORD pid)
{
if (GetDevicePath(vid, pid))
{
writeHandle = CreateFile(devDetail->DevicePath,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);

    if (writeHandle == INVALID_HANDLE_VALUE)
    {
        cout << "创建写句柄失败!" << endl;
    }

    readHandle = CreateFile(devDetail->DevicePath,
        GENERIC_WRITE | GENERIC_READ,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
        NULL);

    if (readHandle == INVALID_HANDLE_VALUE)
    {
        cout << "创建读句柄失败!" << endl;
    }
}
else
{
    cout << "获取设备路径失败!" << endl;
}

}

BOOL DevHID::GetDevicePath(DWORD VID, DWORD PID)
{
HDEVINFO hDevInfo; // 设备信息结构
GUID hidGuid; // 设备接口类

ULONG  requiredLength;        //  指定接受缓冲区大小;  

//  设备句柄
HANDLE hidHandle;


//  用于接受VID PID
_HIDD_ATTRIBUTES hidAttributes;

//  获取符合条件的设备信息
HidD_GetHidGuid(&hidGuid);

//  获得信息
hDevInfo = SetupDiGetClassDevs(
    &hidGuid,
    NULL,
    NULL,
    (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));

if (hDevInfo == INVALID_HANDLE_VALUE)
{
    return FALSE;  //  错误返回
}

SP_DEVICE_INTERFACE_DATA devInfoData;       //  设备信息集合
devInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

for (int deviceNo = 0; deviceNo < 20; deviceNo++)
{
    //  枚举设备
    if (!SetupDiEnumInterfaceDevice(
        hDevInfo,
        0,
        &hidGuid,
        deviceNo,
        &devInfoData))
    {
        //  错误返回
        SetupDiDestroyDeviceInfoList(hDevInfo);
        return FALSE; 
    }

    //   获得缓冲区大小
    SetupDiGetInterfaceDeviceDetail(hDevInfo,
        &devInfoData,
        NULL,
        0,
        &requiredLength, // 接受缓冲区大小
        NULL);

    //  根据返回的信息分配缓冲区大小
    devDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredLength);

    //  指定大小
    devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);


    //  填充设备路径信息缓冲区
    if (!SetupDiGetInterfaceDeviceDetail(hDevInfo,
        &devInfoData,
        devDetail,
        requiredLength,
        NULL,
        NULL))
    {
        SetupDiDestroyDeviceInfoList(hDevInfo);
        free(devDetail);
        return FALSE;
    }

    //  打开设备
    hidHandle = CreateFile(devDetail->DevicePath,
        NULL,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    if (hidHandle == INVALID_HANDLE_VALUE)
    {
        // 失败
        SetupDiDestroyDeviceInfoList(hDevInfo);
        CloseHandle(hidHandle);
        free(devDetail);
        return FALSE;
    }

    if (!HidD_GetAttributes(hidHandle, &hidAttributes))
    {
        //  获得失败
        SetupDiDestroyDeviceInfoList(hDevInfo);
        CloseHandle(hidHandle);
        free(devDetail);
        return FALSE;
    }

    if ((VID == hidAttributes.VendorID  && 
        PID == hidAttributes.ProductID))
    {
        //  匹配并退出
        SetupDiDestroyDeviceInfoList(hDevInfo);
        CloseHandle(hidHandle);
        return TRUE;
    }
}
SetupDiDestroyDeviceInfoList(hDevInfo);
CloseHandle(hidHandle);
return FALSE;

}


OVERLAPPED WriteOverlapped;
WriteOverlapped.Offset = 0;
WriteOverlapped.OffsetHigh = 0;

WriteOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);


char WriteReportBuffer[64];

WriteReportBuffer[0] = 0x00;    
WriteReportBuffer[1] = 0x10;
WriteReportBuffer[2] = 0x20;
WriteReportBuffer[3] = 0x30;
WriteReportBuffer[4] = 0x40;
WriteReportBuffer[5] = 0x30;
WriteReportBuffer[6] = 0x40;


WriteFile(writeHandle,
    WriteReportBuffer,
    9,
    NULL,
    &WriteOverlapped);
cout << "CODE:" << GetLastError() << endl;

switch (WaitForSingleObject(WriteOverlapped.hEvent, 2000))
{
case WAIT_OBJECT_0:
    cout << " 函数返回成功!" << endl;
    return;
case WAIT_TIMEOUT:
    cout << " 间隔时间到!" << endl;
    return;
case WAIT_FAILED:
    cout << "WaitForSingleObject 失败!" << endl;
    return;
default:
    cout << "什么也没有做!" << endl;
}

1个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.01.28 13:38

你的方法调用没有问题,第三个参数表示需要将缓冲区中多少字节的数据写入问题,只要这个值不超过缓冲区的长度,应该是没有问题的。
你调用不是9时报的错误信息是什么呢?

bai030811
bai030811 传9以外的数字都是报 1784 这个错误 说是 〖1784〗-提供给请求操作的用户缓冲区无效。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!