이 부분은 간단한 오류입니다. 따라서 "쉬지 않고"라는 말을 주저하지 마십시오.XCode strcpy (char * dest, io_name_t src)가 dest에 복사하지 않습니다.
여기 내 코드입니다. 변수가 io_name_t
에서 strcpy로 변하기 때문에 데이터가 복사되지 않습니다.
static struct sUSBDevInfo {
char devName[200];
char svcPlane[200];
char othPlane[200];
int isScreen;
} USBDevInfo[99];
지금 구조체 채우기 :
더 사용하기 위해 데이터를 저장하는 구조체를 정의 나는 그것이deviceName
이 값을 예상했다고 보여줍니다 디버깅 할 때
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);
}
(예 deviceName:Root Hub Simulation Simulation
)하지만, strcpy를 USBDevInfo[i].devName
으로 변경하면 목적지는 변경되지 않습니다 ('\0'
).
편집.
시도해 보았습니다. 다른 지역 변수 작품을 복사하지만, 글로벌 정적 구조체의 항목 번호 :
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 */
편집 2
아래의 답변을 참조하지 않습니다.
"편집 2"변경 사항이 적용되는 경우 지금도 질문이 있습니까? –
@Michael - 예, 지금 작동 중입니다. – TenG