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

📄 bthnscreate.cxx

📁 SDP Search and Record generator OVERVIEW: Recognizing the difficulty in creating an SDP service
💻 CXX
📖 第 1 页 / 共 3 页
字号:
    fscanf(pf, "%I64x,",&u128.HighPart);
    fscanf(pf, "%I64x\n",&u128.LowPart);

    NDW ndw;
    ndw.Int128(&u128);
    MyPrint(L"largeInt.LowPart  = %s;",PrintNumber64(u128.LowPart));
    MyPrint(L"largeInt.HighPart = %s;",PrintNumber64(u128.HighPart));
    MyPrint(L"ndw.Int128(&largeInt);");
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUuid16(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);
    NDW ndw;
    ndw.Uuid16((USHORT) (i & 0xFFFF));
    MyPrint(L"ndw.Uuid16(0x%x);",(i & 0xFFFF));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUuid32(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%x\n", &i);
    NDW ndw;
    ndw.Uuid32(i);
    MyPrint(L"ndw.Uuid32(0x%x);",i);
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUuid128(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    GUID guid[2];
    memset(&guid,0,sizeof(guid));
    fscanf(pf,GUID_FORMAT,GUID_ELEMENTS((&guid[0])));

    NDW ndw;
    ndw.Uuid128(guid[0]);

    MyPrint(L"SET_GUID_VALUES(guid,0x%08x,0x%04x,0x%04x,",guid[0].Data1,guid[0].Data2,guid[0].Data3);
    MyPrint(L"  0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);",guid[0].Data4[0],guid[0].Data4[1] ,guid[0].Data4[2],guid[0].Data4[3],guid[0].Data4[4],guid[0].Data4[5],guid[0].Data4[6],guid[0].Data4[7]);
    MyPrint(L"ndw.Uuid128(guid);");
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddBoolean(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    int i;
    fscanf(pf, "%i\n", &i);

    NDW ndw;
    ndw.Boolean(i);
    MyPrint(L"ndw.Boolean(%i);",i);
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddNil(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    NDW ndw;
    ndw.Nil();
    MyPrint(L"ndw.Nil();");
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void SdpReadString(FILE *pf, CHAR *buffer, int iSizeofBuffer)
{
    int i;
    for (i = 0; i < iSizeofBuffer; i++)  {
        if (EOF == fscanf(pf, "%c", &buffer[i])) {
            buffer[i] = 0;
            break;
        }

        if (buffer[i] == '\r' || buffer[i] == '\n')  {
            if (buffer[i] == '\r')
                fscanf(pf, "%c", &buffer[i]);
            buffer[i] = 0;
            break;
        }
    }
}

// Handles comments in stream.
BOOL ParseHandleComment(FILE *pf, PSTR szBuffer, DWORD cbBuffer)
{
    if (szBuffer[0] != SDP_RECORD_COMMENT_CHAR)
        return FALSE;
 
    int iLen = strlen(szBuffer);
    SdpReadString(pf,szBuffer+iLen,cbBuffer-iLen);

    MyPrint(L"// %S",&szBuffer[1]);
    return TRUE;
}


void AddString(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    CHAR buffer[2048];
    SdpReadString(pf,buffer,sizeof(buffer));

    NDW ndw;
    ndw.String(buffer, strlen(buffer));
    MyPrint(L"ndw.String(\"%S\", %d);",buffer,strlen(buffer));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddUrl(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    CHAR buffer[2048];
    SdpReadString(pf,buffer,sizeof(buffer));

    NDW ndw;
    ndw.Url(buffer, strlen(buffer));
    MyPrint(L"ndw.Url(\"%S\", %x);",buffer,strlen(buffer));
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
}

void AddSeqAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont, BOOL fSequence) 
{
    PrintLineBreak(1);
    MyPrint(L"// New Sequence List handling");
    MyPrint(L"do {");
    INDENT(1);

    DO(ISdpNodeContainer *pContainer = NULL;);

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

    MyPrint(L"HRESULT hr = pCFContainer->CreateInstance(NULL,");
    MyPrint(L"                  __uuidof(ISdpNodeContainer),");
    MyPrint(L"                  (PVOID*) &pContainer);\n");
    
    MyPrint(L"if (!pContainer) {");
    INDENT(1);
    MyPrint(L"pNewContainer = NULL;");
    MyPrint(L"fSuccess = FALSE;");
    MyPrint(L"break;");
    INDENT(-1);
    MyPrint(L"}\n");
    
    if (!pContainer)  {
        wprintf(L"*** ERROR *** CoCreateInstance ISdpNodeContainer failed, hr=0x%08x\n",hr);
        return;
    }

    if (fSequence) {
        DO(pContainer->SetType(NodeContainerTypeSequence););
    }
    else {
        DO(pContainer->SetType(NodeContainerTypeAlternative););
    }


    CHAR buffer[2048];
    fscanf(pf, "\n");

    while (1) {
        fscanf(pf, "%s", buffer);
        if (ParseHandleComment(pf,buffer,sizeof(buffer)))
            continue;

        if (_stricmp(buffer,"END") == 0)
            break;

        for (int i = 0; entries[i].name != NULL; i++) {
            if (_stricmp(buffer, entries[i].name) == 0) {
                entries[i].pfnParse(pf,0, 0,pContainer);
                break;
            }
        }
        if (entries[i].name == NULL) {
            wprintf(L"*** ERROR *** %S unknown type",buffer);
            break;
        }
    }

    MyPrint(L"pNewContainer = pContainer;");
    INDENT(-1);
    MyPrint(L"} while(0);");

    MyPrint(L"if (pNewContainer && fSuccess) {");

    NDW ndw;
    INDENT(1);
    ndw.SdpNodeContainer(pContainer);
    MyPrint(L"ndw.SdpNodeContainer(pNewContainer);");
    SdpAddEntry(attribVal,pRecord, pCont,&ndw);
    pContainer->Release();
    INDENT(-1);
    MyPrint(L"}");

    MyPrint(L"if (pNewContainer) {");
    MyPrint(L"\tpNewContainer->Release();");
    MyPrint(L"\tpNewContainer = NULL;");
    MyPrint(L"}");

    if (g_iIndent != 1) {
        // We're in the call recursivly.
        MyPrint(L"if (!fSuccess) {");
        MyPrint(L"\tpNewContainer = pContainer;");
        MyPrint(L"\tbreak;");
        MyPrint(L"}");
    }
    else {
        // This is the top level container.
        MyPrint(L"if (!fSuccess)");
        MyPrint(L"\tgoto Finished;");
    }
    MyPrint(L"// End Sequence List handling\n");
}

void AddSequence(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    AddSeqAlternative(pf, attribVal, pRecord, pCont, TRUE);
}

void AddAlternative(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    AddSeqAlternative(pf, attribVal, pRecord, pCont, FALSE);
}

void ContainerEnd(FILE *pf, USHORT attribVal, ISdpRecord *pRecord, ISdpNodeContainer *pCont)
{
    wprintf(L"*** ERROR ***  CointainerEnd not support \n");
}

void CommonIncludes(void) {
    if (!g_fVerbose)
        return;

    wprintf(L"#include <windows.h>\n"
            L"#include <bthapi.h>\n"
            L"#include <winsock2.h>\n"
            L"#include <bt_sdp.h>\n"
            L"#include <bt_api.h>\n"
            L"#include <ws2bth.h>\n"
            L"#include <bthnscreate.hxx>\n\n\n"
            L"// Link against the following libraries: coredll.dll, ole32.lib, \n"
            L"//   uuid.lib, oleaut32.lib, and btdrt.lib\n");
}

void CreateSdpRecord(FILE* pf)
{
    CommonIncludes();
    MyPrint(L"IClassFactory *pCFContainer = NULL;\n\n");
    MyPrint(L"{");
    INDENT(1);

    DO(IClassFactory *pCFRecord = NULL;);

    DO(ISdpRecord *pRecord = NULL;)
    DO(UCHAR *pSdpRecordStream=NULL;)
    DO(DWORD dwSize;)

    int attribId;
    CHAR buffer[2048];
    DO(HRESULT hr;)

    // These are variables that are not used in this program itself but will be used 
    // in verbose programs.
    MyPrint(L"NDW ndw;");
    MyPrint(L"ISdpNodeContainer *pNewContainer = NULL;");
    MyPrint(L"SDP_LARGE_INTEGER_16 largeInt;");
    MyPrint(L"SDP_ULARGE_INTEGER_16 ulargeInt;");
    MyPrint(L"GUID guid;");
    MyPrint(L"ULONG recordHandle = 0;\n");
    MyPrint(L"BOOL fSuccess = TRUE;\n\n");

	if (g_fVerbose) {
        wprintf(L"\tWSAQUERYSET Service;\n"
                L"\tPBTHNS_SETBLOB pSetBlob;\n"
                L"\tULONG sdpVersion = BTH_SDP_VERSION;\n"
                L"\tBLOB blob;\n\n\n");
	}

    DO(CoInitializeEx(NULL,COINIT_MULTITHREADED););

    hr = CoGetClassObject(__uuidof(SdpRecord),
                     CLSCTX_INPROC_SERVER,
                     NULL,    
                     IID_IClassFactory,
                     (PVOID*)&pCFRecord);

    MyPrint(L"hr = CoGetClassObject(__uuidof(SdpRecord),\n"
                 L"\t                      CLSCTX_INPROC_SERVER,\n"
                 L"\t                      NULL,\n"
                 L"\t                      IID_IClassFactory,\n"
                 L"\t                      (PVOID*)&pCFRecord);\n");

    // We can't just use the DO macro because this code path may not
    // get executed when the sdprecord is run, but may be included if the
    // text is copied and pasted into a program.

    MyPrint(L"if (pCFRecord == NULL)\n\t\tgoto Finished;\n");

    if (pCFRecord == NULL) {
        wprintf(L"*** ERROR ***  CoGetClassObject(__uuidof(SdpRecord) fails, err=0x%08x\n",hr);
        goto Finished;
    }

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

    MyPrint(L"hr = CoGetClassObject(__uuidof(SdpNodeContainer),\n"
                 L"\t                      CLSCTX_INPROC_SERVER,\n"
                 L"\t                      NULL,\n"
                 L"\t                      IID_IClassFactory,\n"
                 L"\t                      (PVOID*)&pCFContainer);\n");

    MyPrint(L"if (pCFContainer == NULL) \n\t\tgoto Finished;\n");

    if (pCFContainer == NULL)  {
        wprintf(L"*** ERROR *** CoGetClassObject(__uuidof(SdpNodeContainer) fails, err=0x%08x\n",hr);
        goto Finished;
    }

    hr = pCFRecord->CreateInstance(NULL, 
                              __uuidof(ISdpRecord),
                              (PVOID*) &pRecord);

    MyPrint(L"hr = pCFRecord->CreateInstance(NULL, \n"
                 L"\t                               __uuidof(ISdpRecord),\n"
                 L"\t                               (PVOID*) &pRecord);\n");


    MyPrint(L"if (pRecord == NULL) \n\t\tgoto Finished;\n");

    if (pRecord == NULL) {
        wprintf(L"*** ERROR *** CreateInstance on ISdpRecord fails, err = 0x%08x\n",hr);
        goto Finished;
    }

    while (fscanf(pf, "%s", buffer) != EOF) {
        if (ParseHandleComment(pf,buffer,sizeof(buffer)))
            continue;

        // Otherwise the first element on line is the Attribute ID
        if (! sscanf(buffer, "%x", &attribId)) {
            wprintf(L"*** ERROR ***: buffer does not contain attribute ID, buffer = %S\n",buffer);
            break;
        }

        fscanf(pf, "%s\n", buffer);
        for (int i = 0; entries[i].name != NULL; i++) {
            if (_stricmp(buffer, entries[i].name) == 0) {

                entries[i].pfnParse(pf,attribId, pRecord, NULL);
                break;
            }
        }
        if (entries[i].name == NULL) { 
            wprintf(L"*** ERROR ***: buffer does not contain attribute ID, buffer = %S\n",buffer);
            goto Finished;
        }
    }

    MyPrint(L"// *** pSdpRecordStream contains the final record information");
    DO(hr = pRecord->WriteToStream(&pSdpRecordStream, &dwSize,0,0););
    
    PrintLineBreak(2);
    MyPrint(L"#if 0");

⌨️ 快捷键说明

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