📄 deviceenum.cpp
字号:
//set a flag if the passed in device is equal to the one at this
// iteration
if(bEqual && !fIsIRDA)
{
bEqual = ( (SUCCEEDED(pNode->pItem->GetDeviceAddress(&var2))) &&
(
((varAddress.vt == VT_BSTR) && (varAddress.vt == var2.vt) && (0 == wcscmp(varAddress.bstrVal, var2.bstrVal)))
)
);
}
else if(bEqual)
{
bEqual = ( (SUCCEEDED(pNode->pItem->GetDeviceName(&var2))) &&
(
((varName.vt == VT_BSTR) && (varName.vt == var2.vt) && (0 == wcscmp(varName.bstrVal, var2.bstrVal)))
)
);
}
VariantClear(&var2);
if (bEqual)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE, (L"FindDeviceThatMatch -- Device (%08x%08x) already present\n", dwNAP, dwSAP) );
//insert the device on the return list
DEVICE_LIST *pNew = new DEVICE_LIST();
if( !pNew )
{
VariantClear(&varName);
VariantClear(&varAddress);
VariantClear(&varTransport);
return E_OUTOFMEMORY;
}
pNode->pItem->AddRef();
pNew->pDevice = pNode->pItem;
pNew->pNext = pRetList;
pRetList = pNew;
}
pNode = pNode->pNext;
}
VariantClear(&varName);
VariantClear(&varAddress);
VariantClear(&varTransport);
//set the list pointer and return
*pDeviceList = pRetList;
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::FindDevice() : returning %s\n", generated_response == S_OK ? L"S_OK" : L"S_FALSE"));
return S_OK;
}
/*****************************************************************************/
/* CDeviceEnum::QueryInterface */
/* Query for a interface */
/*****************************************************************************/
HRESULT STDMETHODCALLTYPE
CDeviceEnum::QueryInterface(REFIID riid, void** ppv)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::QueryInterface()\n"));
if(!ppv)
return E_POINTER;
if(riid == IID_IUnknown)
*ppv = this;
else if(riid == IID_IDeviceEnum)
*ppv = static_cast<IDeviceEnum *>(this);
else
return *ppv = 0, E_NOINTERFACE;
return AddRef(), S_OK;
}
/*****************************************************************************/
/* CDeviceEnum::AddRef */
/* inc the ref count */
/*****************************************************************************/
ULONG STDMETHODCALLTYPE
CDeviceEnum::AddRef()
{
DEBUGMSG(OBEX_ADDREFREL_ZONE,(L"CDeviceEnum::AddRef()\n"));
return InterlockedIncrement((LONG *)&_refCount);
}
/*****************************************************************************/
/* CDeviceEnum::Release */
/* dec the ref count, on zero delete the object */
/*****************************************************************************/
ULONG STDMETHODCALLTYPE
CDeviceEnum::Release()
{
DEBUGMSG(OBEX_ADDREFREL_ZONE,(L"CDeviceEnum::Release()\n"));
SVSUTIL_ASSERT(_refCount != 0xFFFFFFFF);
ULONG ret = InterlockedDecrement((LONG *)&_refCount);
if(!ret)
delete this;
return ret;
}
/*****************************************************************************/
/* CDeviceEnum::EnumDevices */
/* helper function to return devices of a particular transport */
/*****************************************************************************/
HRESULT
CDeviceEnum::EnumDevices(LPDEVICEENUM *ppDeviceEnum, REFCLSID uuidTransport)
{
DEBUGMSG(OBEX_DEVICEENUM_ZONE,(L"CDeviceEnum::EnumDevices()\n"));
if (!ppDeviceEnum)
return E_POINTER;
*ppDeviceEnum = NULL;
CDeviceEnum *pEnum = new CDeviceEnum;
if (!pEnum)
return E_OUTOFMEMORY;
SVSUTIL_ASSERT(gpSynch->IsLocked());
BOOL bNull = (0 == memcmp(&uuidTransport, &CLSID_NULL, sizeof(CLSID)));
HRESULT hr = S_OK;
DEVICE_NODE *pNode = pListHead;
while (pNode)
{
CLSID uuidTrans = pNode->pItem->GetTransport();
if ( (bNull) || (0 == memcmp(&uuidTransport, &uuidTrans, sizeof(CLSID))) )
hr = pEnum->Insert(pNode->pItem);
if (FAILED(hr))
break;
pNode = pNode->pNext;
}
if (SUCCEEDED(hr))
hr = pEnum->QueryInterface(IID_IDeviceEnum, (LPVOID *)ppDeviceEnum);
pEnum->Release();
return hr;
}
HRESULT
CDeviceEnum::StartEnumRun(REFCLSID clsid)
{
SVSUTIL_ASSERT(clsid != CLSID_NULL);
SVSUTIL_ASSERT(gpSynch->IsLocked());
DEVICE_NODE *pNode = pListHead;
while (pNode)
{
PREFAST_ASSERT(pNode->pItem);
CLSID uuidTrans = pNode->pItem->GetTransport();
if (0 == memcmp(&clsid, &uuidTrans, sizeof(CLSID)))
{
pNode->pItem->SetPresent (FALSE);
}
pNode = pNode->pNext;
}
return S_OK;
}
/*****************************************************************************/
/* CDeviceEnum::FinishEnumRun */
/* Finish the enumeration run by building a propertybag enumerator that */
/* contains all the devices that are no longer present */
/* this is determined by !GetPresent() */
/*****************************************************************************/
HRESULT
CDeviceEnum::FinishEnumRun(DEVICE_PROPBAG_LIST **ppDevicesToRemove, REFCLSID uuidTransport)
{
SVSUTIL_ASSERT(uuidTransport != CLSID_NULL);
PREFAST_ASSERT(ppDevicesToRemove);
*ppDevicesToRemove = NULL;
SVSUTIL_ASSERT(gpSynch->IsLocked());
DEVICE_PROPBAG_LIST *pDevicesToRemove = NULL;
HRESULT hr = S_OK;
DEVICE_NODE *pNode = pListHead;
DEVICE_NODE *pPrev = NULL;
while (pNode)
{
PREFAST_ASSERT(pNode->pItem);
CLSID uuidTrans = pNode->pItem->GetTransport();
if (0==memcmp(&uuidTransport, &uuidTrans, sizeof(CLSID)))
{
LPPROPERTYBAG2 pBag = NULL;
//if we cant get a properybag, something is up so just continue
if(FAILED(pNode->pItem->GetPropertyBag(&pBag)) || !pBag)
{
SVSUTIL_ASSERT(FALSE);
pPrev = pNode;
pNode = pNode->pNext;
continue;
}
BOOL fIsCorpse = FALSE;
VARIANT varCorpse;
VariantInit(&varCorpse);
if(SUCCEEDED(pBag->Read(L"Corpse", &varCorpse, NULL)))
{
if(varCorpse.lVal == 1)
fIsCorpse = 1;
VariantClear(&varCorpse);
}
//if this bag hasnt timed out AND its not a corpse then its good
// so skip it
if(pNode->pItem->GetPresent() && !fIsCorpse)
{
pBag->Release();
pPrev = pNode;
pNode = pNode->pNext;
continue;
}
// unchain the node from list of active devices
if (!pPrev)
pListHead = pNode->pNext;
else
pPrev->pNext = pNode->pNext;
DEVICE_NODE *pCurrent = pNode->pNext;
//create a new node and chain to removal list
DEVICE_PROPBAG_LIST *pNew = new DEVICE_PROPBAG_LIST();
if( !pNew )
return E_OUTOFMEMORY;
pNew->pNext = pDevicesToRemove;
pNew->pBag = pBag;
pDevicesToRemove = pNew;
//clean up
pBag = NULL;
pNode->pItem->Release();
delete pNode;
if (FAILED(hr))
break;
pNode = pCurrent;
}
else
{
pPrev = pNode;
pNode = pNode->pNext;
}
}
*ppDevicesToRemove = pDevicesToRemove;
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -