因一产品的串口上同时接着IRDA和外接串口,默认功能为IRDA。启动之后如果测试外接串口,就不能打开设备端口,所以写了一段代码先Deinit此设备再加载,这样IRDA使用的设备句柄已经无效,IRDA功能不再有效。但外接串口可以使用,代码如下:
pcsDevName为设备名,如果COM2:pcsDevKey为设备的注册表,两者给其一即可。
void ReLoadDevice2Test( TCHAR * pcsDevName, TCHAR * pcsDevKey )
{
HKEY hKey=NULL;
TCHAR csKeyName[MAX_PATH];
TCHAR csDevice[MAX_PATH];
TCHAR csName[MAX_PATH];
int i;
DWORD dwSize=0;
DWORD dwType;
for( i=1; i<100;i++ )
{
if( i < 10 )
wsprintf( csKeyName, L"Drivers\\Active\\0%d",i );
else
wsprintf( csKeyName, L"Drivers\\Active\\%d",i );
wprintf( L"trying %s",csKeyName);
if( ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE, csKeyName, 0, 0, &hKey ) )
{
//NKDbgPrintfW(L"[%s]\r\n",csKeyName);
dwSize=sizeof(csDevice);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, L"Key", NULL, &dwType, (LPBYTE)csDevice, &dwSize) )
csDevice[0]=0;
;
//NKDbgPrintfW(L"\"Key\"=%s\r\n",csDevice);
dwSize=sizeof(csDevice);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, L"Name", NULL, &dwType, (LPBYTE)csName, &dwSize) )
csName[0]=0;
;
//NKDbgPrintfW(L"\"Name\"=%s\r\n",csName);
//NKDbgPrintfW(L"\r\n");
if( pcsDevName && csName[0] )
{
if( !wcscmp( pcsDevName , csName ) )
{
break;
}
}
else if( pcsDevKey && csDevice[0] )
{
if( !wcscmp( pcsDevKey , csDevice ) )
{
break;
}
}
RegCloseKey( hKey );
hKey=NULL;
}
}
if( hKey )
{
//The device is found, read out Hnd and deactivate it.
DWORD dwDevHnd;
dwSize=sizeof(dwDevHnd);
if( ERROR_SUCCESS == RegQueryValueEx( hKey, L"Hnd", NULL, &dwType, (LPBYTE)&dwDevHnd, &dwSize) )
{
DeactivateDevice( (HANDLE)dwDevHnd );
//Because pcsDevKey maybe NULL, csDevice is used.
ActivateDevice( csDevice ,NULL );
}
RegCloseKey( hKey );
}
else if( pcsDevKey )
{
ActivateDevice( pcsDevKey, NULL );
}
}