C# BluetoothGATTGetCharacteristics在BluetoothAPI.dll中引发访问冲突读取位置0xFFFFFFF7

C# BluetoothGATTGetCharacteristics在BluetoothAPI.dll中引发访问冲突读取位置0xFFFFFFF7,c#,c++,.net,windows,bluetooth-lowenergy,C#,C++,.net,Windows,Bluetooth Lowenergy,我正在尝试编写一个Win32控制台应用程序,该应用程序将连接iPhone Bluetooth以提供一些可扩展的服务。如果我在这里出错,请纠正我- 在以下位置获取访问冲突错误- hr = BluetoothGATTGetCharacteristics( hLEDevice, pServiceBuffer, 0, NULL, &charBufferSize, BLUETOOTH_GATT_FLAG_NONE); 原因是,我无法循环此设备cla

我正在尝试编写一个Win32控制台应用程序,该应用程序将连接iPhone Bluetooth以提供一些可扩展的服务。如果我在这里出错,请纠正我-

在以下位置获取访问冲突错误-

hr = BluetoothGATTGetCharacteristics(
    hLEDevice,
    pServiceBuffer,
    0,
    NULL,
    &charBufferSize,
    BLUETOOTH_GATT_FLAG_NONE);
原因是,我无法循环此设备classId的-
SetupDienumDeviceInterface

主要功能了解更多详细信息-

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <bthdef.h>
#include <Bluetoothleapis.h>
#pragma comment(lib, "SetupAPI")
#pragma comment(lib, "BluetoothApis.lib")

HANDLE GetBLEHandle(__in GUID AGuid)
{
    HDEVINFO hDI;
    SP_DEVICE_INTERFACE_DATA did;
    SP_DEVINFO_DATA dd;
    GUID BluetoothInterfaceGUID = AGuid;
    HANDLE hComm = NULL;

    hDI = SetupDiGetClassDevs(&BluetoothInterfaceGUID, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

    if (hDI == INVALID_HANDLE_VALUE) return NULL;

    did.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    dd.cbSize = sizeof(SP_DEVINFO_DATA);

    for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDI, NULL, &BluetoothInterfaceGUID, i, &did); i++)
    {
        SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;

        DeviceInterfaceDetailData.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        DWORD size = 0;

        if (!SetupDiGetDeviceInterfaceDetail(hDI, &did, NULL, 0, &size, 0))
        {
            int err = GetLastError();

            if (err == ERROR_NO_MORE_ITEMS) break;

            PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)GlobalAlloc(GPTR, size);

            pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

            if (!SetupDiGetDeviceInterfaceDetail(hDI, &did, pInterfaceDetailData, size, &size, &dd))
                break;

            hComm = CreateFile(
                pInterfaceDetailData->DevicePath,
                GENERIC_WRITE | GENERIC_READ,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL,
                OPEN_EXISTING,
                0,
                NULL);

            GlobalFree(pInterfaceDetailData);
        }
    }

    SetupDiDestroyDeviceInfoList(hDI);
    return hComm;
}


int main(int argc, char *argv[], char *envp[]){

GUID AGuid = GUID_DEVCLASS_BLUETOOTH;



//now get the handle 
HANDLE hLEDevice = GetBLEHandle(AGuid);


//Step 2: Get a list of services that the device advertises
// first send 0,NULL as the parameters to BluetoothGATTServices inorder to get the number of
// services in serviceBufferCount
USHORT serviceBufferCount;
////////////////////////////////////////////////////////////////////////////
// Determine Services Buffer Size
////////////////////////////////////////////////////////////////////////////

HRESULT hr = BluetoothGATTGetServices(
    hLEDevice,
    0,
    NULL,
    &serviceBufferCount,
    BLUETOOTH_GATT_FLAG_NONE);

if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) != hr) {
    printf("BluetoothGATTGetServices - Buffer Size %d", hr);
}

PBTH_LE_GATT_SERVICE pServiceBuffer = (PBTH_LE_GATT_SERVICE)
    malloc(sizeof(BTH_LE_GATT_SERVICE) * serviceBufferCount);

if (NULL == pServiceBuffer) {
    printf("pServiceBuffer out of memory\r\n");
}
else {
    RtlZeroMemory(pServiceBuffer,
        sizeof(BTH_LE_GATT_SERVICE) * serviceBufferCount);
}

////////////////////////////////////////////////////////////////////////////
// Retrieve Services
////////////////////////////////////////////////////////////////////////////

USHORT numServices;
hr = BluetoothGATTGetServices(
    hLEDevice,
    serviceBufferCount,
    pServiceBuffer,
    &numServices,
    BLUETOOTH_GATT_FLAG_NONE);

if (S_OK != hr) {
    printf("BluetoothGATTGetServices - Buffer Size %d", hr);
}


//Step 3: now get the list of charactersitics. note how the pServiceBuffer is required from step 2
////////////////////////////////////////////////////////////////////////////
// Determine Characteristic Buffer Size
////////////////////////////////////////////////////////////////////////////

USHORT charBufferSize;
hr = BluetoothGATTGetCharacteristics(
    hLEDevice,
    pServiceBuffer,
    0,
    NULL,
    &charBufferSize,
    BLUETOOTH_GATT_FLAG_NONE);

if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) != hr) {
    printf("BluetoothGATTGetCharacteristics - Buffer Size %d", hr);
}

