1

Why is it that this code will work if I comment out all of the boost parts, but when I include them, the debugger doesn't seem to recognize the code relating to bluetooth?

#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;
}

The debugger gives me:

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 ==========
emartel
  • 7,712
  • 1
  • 30
  • 58
got it--thanks
  • 233
  • 2
  • 9
  • Just a guess. Looks like the compiler error is due to `_WIN32_WINNT` not being defined, or not defined correctly. I would add a definition for this in the project settings, and then move the `#include ` to the very beginning, before you include anything else. Under x64 builds you may also have problems if `WIN64` isn't defined, so you'll need to add a project settings definition for that as well (just for x64 builds). – John Dibling Nov 26 '12 at 18:53

3 Answers3

0

Try pushing all the "using namespace" lines after all the "#include" ones

On Freund
  • 4,376
  • 2
  • 23
  • 30
0

Best to include c than c++ then 3rd party then your own headers in that order (except in cc files include your own declaration header first). In this case there is probably a definition that asio is checking for that's interfering. winsock stuff is a good place to check for a start. I think you will find this could be the problem (or very close to it) Boost::asio winsock and winsock 2 compatibility issue

These issues are common with headers that include windows.h along the line.

Community
  • 1
  • 1
dirvine
  • 57,399
  • 2
  • 18
  • 19
  • Some would argue that it's best to do it the other way around: http://stackoverflow.com/questions/2762568/c-c-include-file-order-best-practices – On Freund Nov 26 '12 at 19:38
  • Yes this is arguable I suppose, I think hidden dependencies though could be bad. I think the include what you use paradigm is the safest. Google style guide has the opposing argument to some of these http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Names_and_Order_of_Includes#Names_and_Order_of_Includes I think it may be best that you don't hide requirements although it is possibly a personal preference. The stackoverflow question seems to make a good point, but not the as long as it compiles argument :-) – dirvine Nov 27 '12 at 00:45
  • Okay I moved the boost::asio after the bluetooth ones, and I don't get those errors, anymore. But now it's throwing an exception: Unhandled exception at at 0x760DC41F in test.exe: Microsoft C++ exception: boost::exception_detail::clone_impl > at memory location 0x0040F068. – got it--thanks Nov 27 '12 at 02:24
  • I think you may have to check if the Bluetooth headers also have some windows.h requirement. There's probably nothing for it but to traverse these headers to find what they try to include. I am sorry but it's hard to know what that header contains. It could be that you actually do need to define the windows version as the compiler is saying. This will depend on what version of windows you are using , maybe this will help http://msdn.microsoft.com/en-gb/library/windows/desktop/aa383745(v=vs.85).aspx – dirvine Nov 27 '12 at 02:46
0

wild guess:

with asio you pull in windows header files (needed for sockets) that have the nasty min max macros defined that collide with STL min/max. you have to define NOMINMAX before including boost asio.

stefan
  • 3,681
  • 15
  • 25