⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbdrv.cpp

📁 全是好东西啊 欢迎观看,共勉,加油啊
💻 CPP
字号:
#include "stdafx.h"

#include <setupapi.h>
#include <basetyps.h>

//#include "usb.h"

DEFINE_GUID(GUID_DFU_BOOSTER, 
0x93c7ae0a, 0xf32d, 0x4061, 0x80, 0x6f, 0x22, 0x81, 0x84, 0xb7, 0xbc, 0x67);

#define NOISY(_x_) MessageBox(NULL, _x_ , "ERROR", MB_OK);

HANDLE openFile(IN LPGUID pGuid, IN PCHAR pFileName)
{
	HANDLE hOut = INVALID_HANDLE_VALUE; /* Resulting Handle */

	HDEVINFO                         hardwareDeviceInfo;
	SP_INTERFACE_DEVICE_DATA         deviceInfoData;
	PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetailData;

	PCHAR completeFileName;
	DWORD nbBytes; /* Nb of bytes to allocate for deviceDetailData */

	/* Open Device information */
	hardwareDeviceInfo = SetupDiGetClassDevs(
                           pGuid,
                           NULL, // Define no enumerator (global)
                           NULL, // Define no
                           (DIGCF_PRESENT | // Only Devices present
                            DIGCF_INTERFACEDEVICE)); // Function class devices.

	if (hardwareDeviceInfo == INVALID_HANDLE_VALUE) {
		NOISY("Failed to open USB");
		return hOut;
	}

	/* Perform enumeration */
	deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);

	if (!SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,
                          NULL, // We don't care about specific PDOs
						  pGuid,
                          0,
                          &deviceInfoData)) {
       	NOISY("Can not open interface");
        return hOut;
	}


	/* Get detailed information about the device */
	SetupDiGetInterfaceDeviceDetail(hardwareDeviceInfo, &deviceInfoData, NULL, 0, &nbBytes, NULL);
	deviceDetailData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (nbBytes);
	if (!deviceDetailData) {
		NOISY("Failed to get memory for interface detail");
		SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
		return hOut;
	}
	deviceDetailData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);
	if (!SetupDiGetInterfaceDeviceDetail(
		hardwareDeviceInfo, &deviceInfoData, deviceDetailData, nbBytes, NULL, NULL)) {
		NOISY("Failed getting interface detail");
		free(deviceDetailData);
		SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
		return hOut;
	}

	/* Build the completeFileName */
	if (pFileName)
		completeFileName = (PCHAR) malloc(strlen(deviceDetailData->DevicePath) + strlen(pFileName) + 2);
	else
		completeFileName = (PCHAR) malloc(strlen(deviceDetailData->DevicePath) + 1);
	if (!completeFileName) {
		NOISY("Failed to get memory for completeFileName");
		free(deviceDetailData);
		SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
		return hOut;
	}
	if (pFileName)
		sprintf(completeFileName, "%s\\%s", deviceDetailData->DevicePath, pFileName);
	else
		strcpy(completeFileName, deviceDetailData->DevicePath);

	/* Open file */
	hOut =  CreateFile (
                  completeFileName,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL, // no SECURITY_ATTRIBUTES structure
                  OPEN_EXISTING, // No special create flags
                  0, // No special attributes
                  NULL); // No template file

	/* Free ressources */
	free(completeFileName);
	free(deviceDetailData);
	SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
	return hOut;
}


ULONG Usb_Download(BYTE * const buffer, ULONG bufferSize)
{
	HANDLE pFile = openFile((LPGUID) &GUID_DFU_BOOSTER, NULL);
	ULONG   bytesWritten = 0;

	if (pFile == INVALID_HANDLE_VALUE) {
		MessageBox(NULL, "Can not open USB device", "ERROR", MB_OK);
		return 0;
	}

	if ( !WriteFile(pFile, buffer, bufferSize, &bytesWritten, NULL) )
		MessageBox(NULL, "Failed to download code", "ERROR", MB_OK);

	CloseHandle( pFile );
	return bufferSize;
}

ULONG Usb_Upload(BYTE *buffer, ULONG bufferSize)
{
	HANDLE pFile = openFile((LPGUID) &GUID_DFU_BOOSTER, NULL);
	ULONG   nbBytesRead;

	if (pFile == INVALID_HANDLE_VALUE) {
		MessageBox(NULL, "Can not open USB device", "ERROR", MB_OK);
		return 0;
	}

	if ( !ReadFile(pFile, buffer, bufferSize, &nbBytesRead, NULL) )
		MessageBox(NULL, "Failed to upload code", "ERROR", MB_OK);

	CloseHandle( pFile );
	return nbBytesRead;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -