⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 obutil.cxx

📁 Windows CE 6.0 Server 源码
💻 CXX
📖 第 1 页 / 共 2 页
字号:
    memset(&Service,0,sizeof(Service));
    Service.dwSize = sizeof(Service);
    Service.lpBlob = &blob;
    Service.dwNameSpace = NS_BTH;

    HINSTANCE hLib = LoadLibrary(L"btdrt.dll");
    if (!hLib) {
        IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_SdpAddRecord: Record too big. Max allowed size=%d\n", MAX_SDPRECSIZE));
        delete [] pSDPBuf;
        return GetLastError();
    }

    int iErr = ERROR_PROC_NOT_FOUND;
    typedef int (*BTHNSSETSERVICE)(LPWSAQUERYSET, WSAESETSERVICEOP, DWORD);
    BTHNSSETSERVICE pfnBthNsSetService = (BTHNSSETSERVICE )GetProcAddress(hLib, L"BthNsSetService");
    if (pfnBthNsSetService)
        iErr = pfnBthNsSetService(&Service,RNRSERVICE_REGISTER,0);
    
    if (iErr != ERROR_SUCCESS) {
        IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_SdpAddRecord: BthNsSetService returns 0x%08x.\n", iErr));
    }
    FreeLibrary(hLib);
    delete [] pSDPBuf;
    return iErr;
}


static int obutil_WriteSDPRecord(unsigned char *pBuf, unsigned long nSize, unsigned char nPort, ULONG *pulHandle) {
    SVSUTIL_ASSERT(pBuf);
    SVSUTIL_ASSERT((nPort > 0) && (nPort < 32));
    SVSUTIL_ASSERT(pulHandle);

    ISdpRecord *pSdpRecord = NULL;
    BOOL   fRet = FALSE;
    NodeData  nodeData;
    ISdpNodeContainer *pProtocolContainer = NULL;
    HRESULT hr;
    UCHAR *pSdpRecordStream=NULL;
    DWORD dwSize;

    IClassFactory *pCFContainer = NULL;
    ISdpNodeContainer *pL2CAPContainer = NULL;
    ISdpNodeContainer *pRFCOMMContainer = NULL;
    ISdpNodeContainer *pOBEXContainer = NULL;

    if (FAILED(CoInitializeEx(NULL,COINIT_MULTITHREADED)))
        return FALSE;

    hr = CoCreateInstance(__uuidof(SdpRecord),NULL,CLSCTX_INPROC_SERVER,__uuidof(ISdpRecord),(LPVOID *) &pSdpRecord);
    if (FAILED(hr))
        goto done;

    hr = pSdpRecord->CreateFromStream(pBuf, nSize);
    if (FAILED(hr))
        goto done;

    // remove any existing protocol info the extension may have set.
    pSdpRecord->SetAttribute(SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST,NULL);

    hr = CoGetClassObject(__uuidof(SdpNodeContainer),CLSCTX_INPROC_SERVER,NULL,
                          IID_IClassFactory,(PVOID*)&pCFContainer);

    if (FAILED(hr) || !pCFContainer)
        goto done;

    hr = pCFContainer->CreateInstance(NULL,__uuidof(ISdpNodeContainer),(PVOID*) &pProtocolContainer);

    if (FAILED(hr) || !pProtocolContainer)
        goto done;

    pProtocolContainer->SetType(NodeContainerTypeSequence);

    // L2CAP Entry
    hr = pCFContainer->CreateInstance(NULL,__uuidof(ISdpNodeContainer),(PVOID*) &pL2CAPContainer);

    if (FAILED(hr) || !pL2CAPContainer)
        goto done;

    pL2CAPContainer->SetType(NodeContainerTypeSequence);
    obutil_SetUUID16(&nodeData,L2CAP_PROTOCOL_UUID16);
    if (ERROR_SUCCESS != pL2CAPContainer->AppendNode(&nodeData))
        goto done;

    if (ERROR_SUCCESS != obutil_AppendContainer(pProtocolContainer,pL2CAPContainer))
        goto done;

    //  RFCOMM and cid
    hr = pCFContainer->CreateInstance(NULL,__uuidof(ISdpNodeContainer),(PVOID*) &pRFCOMMContainer);

    if (FAILED(hr) || !pRFCOMMContainer)
        goto done;

    pRFCOMMContainer->SetType(NodeContainerTypeSequence);

    obutil_SetUUID16(&nodeData,RFCOMM_PROTOCOL_UUID16);
    if (ERROR_SUCCESS != pRFCOMMContainer->AppendNode(&nodeData))
        goto done;

    obutil_SetUINT8(&nodeData,nPort); // channel ID!
    if (ERROR_SUCCESS != pRFCOMMContainer->AppendNode(&nodeData))
        goto done;

    if (ERROR_SUCCESS != obutil_AppendContainer(pProtocolContainer,pRFCOMMContainer))
        goto done;

    // OBEX
    hr = pCFContainer->CreateInstance(NULL,__uuidof(ISdpNodeContainer),(PVOID*) &pOBEXContainer);

    if (FAILED(hr) || !pOBEXContainer)
        goto done;

    pOBEXContainer->SetType(NodeContainerTypeSequence);
    obutil_SetUUID16(&nodeData,OBEX_PROTOCOL_UUID16);
    if (ERROR_SUCCESS != pOBEXContainer->AppendNode(&nodeData))
        goto done;

    if (ERROR_SUCCESS != obutil_AppendContainer(pProtocolContainer,pOBEXContainer))
        goto done;

    // insert everything into the record
    nodeData.type=SDP_TYPE_CONTAINER;
    nodeData.specificType=SDP_ST_NONE;
    nodeData.u.container = pProtocolContainer;
    if (ERROR_SUCCESS != pSdpRecord->SetAttribute(SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST, &nodeData))
        goto done;

    if (ERROR_SUCCESS != pSdpRecord->WriteToStream(&pSdpRecordStream, &dwSize,0,0))
        goto done;

    if (ERROR_SUCCESS == obutil_SdpAddRecord(pSdpRecordStream, dwSize, pulHandle)) {
        IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_INIT, L"[OBEX] obutil_WriteSDPRecord successful\n"));
        fRet = TRUE;    
    }

