Цитата(juvf @ Jul 22 2013, 13:04)

Как программно перебрать все порты в системе и найти тот компорт, у которого "id" == MyDevice ?
ps для Windows и для Linux.
У меня под виндами так:
Код
typedef struct _SERIAL_LIST SERIAL_LIST;
struct _SERIAL_LIST
{
char* strName;
char* strDescription;
};
//---------------------------------------------------------------------------
unsigned long WINAPI __export Serial_GetPortList (SERIAL_LIST** ppPortList)
{
unsigned long ulPortListCount;
HKEY hkKey;
unsigned long ulIndex;
char* strValueName;
unsigned long ulValueNameSize;
unsigned long ulType;
char* strData;
unsigned long ulDataSize;
*ppPortList = NULL;
if (RegOpenKey(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", &hkKey) == ERROR_SUCCESS)
{
ulPortListCount = 0;
*ppPortList = (SERIAL_LIST*)malloc(sizeof(SERIAL_LIST));
(*ppPortList)->strName = NULL;
(*ppPortList)->strDescription = NULL;
ulIndex=0;
ulValueNameSize = 255;
strValueName = (char*)malloc(ulValueNameSize+1);
ulDataSize = 255;
strData = (char*)malloc(ulDataSize+1);
while (RegEnumValue(hkKey, ulIndex, strValueName, &ulValueNameSize, NULL, &ulType, strData, &ulDataSize) == ERROR_SUCCESS)
{
if (ulType == REG_SZ)
if (ulDataSize >= 4)
if (memcmp(strData, "COM", 3) == 0)
{
*ppPortList = (SERIAL_LIST*)realloc(*ppPortList, sizeof(SERIAL_LIST)*(ulPortListCount+2));
((*ppPortList)+ulPortListCount)->strName = (char*)malloc(strlen(strData)+1);
((*ppPortList)+ulPortListCount)->strDescription = (char*)malloc(strlen(strValueName)+1);
strcpy(((*ppPortList)+ulPortListCount)->strName, strData);
strcpy(((*ppPortList)+ulPortListCount)->strDescription, strValueName);
ulPortListCount++;
((*ppPortList)+ulPortListCount)->strName = NULL;
((*ppPortList)+ulPortListCount)->strDescription = NULL;
}
ulIndex++;
ulValueNameSize = 255;
ulDataSize = 255;
}
free(strValueName);
free(strData);
RegCloseKey(hkKey);
}
if (!ulPortListCount)
{
Serial_FreePortList(*ppPortList);
*ppPortList = NULL;
}
return ulPortListCount;
}
//---------------------------------------------------------------------------
void WINAPI __export Serial_FreePortList (SERIAL_LIST* pPortList)
{
SERIAL_LIST *pPort;
if (pPortList)
{
for (pPort=pPortList;pPort->strName || pPort->strDescription;pPort++)
{
free (pPort->strName);
free (pPort->strDescription);
}
free (pPortList);
}
}
//---------------------------------------------------------------------------