📄 usbif.cpp
字号:
/************************************************************************
*
* Module: UsbIF.cpp
* Description: USB Interface for Cygnal USB development board
* Company: Cygnal Integrated Products
*
************************************************************************/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "UsbIF.h"
// standard constructor
CUsbIF::CUsbIF()
{
memset(m_DeviceName, 0, sizeof(m_DeviceName));
}
// destructor
CUsbIF::~CUsbIF()
{
}
// Initialize the GUID
void CUsbIF::SetGUID(GUID newGUID)
{
m_GUID = newGUID;
}
BOOL CUsbIF::OpenUsbDevice()
{
// Retrieve device list for GUID that has been specified.
HDEVINFO hDevInfoList = SetupDiGetClassDevs (&m_GUID, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfoList != NULL)
{
SP_DEVICE_INTERFACE_DATA deviceInfoData;
for (int MemberIndex = 0; MemberIndex < 127; MemberIndex++)
{
// Clear data structure
ZeroMemory(&deviceInfoData, sizeof(deviceInfoData));
deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
// retrieves a context structure for a device interface of a device information set.
if (SetupDiEnumDeviceInterfaces (hDevInfoList, 0, &m_GUID, MemberIndex, &deviceInfoData))
{
// Must get the detailed information in two steps
// First get the length of the detailed information and allocate the buffer
// retrieves detailed information about a specified device interface.
PSP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength, requiredLength;
predictedLength = requiredLength = 0;
SetupDiGetDeviceInterfaceDetail (
hDevInfoList,
&deviceInfoData,
NULL, // Not yet allocated
0, // Set output buffer length to zero
&requiredLength,// Find out memory requirement
NULL);
predictedLength = requiredLength;
functionClassDeviceData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc (predictedLength);
functionClassDeviceData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA did = {sizeof(SP_DEVINFO_DATA)};
// Second, get the detailed information
if ( SetupDiGetDeviceInterfaceDetail (
hDevInfoList,
&deviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
&did))
{
TCHAR fname[256];
if (!SetupDiGetDeviceRegistryProperty(hDevInfoList, &did, SPDRP_FRIENDLYNAME, NULL, (PBYTE) fname, sizeof(fname), NULL)
&& !SetupDiGetDeviceRegistryProperty(hDevInfoList, &did, SPDRP_DEVICEDESC, NULL, (PBYTE) fname, sizeof(fname), NULL))
_tcsncpy(fname, functionClassDeviceData->DevicePath, 256);
CString serialnumber = GetSerialNumber(functionClassDeviceData->DevicePath);
serialnumber = " Serial " + serialnumber;
serialnumber = fname + serialnumber;
CDeviceListEntry e(functionClassDeviceData->DevicePath, serialnumber);
free( functionClassDeviceData );
m_list.Add(e);
}
}
else
{
if ( GetLastError() == ERROR_NO_MORE_ITEMS )
break;
}
}
}
// SetupDiDestroyDeviceInfoList() destroys a device information set
// and frees all associated memory.
SetupDiDestroyDeviceInfoList (hDevInfoList);
return m_list.GetSize();
}
CString CUsbIF::GetSerialNumber(LPCTSTR DevicePath)
{
CString temp = DevicePath;
temp = temp.Left((temp.GetLength()) - (temp.ReverseFind('#')));
temp = temp.Right((temp.GetLength()) - (temp.ReverseFind('#')));
return temp;
}
HANDLE CUsbIF::open_file( char *filename)
{
HANDLE hFile;
if(filename)
{
strcat (m_DeviceName, "\\");
strcat (m_DeviceName, filename);
}
hFile = CreateFile( m_DeviceName,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
return hFile;
}
CDeviceListEntry::CDeviceListEntry(LPCTSTR linkname, LPCTSTR friendlyname)
{ // CDeviceListEntry::CDeviceListEntry
m_linkname = linkname;
m_friendlyname = friendlyname;
}
/*************************** EOF **************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -