📄 sdpprint.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/**
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
Abstract:
Windows CE Bluetooth application sample
Demonstrates using the COM apis in SDP user to print data in an SDP record
or an SDP Attribute or ServiceAttribute response.
The program takes one argument, which is the name of a file that contains SDP
record to display. File format should be hex digits (without 0x) separated by
white spaces, e.g. "35 43 35 41 09 00 ...".
*/
#include <windows.h>
#include <bthapi.h>
#include <initguid.h>
#include <bt_sdp.h>
#include <svsutil.hxx>
#include "..\sdpcommon\sdpcommon.h"
// Sample of what a service attribute response looks like in raw form.
UCHAR szBuf[] = {0x35, 0x43, 0x35, 0x41, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x06, 0x09, 0x00, 0x04, 0x35, 0x11,
0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0A, 0x35, 0x03, 0x19, 0x00, 0x08,
0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09,
0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x06, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x03, 0x46, 0x54, 0x50};
int MyReadFile(WCHAR *wszFileName, CHAR **ppszFileData, unsigned long *pulFileSize);
BOOL ConvertToByteStream(PSTR szRecord, DWORD cbRecord, DWORD *pcbWrite);
const WCHAR cszFormatError[] = L"File must contain XX XX XX ..., where XX are hex digits separated by spaces!\r\n";
void SdpPrintFunction(PVOID pvContext, BOOL fError, WCHAR *wszFormat,...) {
if (fError)
wprintf(L"ERROR: ");
va_list ap;
va_start(ap,wszFormat);
vwprintf(wszFormat,ap);
va_end (ap);
}
int
WINAPI
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow)
{
PSTR szRecord = NULL;
DWORD cbRecord;
DWORD cbWrite = 0;
ULONG ulRecords = 0;
DWORD i;
ISdpRecord **pRecordArg=NULL;
CoInitializeEx(NULL,COINIT_MULTITHREADED);
svsutil_Initialize();
if (! MyReadFile(lpCmdLine,&szRecord,&cbRecord)) {
wprintf(L"Unable to open %s, error = 0x%08x\r\n",lpCmdLine,GetLastError());
goto done;
}
if (! ConvertToByteStream(szRecord,cbRecord,&cbWrite))
goto done;
if (FAILED(ServiceAndAttributeSearchParse((PUCHAR)szRecord,cbWrite,&pRecordArg,&ulRecords)))
goto done;
for (i = 0; i < ulRecords; i++) {
PrintRecordInfo(pRecordArg[i],i,SdpPrintFunction,NULL);
}
done:
if (szRecord)
g_funcFree(szRecord,g_pvFreeData);
for (i = 0; i < ulRecords; i++)
pRecordArg[i]->Release();
if (pRecordArg)
CoTaskMemFree(pRecordArg);
svsutil_DeInitialize();
CoUninitialize();
return 0;
}
// Pass wszFileName as argument, function will allocate *ppszFileData to contain file contents.
// note: use g_funcFree to release *ppszFileData, not LocalFree.
int MyReadFile(WCHAR *wszFileName, CHAR **ppszFileData, unsigned long *pulFileSize)
{
HANDLE hFile;
DWORD cbRead = 0;
int iRet = 0;
hFile = CreateFile(wszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE == hFile)
goto done;
*pulFileSize = GetFileSize(hFile,NULL);
if (INVALID_HANDLE_VALUE == (HANDLE) *pulFileSize)
goto done;
*ppszFileData = (CHAR*)g_funcAlloc(*pulFileSize + 1,g_pvAllocData);
if (NULL == (*ppszFileData))
goto done;
(*ppszFileData)[*pulFileSize] = '\0';
if (! ReadFile(hFile, (LPVOID) *ppszFileData, *pulFileSize, &cbRead, 0))
goto done;
iRet = 1;
done:
if (INVALID_HANDLE_VALUE != hFile)
CloseHandle(hFile);
return iRet;
}
BOOL AsciiByteToHex(CHAR cIn, CHAR *pcOut) {
if (cIn >= '0' && cIn <= '9')
*pcOut += cIn - 0x30;
else if (cIn >= 'A' && cIn <= 'F')
*pcOut += cIn - 0x37;
else if (cIn >= 'a' && cIn <= 'f')
*pcOut += cIn - 0x57;
else
return FALSE;
return TRUE;
}
BOOL ConvertToByteStream(PSTR szRecord, DWORD cbRecord, DWORD *pcbWrite) {
PSTR szRead = szRecord;
PSTR szWrite = szRecord;
// we read file as string "AB 01 F3 ..", need to convert to hex.
for (DWORD i = 0; i < cbRecord-1; i++) {
CHAR c = 0;
if ((!isspace(*(szRead+2)) && (*(szRead+2) != '\0'))) {
wprintf(cszFormatError);
return FALSE;
}
if (!AsciiByteToHex(*szRead++,&c)) {
wprintf(cszFormatError);
return FALSE;
}
c *= 16;
if (!AsciiByteToHex(*szRead++,&c)) {
wprintf(cszFormatError);
return FALSE;
}
*szWrite++ = c;
(*pcbWrite)++;
while (isspace(*szRead))
szRead++;
if (*szRead == 0)
break;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -