This has to be a simple error on my part, hence why I use the term "not working" with hesitation.
Here is my code, the problem is the strcpy from io_name_t
variable to char
array seems to not work, i.e. the data is not copied.
Define the struct to hold the data for further use:
static struct sUSBDevInfo {
char devName[200];
char svcPlane[200];
char othPlane[200];
int isScreen;
} USBDevInfo[99];
Now populate the struct:
int getUSBDevices ()
{
int i = -1;
CFMutableDictionaryRef matchingDict;
io_iterator_t iter;
kern_return_t kr;
io_service_t device;
io_name_t deviceName;
io_string_t pathName;
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if ( matchingDict == NULL)
{
return ( "No match" );
}
/* Now we have a dictionary, get an iterator.*/
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr != KERN_SUCCESS)
{
return (0);
}
/* iterate */
while ((device = IOIteratorNext(iter)))
{
/* do something with device, eg. check properties */
/***Display the device names ***/
kr = IORegistryEntryGetName(device, deviceName);
if (KERN_SUCCESS != kr)
{
deviceName[0] = '\0';
}
i++;
printf("\ndeviceName:%s",deviceName);
strcpy(USBDevInfo[i].devName, deviceName); /* !!! This and the following strcpy leave the dest variable unchanged !!! */
IORegistryEntryGetPath(device, kIOServicePlane, pathName);
strcpy(USBDevInfo[i].svcPlane, pathName);
IORegistryEntryGetPath(device, kIOUSBPlane, pathName);
strcpy(USBDevInfo[i].othPlane, pathName);
USBDevInfo[i].isScreen = isScreen(deviceName);
IOObjectRelease(device);
}
/* Done, release the iterator */
IOObjectRelease(iter);
return ( i );
}
When I debug it shows that deviceName
has expected values (e.g. deviceName:Root Hub Simulation Simulation
) , but the strcpy to USBDevInfo[i].devName
leaves the destination unchanged ('\0'
).
Edit.
Tried experimenting. Copy to another local variable works, but no to the global static struct item:
char cpy[200]; /* local variable */
.
.
strcpy(cpy, deviceName); /* Works - copied to cpy */
strcpy(USBDevInfo[i].devName, deviceName); /* Not working */
USBDevInfo[i].devName[0] = deviceName[3]; /* Not working */
Edit 2
See the answer below.