相同代码在MFC中不报错,在控制台工程中就报错
废话不多说直接看代码
库的头文件,问题就在GetUSBConfig这个函数的返回类型
class CCyUSBDevice
{
// The public members are accessible (i.e. corruptible) by the user of the library
// Algorithms of the class don't rely on any public members. Instead, they use the
// private members of the class for their calculations.
public:
CCyUSBDevice(HANDLE hnd = NULL, GUID guid = CYUSBDRV_GUID, BOOL bOpen = true);
~CCyUSBDevice(void);
CCyUSBEndPoint **EndPoints; // Shortcut to USBCfgs[CfgNum]->Interfaces[IntfcIndex]->Endpoints
CCyUSBEndPoint *EndPointOf(UCHAR addr);
CCyControlEndPoint *ControlEndPt;
CCyIsocEndPoint *IsocInEndPt;
CCyIsocEndPoint *IsocOutEndPt;
CCyBulkEndPoint *BulkInEndPt;
CCyBulkEndPoint *BulkOutEndPt;
CCyInterruptEndPoint *InterruptInEndPt;
CCyInterruptEndPoint *InterruptOutEndPt;
USHORT StrLangID;
ULONG UsbdStatus;
ULONG NtStatus;
ULONG DriverVersion;
ULONG USBDIVersion;
char DeviceName[USB_STRING_MAXLEN];
char FriendlyName[USB_STRING_MAXLEN];
wchar_t Manufacturer[USB_STRING_MAXLEN];
wchar_t Product[USB_STRING_MAXLEN];
wchar_t SerialNumber[USB_STRING_MAXLEN];
CHAR DevPath[USB_STRING_MAXLEN];
USHORT BcdUSB;
USHORT VendorID;
USHORT ProductID;
UCHAR USBAddress;
UCHAR DevClass;
UCHAR DevSubClass;
UCHAR DevProtocol;
UCHAR MaxPacketSize;
USHORT BcdDevice;
UCHAR ConfigValue;
UCHAR ConfigAttrib;
UCHAR MaxPower;
UCHAR IntfcClass;
UCHAR IntfcSubClass;
UCHAR IntfcProtocol;
bool bHighSpeed;
DWORD BytesXfered;
UCHAR DeviceCount(void);
UCHAR ConfigCount(void);
UCHAR IntfcCount(void);
UCHAR AltIntfcCount(void);
UCHAR EndPointCount(void);
UCHAR Config(void) { return CfgNum; } // Normally 0
void SetConfig(UCHAR cfg);
UCHAR Interface(void) { return IntfcNum; } // Usually 0
// No SetInterface method since only 1 intfc per device (per Windows)
UCHAR AltIntfc(void);
bool SetAltIntfc(UCHAR alt);
GUID DriverGUID(void) { return DrvGuid; }
HANDLE DeviceHandle(void) { return hDevice; }
void UsbdStatusString(ULONG stat, PCHAR s);
bool CreateHandle(UCHAR dev);
void DestroyHandle();
bool Open(UCHAR dev);
void Close(void);
bool Reset(void);
bool ReConnect(void);
bool Suspend(void);
bool Resume(void);
bool IsOpen(void) { return (hDevice != INVALID_HANDLE_VALUE); }
UCHAR PowerState(void);
void GetDeviceDescriptor(PUSB_DEVICE_DESCRIPTOR descr);
void GetConfigDescriptor(PUSB_CONFIGURATION_DESCRIPTOR descr);
void GetIntfcDescriptor(PUSB_INTERFACE_DESCRIPTOR descr);
CCyUSBConfig GetUSBConfig(int index);
private:
USB_DEVICE_DESCRIPTOR USBDeviceDescriptor;
PUSB_CONFIGURATION_DESCRIPTOR USBConfigDescriptors[2];
CCyUSBConfig *USBCfgs[2];
HANDLE hWnd;
HANDLE hDevice;
HANDLE hDevNotification;
HANDLE hHndNotification;
GUID DrvGuid;
UCHAR Devices;
UCHAR Interfaces;
UCHAR AltInterfaces;
UCHAR Configs;
UCHAR DevNum;
UCHAR CfgNum;
UCHAR IntfcNum; // The current selected interface's bInterfaceNumber
UCHAR IntfcIndex; // The entry in the Config's interfaces table matching to IntfcNum and AltSetting
void GetDevDescriptor(void);
void GetCfgDescriptor(int descIndex);
void GetString(wchar_t *s, UCHAR sIndex);
void SetStringDescrLanguage(void);
void SetAltIntfcParams(UCHAR alt);
bool IoControl(ULONG cmd, PUCHAR buf, ULONG len);
void SetEndPointPtrs(void);
void GetDeviceName(void);
void GetFriendlyName(void);
void GetDriverVer(void);
void GetUSBDIVer(void);
void GetSpeed(void);
void GetUSBAddress(void);
//void CloseEndPtHandles(void);
bool RegisterForPnpEvents(HANDLE h);
};
CCyUSBConfig类
class CCyUSBConfig
{
private:
protected:
public:
CCyUSBInterface *Interfaces[MAX_INTERFACES];
UCHAR bLength;
UCHAR bDescriptorType;
USHORT wTotalLength;
UCHAR bNumInterfaces;
UCHAR bConfigurationValue;
UCHAR iConfiguration;
UCHAR bmAttributes;
UCHAR MaxPower;
UCHAR AltInterfaces;
CCyUSBConfig(void);
CCyUSBConfig(CCyUSBConfig& cfg); // Copy Constructor
CCyUSBConfig(HANDLE h, PUSB_CONFIGURATION_DESCRIPTOR pConfigDescr);
~CCyUSBConfig(void);
};
使用的代码
CCyUSBDevice* USBDevice = new CCyUSBDevice(NULL);
char buf[512];
std::string s;
for (int c = 0; c < USBDevice->ConfigCount(); c++)
{
CCyUSBConfig cfg = USBDevice->GetUSBConfig(c);
sprintf_s(buf, "bLength: 0x%x\n", cfg.bLength); s.append(buf);
sprintf_s(buf, "bDescriptorType: %d\n", cfg.bDescriptorType); s.append(buf);
sprintf_s(buf, "wTotalLength: %d (0x%x)\n", cfg.wTotalLength, cfg.wTotalLength);
s.append(buf);
sprintf_s(buf, "bNumInterfaces: %d\n", cfg.bNumInterfaces); s.append(buf);
sprintf_s(buf, "bConfigurationValue: %d\n", cfg.bConfigurationValue); s.append(buf);
sprintf_s(buf, "iConfiguration: %d\n", cfg.iConfiguration); s.append(buf);
sprintf_s(buf, "bmAttributes: 0x%x\n", cfg.bmAttributes); s.append(buf);
sprintf_s(buf, "MaxPower: %d\n", cfg.MaxPower); s.append(buf);
s.append("**********************************\n");
std::cout << s;
s.clear();
for (int i = 0; i < cfg.AltInterfaces; i++)
{
CCyUSBInterface* ifc = cfg.Interfaces[i];
sprintf_s(buf, "Interface Descriptor:%d\n", (i + 1)); s.append(buf);
sprintf_s(buf, "--------------------------------\n"); s.append(buf);
sprintf_s(buf, "bLength: 0x%x\n", ifc->bLength); s.append(buf);
sprintf_s(buf, "bDescriptorType: %d\n", ifc->bDescriptorType); s.append(buf);
sprintf_s(buf, "bInterfaceNumber: %d\n", ifc->bInterfaceNumber); s.append(buf);
sprintf_s(buf, "bAlternateSetting: %d\n", ifc->bAlternateSetting); s.append(buf);
sprintf_s(buf, "bNumEndpoints: %d\n", ifc->bNumEndpoints); s.append(buf);
sprintf_s(buf, "bInterfaceClass: %d\n", ifc->bInterfaceClass); s.append(buf);
sprintf_s(buf, "**********************************\n"); s.append(buf);
std::cout << s;
s.clear();
for (int e = 0; e < ifc->bNumEndpoints; e++)
{
CCyUSBEndPoint* ept = ifc->EndPoints[e + 1];
sprintf_s(buf, "EndPoint Descriptor:%d\n", (e + 1)); s.append(buf);
sprintf_s(buf, "--------------------------------\n"); s.append(buf);
sprintf_s(buf, "bLength: 0x%x\n", ept->DscLen); s.append(buf);
sprintf_s(buf, "bDescriptorType: %d\n", ept->DscType); s.append(buf);
sprintf_s(buf, "bEndpointAddress: 0x%x\n", ept->Address); s.append(buf);
sprintf_s(buf, "bmAttributes: 0x%x\n", ept->Attributes); s.append(buf);
sprintf_s(buf, "wMaxPacketSize: %d\n", ept->MaxPktSize); s.append(buf);
sprintf_s(buf, "bInterval: %d\n", ept->Interval); s.append(buf);
s.append("**********************************\n");
std::cout << s;
s.clear();
}
}
}
报错点,控制台工程中报错

MFC中就不报错

问题其实很简单,就是CCyUSBConfig类没有重写operator=,但是为什么MFC工程中就不报错呢?