如果我注释掉所有的 boost 部分,为什么这段代码会起作用,但是当我包含它们时,调试器似乎无法识别与蓝牙相关的代码?
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <boost/asio.hpp>
using namespace::boost::asio;
// These two require ws2_32.lib
#include <Winsock2.h>
#include <Ws2bth.h>
// This one requires Bthprops.lib
#include <BluetoothAPIs.h>
using namespace std;
BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {sizeof(BLUETOOTH_FIND_RADIO_PARAMS)};
BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL };
BLUETOOTH_DEVICE_INFO m_device_info = { sizeof(BLUETOOTH_DEVICE_INFO), 0, };
// Note:
// Radio - is the thing plugged in/attached to the local machine.
// Device - is the thing that is connected to via the Bluetooth connection.
int main()
{
HANDLE m_radio = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;
int m_device_id;
serial_port_base::baud_rate BAUD(9600);
serial_port_base::character_size CSIZE( 8 );
serial_port_base::flow_control FLOW( serial_port_base::flow_control::none );
serial_port_base::parity PARITY( serial_port_base::parity::none );
serial_port_base::stop_bits STOP( serial_port_base::stop_bits::one );
io_service io;
serial_port port( io, "COM3" );
port.set_option( BAUD );
port.set_option( CSIZE );
port.set_option( FLOW );
port.set_option( PARITY );
port.set_option( STOP );
unsigned char command[1] = {0};
bool change = false;
bool checker;
while( true ) {
do
{
ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);
// Next for every radio, get the device
m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);
system("cls");
if(m_bt_dev != NULL)
cout << "\nBluetoothFindFirstDevice() is working!\n";
else
cout << "\nBluetoothFindFirstDevice() failed with error code " << GetLastError() << endl;
m_device_id = 0;
checker = m_device_info.fConnected;
// Get the device info
do
{
char deviceName[248];
char deviceName2[248] = "SCH-R920";
for( int i = 0; i < 248; i++ )
{
deviceName[i] = (char) m_device_info.szName[i];
}
if( !strcmp( deviceName, deviceName2 ) )
{
cout << "\nDevice " << m_device_id << ":";
wcout << "\n\tInstance Name: " << m_device_info.szName;
cout << "\n\tAddress: " << hex << uppercase << static_cast<unsigned>(m_device_info.Address.rgBytes[5]) << ":" <<
static_cast<unsigned>(m_device_info.Address.rgBytes[4]) << ":" << static_cast<unsigned>(m_device_info.Address.rgBytes[3]) << ":" <<
static_cast<unsigned>(m_device_info.Address.rgBytes[2]) << ":" << static_cast<unsigned>(m_device_info.Address.rgBytes[1]) << ":" <<
static_cast<unsigned>(m_device_info.Address.rgBytes[0]); // need to learn to do templates
cout << "\n\tClass: 0x00" << nouppercase << m_device_info.ulClassofDevice; // template for the two 0's in quotes
cout << "\n\tConnected: " << ( m_device_info.fConnected != 0 ? "True" : "False" );
cout << "\n\tAuthenticated: " << ( m_device_info.fAuthenticated != 0 ? "True" : "False" );
cout << "\n\tRemembered: " << ( m_device_info.fRemembered != 0 ? "True" : "False" );
}
else
m_device_id++;
} while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
if( m_device_info.fConnected == checker )
{
change = true;
}
// NO more device, close the device handle
if(BluetoothFindDeviceClose(m_bt_dev) == TRUE)
wcout << "\nBluetoothFindDeviceClose(m_bt_dev) is OK!\n";
else
cout << "\nBluetoothFindDeviceClose(m_bt_dev) failed with error code " << GetLastError() << endl;
} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
if( change )
{
if( m_device_info.fConnected != 0 )
{
cout << "It's connected";
cin.get();
//write( port, buffer( '1', 1 ) );
}
else
{
cout << "It's not connected";
cin.get();
//write( port, buffer( '0', 1 ) );
}
}
change = false;
}
std::cout << "\n\n_____________________________\n"
<< "Press any key to exit....";
std::cin.get();
return 0;
}
调试器给了我:
1>------ Build started: Project: Winsock, Configuration: Debug Win32 ------
1> Source.cpp
1> Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
1> - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
1> - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
1> Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(20): error
C2146: syntax error : missing ';' before identifier 'm_bt_find_radio'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(21): error C2146: syntax error : missing ';' before identifier 'm_search_params'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(23): error C2078: too many initializers
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C2146: syntax error : missing ';' before identifier 'm_device_info'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(30): error C2078: too many initializers
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2065: 'HBLUETOOTH_DEVICE_FIND' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2146: syntax error : missing ';' before identifier 'm_bt_dev'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(40): error C2065: 'm_bt_dev' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(65): error C2228: left of '.dwSize' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(68): error C2065: 'm_bt_dev' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(68): error C3861: 'BluetoothFindFirstDevice': identifier not found
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(70): error C2065: 'm_bt_dev' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(76): error C2228: left of '.fConnected' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(85): error C2228: left of '.szName' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(91): error C2228: left of '.szName' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(92): error C2228: left of '.Address' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(92): error C2228: left of '.rgBytes' must have class/struct/union
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(93): error C2228: left of '.Address' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(93): error C2228: left of '.rgBytes' must have class/struct/union
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(94): error C2228: left of '.Address' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(94): error C2228: left of '.rgBytes' must have class/struct/union
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(95): error C2228: left of '.Address' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(95): error C2228: left of '.rgBytes' must have class/struct/union
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(96): error C2228: left of '.ulClassofDevice' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(97): error C2228: left of '.fConnected' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(98): error C2228: left of '.fAuthenticated' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(99): error C2228: left of '.fRemembered' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(104): error C2065: 'm_bt_dev' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(104): error C3861: 'BluetoothFindNextDevice': identifier not found
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(106): error C2228: left of '.fConnected' must have class/struct/union
1> type is 'int'
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(112): error C2065: 'm_bt_dev' : undeclared identifier
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(112): error C3861: 'BluetoothFindDeviceClose': identifier not found
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(116): error C3861: 'BluetoothFindNextRadio': identifier not found
1>c:\users\sequoia\documents\visual studio 2012\projects\winsock\winsock\source.cpp(120): error C2228: left of '.fConnected' must have class/struct/union
1> type is 'int'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========