PBTH_LE_GATT_CHARACTERISTIC pCharBuffer;
if (charBufferSize > 0) {
    pCharBuffer = (PBTH_LE_GATT_CHARACTERISTIC)
        malloc(charBufferSize * sizeof(BTH_LE_GATT_CHARACTERISTIC));

    if (NULL == pCharBuffer) {
        printf("pCharBuffer out of memory\r\n");
    }
    else {
        RtlZeroMemory(pCharBuffer,
            charBufferSize * sizeof(BTH_LE_GATT_CHARACTERISTIC));
    }


    USHORT numChars;
    hr = BluetoothGATTGetCharacteristics(
        hLEDevice,
        pServiceBuffer,
        charBufferSize,
        pCharBuffer,
        &numChars,
        BLUETOOTH_GATT_FLAG_NONE);

    if (S_OK != hr) {
        printf("BluetoothGATTGetCharacteristics - Actual Data %d", hr);
    }

    if (numChars != charBufferSize) {
        printf("buffer size and buffer size actual size mismatch\r\n");
    }
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“SetupAPI”)
#pragma注释(lib,“bluetoothapi.lib”)
句柄GetBLEHandle(\uu在GUID AGuid中)
{
HDEVINFO-hDI;
SP_设备_接口_数据不存在;
SP_设备信息_数据dd;
GUID BluetoothInterfaceGUID=AGuid;
句柄hComm=NULL;
hDI=SetupDiGetClassDevs(&BluetoothInterfaceGUID,NULL,NULL,DIGCF_设备接口| DIGCF_PRESENT);
if(hDI==无效的\u句柄\u值)返回NULL;
did.cbSize=sizeof(SP_设备_接口_数据);
dd.cbSize=sizeof(SP设备信息数据);
对于(DWORD i=0;SetupDienumDeviceInterface(hDI、NULL和Bluetooth接口GUID、i和did);i++)
{
SP\设备\接口\详细信息\数据设备接口详细信息数据;
DeviceInterfaceDetailData.cbSize=sizeof(SP\u设备\u接口\u细节\u数据);
DWORD大小=0;
if(!SetupDiGetDeviceInterfaceDetail(hDI,&did,NULL,0,&size,0))
{
int err=GetLastError();
如果(err==ERROR\u NO\u MORE\u ITEMS)中断;
PSP_设备_接口_详细信息_数据pInterfaceDetailData=(PSP_设备_接口_详细信息_数据)GlobalAlloc(GPTR,大小);
pInterfaceDetailData->cbSize=sizeof(SP\u设备\u接口\u细节\u数据);
if(!SetupDiGetDeviceInterfaceDetail(hDI、&did、pInterfaceDetailData、size、&size、&dd))
打破
hComm=CreateFile(
pInterfaceDetailData->DevicePath,
一般写,一般读,
文件共享读取文件共享写入,
无效的
开放式,
0,
无效);
GlobalFree(pInterfaceDetailData);
}
}
SetupDiDestroyDeviceInfo列表(hDI);
返回hComm;
}
int main(int argc,char*argv[],char*envp[]{
GUID AGuid=GUID\u DEVCLASS\u蓝牙;
//现在抓住把手
句柄设备=GetBLEHandle(AGuid);
//步骤2:获取设备广告的服务列表
//首先将0,NULL作为参数发送到BluetoothGATTServices,以获取
//serviceBufferCount中的服务
USHORT-serviceBufferCount;
////////////////////////////////////////////////////////////////////////////
//确定服务缓冲区大小
////////////////////////////////////////////////////////////////////////////
HRESULT hr=蓝牙GattGetServices(
电子设备,
0,
无效的
&serviceBufferCount,
蓝牙(关贸总协定)(标志)(无);;
如果(HRESULT\u来自WIN32(错误\u更多数据)!=hr){
printf(“BluetoothGATTGetServices-缓冲区大小%d”,hr);
}
PBTH_LE_GATT_服务pServiceBuffer=(PBTH_LE_GATT_服务)
malloc(sizeof(BTH_LE_GATT_服务)*serviceBufferCount);
if(NULL==pServiceBuffer){
printf(“pServiceBuffer内存不足\r\n”);
}
否则{
RtlZeroMemory(pServiceBuffer,
sizeof(BTH_LE_GATT_服务)*serviceBufferCount);
}
////////////////////////////////////////////////////////////////////////////
//检索服务
////////////////////////////////////////////////////////////////////////////
USHORT numServices;
hr=蓝牙GattGetServices(
电子设备,
serviceBufferCount,
pServiceBuffer,
&numServices,
蓝牙(关贸总协定)(标志)(无);;
如果(S_OK!=小时){
printf(“BluetoothGATTGetServices-缓冲区大小%d”,hr);
}
//步骤3:现在获取字符列表。注意步骤2中如何需要pServiceBuffer
////////////////////////////////////////////////////////////////////////////
//确定特征缓冲区大小
////////////////////////////////////////////////////////////////////////////
USHORT-Charbuffer大小;
hr=蓝牙GATTGETCH特征(
电子设备,
pServiceBuffer,
0,
无效的
&字符大小,
蓝牙(关贸总协定)(标志)(无);;
如果(HRESULT\u来自WIN32(错误\u更多数据)!=hr){
printf(“BluetoothGATTGetCharacteristics-缓冲区大小%d”,hr);
}
PBTH_LE_GATT_特征pCharBuffer;
如果(charBufferSize>0){
pCharBuffer=(PBTH_LE_GATT_特性)
malloc(charBufferSize*sizeof(BTH_LE_GATT_特征));
if(NULL==pCharBuffer){
printf(“pCharBuffer内存不足\r\n”);
}
否则{
RtlZeroMemory(pCharBuffer,
charBufferSize*sizeof(BTH_LE_GATT_特性);
}
USHORT numChars;
hr=蓝牙GATTGETCH特征(
电子设备,
pServiceBuffer,
字符大小,
pCharBuffer,
&numChars,
蓝牙(关贸总协定)(标志)(无);;
如果(S_OK!=小时){
printf(“蓝牙GattGetCharacteristics-实际数据%d”,hr);
}
if(numChars!=charBufferSize){
printf(“缓冲区大小和缓冲区大小实际大小不匹配\r\n”);
}
}
获取错误

中0x0F5CE138(BluetoothAPI.dll)处未处理的异常 ConsoleApplication.exe:0xC0000005:访问冲突读取位置 0xFFFFF7

有人能提出原因吗


我尝试使用C#桌面应用程序,但运气不佳,在“
wait GattDeviceService.FromIdAsync(device.Id)
”行中也出现了FileNotFound错误。

问题很老,但仍然没有答案。这是因为
getblehold
函数返回空值。
AGuid
是可移动设备的GUID(在Microsoft示例中,它是HRM-心率监视器),您没有连接此GUID的设备。因此
NULL
from
getblehold
中的
device\u未找到
。如果您使用
NULL
设备调用
BluetoothGATTGetServices