done:
    if (pL2CAPContainer)
        pL2CAPContainer->Release();

    if (pRFCOMMContainer)
        pRFCOMMContainer->Release();

    if (pOBEXContainer)
        pOBEXContainer->Release();

    if (pSdpRecord)
        pSdpRecord->Release();

    if (pCFContainer)
        pCFContainer->Release();

    if (pSdpRecordStream)
        CoTaskMemFree(pSdpRecordStream);

    CoUninitialize();
    return fRet;

}
    
int obutil_RegisterPort(unsigned char nPort, ULONG *pBTHHandleArray, UINT *uiNumInArray) {
    SVSUTIL_ASSERT(pBTHHandleArray && uiNumInArray);

    HKEY hKey;
    LONG lRes = ERROR_SUCCESS;
    WCHAR szGUID[128];
    UCHAR *pcData = new UCHAR[MAX_SDPRECSIZE];
    DWORD dwIndex = 0;
    DWORD cName = sizeof(szGUID)/sizeof(WCHAR);
    DWORD cbData;
    UINT uiTotalSlotsAvailable = *uiNumInArray;

    *uiNumInArray = 0;
    
    if(NULL == pcData)
        return ERROR_OUTOFMEMORY;

    UINT numArraySlotsUsed = 0;
    
    if ((nPort == 0) || (nPort > 31))  {
        IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_RegisterPort: Invalid port number %d\n", nPort));
        delete [] pcData;
        return ERROR_INVALID_PARAMETER;
    }
    
    if (ERROR_SUCCESS != (lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Obex\\Services", 0, 0, &hKey))) {
        IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_RegisterPort: Failed to open HKLM\\Software\\Microsoft\\Obex\\Services\n"));
        delete [] pcData;
        return lRes;
    }

    SVSUTIL_ASSERT(hKey);
    BOOL bWritten = FALSE;
    while((ERROR_SUCCESS == (lRes = RegEnumKeyEx(hKey, dwIndex++, szGUID, &cName, NULL, NULL, NULL, NULL))) && 
        (cName < (sizeof(szGUID)/sizeof(WCHAR)) ) ) {
        HKEY hKey1;
        
        if (ERROR_SUCCESS == (lRes = RegOpenKeyEx(hKey, szGUID, 0, 0, &hKey1))) {
            SVSUTIL_ASSERT(hKey1);

            DWORD dwType = REG_BINARY;
            cbData = MAX_SDPRECSIZE;
            if ( (ERROR_SUCCESS == (lRes = RegQueryValueEx(hKey1, L"BluetoothSdpRecord", NULL, &dwType, pcData, &cbData))) 
                && (dwType == REG_BINARY) && (cbData < MAX_SDPRECSIZE) ){

                ULONG ulHandle;

                //if there arent available spaces for the record (handle spaces)
                //   or if writing fails, send back an error 
                if (numArraySlotsUsed >= uiTotalSlotsAvailable ||
                    !obutil_WriteSDPRecord(pcData, cbData, nPort, &ulHandle))
                {
                    IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_RegisterPort: obutil_WriteSDPRecord fails for %s\n", szGUID));
                    lRes = ERROR_FUNCTION_FAILED;
                }
                else
                {
                    //
                    //  write down the handle for deletion later
                    //                    
                    IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] AddressFamily::Start: cached SDP record handle\n"));                            
                    pBTHHandleArray[numArraySlotsUsed] = ulHandle;    
                    numArraySlotsUsed++;                        
                    bWritten = TRUE;
                }
                

            }
            RegCloseKey(hKey1);
            
        } else {
            IFDBG(svslog_DebugOut(VERBOSE_OUTPUT_ERRORS, L"[OBEX] obutil_RegisterPort: RegOpenKeyEx failed for %s\n", szGUID));
        }

        cName = sizeof(szGUID)/sizeof(WCHAR);

    }

    *uiNumInArray = numArraySlotsUsed;
    
    RegCloseKey(hKey);
    delete [] pcData;
    
    //return ERROR_SUCCESS if we successfully wrote at least one SDP record
    return ((lRes == ERROR_NO_MORE_ITEMS) && (bWritten)) ? ERROR_SUCCESS : lRes;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -