Generate a C code that simulate usb device on windows, with possible adjusments iManufacturer and iProduct variables. It's should be builded statically also, no need description just C code and gcc command.
Assistant
Here is an example C code that simulates a USB device on Windows, with adjustable iManufacturer and iProduct variables:
c
#include <windows.h>
#include <usb.h>
#define DEVICE_INTERFACE 0x01
#define DEVICE_ENDPOINT 0x81
int main() {
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
HANDLE hDevice = INVALID_HANDLE_VALUE;
USB_DEVICE_DESCRIPTOR usbDeviceDescriptor;
USB_CONFIG_DESCRIPTOR usbConfigDescriptor;
USB_INTERFACE_DESCRIPTOR usbInterfaceDescriptor;
USB_ENDPOINT_DESCRIPTOR usbEndpointDescriptor;
DWORD dwRetVal;
char szDevicePath[MAX_PATH];
// Initialize the USB device
hDevInfo = SetupDiGetClassDevsEx(
&GUID_DEVINTERFACE_USB_DEVICE,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_ALLCLASSES,
NULL,
NULL,
NULL
);
if (hDevInfo == INVALID_HANDLE_VALUE) {
return 1;
}
// Find the device
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
dwRetVal = SetupDiEnumDeviceInfo(hDevInfo, 0, &DeviceInfoData);
if (dwRetVal == 0) {
return 1;
}
// Get the device path
SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, szDevicePath, sizeof(szDevicePath), NULL);
printf("Device path: %s\n", szDevicePath);
// Open the device
hDevice = CreateFile(szDevicePath,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (hDevice == INVALID_HANDLE_VALUE) {
return 1;
}
// Read the device descriptor
dwRetVal = ReadFile(hDevice, &usbDeviceDescriptor, sizeof(USB_DEVICE_DESCRIPTOR), &dwRetVal, NULL);
if (dwRetVal != sizeof(USB_DEVICE_DESCRIPTOR)) {
return 1;
}
// Read the configuration descriptor
dwRetVal = ReadFile(hDevice, &usbConfigDescriptor, sizeof(USB_CONFIG_DESCRIPTOR), &dwRetVal, NULL);
if (dwRetVal != sizeof(USB_CONFIG_DESCRIPTOR)) {
return 1;
}
// Read the interface descriptor
dwRetVal = ReadFile(hDevice, &usbInterfaceDescriptor, sizeof(USB_INTERFACE_DESCRIPTOR), &dwRetVal, NULL);
if (dwRetVal != sizeof(USB_INTERFACE_DESCRIPTOR)) {
return 1;
}
// Read the endpoint descriptor
dwRetVal = ReadFile(hDevice, &usbEndpointDescriptor, sizeof(USB_ENDPOINT_DESCRIPTOR), &dwRetVal, NULL);
if (dwRetVal != sizeof(USB_ENDPOINT_DESCRIPTOR)) {
return 1;
}
// Set the device manufacturer and product strings
usbDeviceDescriptor.iManufacturer = 0x0200; // Change this to adjust the manufacturer string
usbDeviceDescriptor.iProduct = 0x0300; // Change this to adjust the product string
// Set the device speed
usbDeviceDescriptor.bcdUSB = 0x0200;
// Write the device descriptor
dwRetVal = WriteFile(hDevice, &usbDeviceDescriptor, sizeof(USB_DEVICE_DESCRIPTOR), &dwRetVal, NULL);
if (dwRetVal != sizeof(USB_DEVICE_DESCRIPTOR)) {
return 1;
}
// Close the device
CloseHandle(hDevice);
return 0;
}
You can compile this code using the following command:
Note that this code is a simple example and may not work as-is on your system. You may need to adjust the device path, device descriptor, and other values to match your specific USB device. Additionally, this code does not handle errors and exceptions properly, so you should add error handling and other safety features to make it more robust.