title: Crash when connecting to Bluetooth device via WinAPI Socket (Access violation reading location 0x00000004)
body: <p>I think you are my last hope. I have got here a Bluetooth device (it is a sensor to be more precisely) which I want to connect to and read data from. The device offers SPP (Serial Port Profile). To avoid the problem of reliable mapping from Bluetooth addresses and virtual serial ports (COM Ports), I am going to use sockets.</p>
  
  <p>Unfortunately the application always crashes before returning from WinAPI function <strong>connect</strong>(...) with:  <em>0xC0000005: Access violation reading location 0x00000004</em>, so I get <strong>no error code</strong>.</p>
  
  <p><strong>BUT, and that is weird, when I right-click on the Bluetooth System Tray Icon to to show available devices, my device shows up being authenticated and connected.</strong> This list was empty before, of course. </p>
  
  <p>My OS is Windows 7 64 Bit, the IDE is Visual Studio 2010, Microsoft Bluetooth Stack. Code to find and connect to my only device:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;algorithm&gt;
  #include &lt;cassert&gt;
  
  #define WIN32_LEAN_AND_MEAN
  
  #include &lt;Windows.h&gt;
  #include &lt;BluetoothAPIs.h&gt;
  #include &lt;Winsock2.h&gt;
  #include &lt;Ws2bth.h&gt;
  
  
  
  BOOL auth_callback_ex(LPVOID pvParam, PBLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS authParams)
  {
      BLUETOOTH_AUTHENTICATE_RESPONSE response;
      response.bthAddressRemote = authParams-&gt;deviceInfo.Address;
      response.authMethod = authParams-&gt;authenticationMethod; // == BLUETOOTH_AUTHENTICATION_METHOD_LEGACY
  
      UCHAR pin[] = "1234";
      std::copy(pin, pin+sizeof(pin), response.pinInfo.pin);
      response.pinInfo.pinLength = sizeof(pin)-1; //excluding '\0'
  
      response.negativeResponse = false;
  
  
      HRESULT err = BluetoothSendAuthenticationResponseEx(NULL, &amp;response);
      if (err)
      {
          std::cout &lt;&lt; "BluetoothSendAuthenticationResponseEx error = " &lt;&lt; err &lt;&lt; std::endl;
      }
  
      return true;
  }
  
  
  int main()
  {
      BLUETOOTH_DEVICE_SEARCH_PARAMS btSearchParams;
  
      btSearchParams.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
      btSearchParams.cTimeoutMultiplier = 5;  //5*1.28s search timeout
      btSearchParams.fIssueInquiry = true;    //new inquiry
  
      //return all known and unknown devices
      btSearchParams.fReturnAuthenticated = true;
      btSearchParams.fReturnConnected = true;
      btSearchParams.fReturnRemembered = true;
      btSearchParams.fReturnUnknown = true;
  
      btSearchParams.hRadio = NULL;   //search on all local radios
  
  
  
      BLUETOOTH_DEVICE_INFO btDeviceInfo;
      ZeroMemory(&amp;btDeviceInfo, sizeof(BLUETOOTH_DEVICE_INFO));   //"initialize"
  
      btDeviceInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
  
      HBLUETOOTH_DEVICE_FIND btDeviceFindHandle = NULL;
  
      btDeviceFindHandle = BluetoothFindFirstDevice(&amp;btSearchParams, &amp;btDeviceInfo);
      if(btDeviceFindHandle)
      {
  
          HBLUETOOTH_AUTHENTICATION_REGISTRATION authCallbackHandle = NULL;
  
          DWORD err = BluetoothRegisterForAuthenticationEx(&amp;btDeviceInfo, &amp;authCallbackHandle, &amp;auth_callback_ex, NULL);
  
  
          if (err != ERROR_SUCCESS)
          {
              DWORD err = GetLastError();
              std::cout &lt;&lt; "BluetoothRegisterForAuthentication Error" &lt;&lt; err &lt;&lt; std::endl; 
          }
  
          /////////////// Socket
          WSADATA wsaData;
          err = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
          if (err)
          {
              std::cout &lt;&lt; "WSAStartup error = " &lt;&lt; err &lt;&lt; std::endl;
          }
  
  
          // create BT socket
          SOCKET s = socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
          assert(s != INVALID_SOCKET);    //WSAGetLastError //throw // runtime check release?
  
          SOCKADDR_BTH btSockAddr;
          btSockAddr.addressFamily = AF_BTH;
          btSockAddr.btAddr = btDeviceInfo.Address.ullLong;
          btSockAddr.serviceClassId = RFCOMM_PROTOCOL_UUID; //SerialPortServiceClass_UUID (no difference)
          btSockAddr.port = BT_PORT_ANY;
  
  
          err = connect(s, reinterpret_cast&lt;SOCKADDR*&gt;(&amp;btSockAddr), sizeof(SOCKADDR_BTH));
  
          /* &lt;--- never got so far --&gt; */
  
          if (err)
          {
              DWORD wsaErr = WSAGetLastError();
              std::cout &lt;&lt; "connect error = " &lt;&lt; wsaErr &lt;&lt; std::endl;
  
          }
          else
          {
              //err = shutdown(s, SD_BOTH);
  
              err = closesocket(s);
              if (err)
              {
                  std::cout &lt;&lt; "closesocket error = " &lt;&lt; err &lt;&lt; std::endl;
              }
          }
  
          WSACleanup();
          ///////////////Socket
  
  
          BOOL ok = BluetoothUnregisterAuthentication(authCallbackHandle);
          if (!ok)
          {
              DWORD err = GetLastError();
              std::cout &lt;&lt; "BluetoothUnregisterAuthentication Error" &lt;&lt; err &lt;&lt; std::endl; 
          }
  
  
  
          ok = BluetoothFindDeviceClose(btDeviceFindHandle);
          if (!ok)
          {
              DWORD err = GetLastError();
              std::cout &lt;&lt; "BluetoothDeviceClose Error" &lt;&lt; err &lt;&lt; std::endl; 
          }
      }
      else
      {
          DWORD err = GetLastError();
          std::cout &lt;&lt; "BluetoothFindFirstDevice Error" &lt;&lt; err &lt;&lt; std::endl; 
      }
  
  
      std::cin.get();
  }
  </code></pre>
  
  <p>I have made some few more observations:</p>
  
  <ul>
  <li>The authentication callback and the <strong>BluetoothSendAuthenticationResponseEx</strong> function are working fine, there is no error given back.</li>
  <li>If I do not install the authentication callback (<strong>BluetoothRegisterForAuthenticationEx</strong>) and therefore have to manually enter the PIN (the UI shows up automatically while trying to connect), <strong>connect</strong> function returns properly and everything works fine, too. I even got data (the recv part is omitted in this snippet).</li>
  <li>If I search and pair completely manually (Bluetooth Tray Icon -> Add Device), everything is fine, too. A service and a virtual serial port is installed. Data come via putty.</li>
  </ul>
  
  <p>So somewhere between calling the authentication callback and end of the <strong>connect</strong> function something is going wrong. Maybe when trying to get a certain structure data via a pointer, which should not be NULL, plus offset.</p>
  
  <p>Or am I doing something wrong? Is there something missing?
  Thanks... </p>