sdfqwerqaz1
sdfqwerqaz1

Reputation: 1527

Error when compiling C code with CreateEvent call

I get several compile errors if I compile the following function in Visual Studio 2005:

void search()
{
    deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");

    BTUINT32 deviceClass = 0; // 0 represents all classes 
    BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
    BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
    Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);

    WaitForSingleObject(deviceEventHandle, INFINITE);

    if (deviceEventHandle != NULL) {
        CloseHandle(deviceEventHandle);
        deviceEventHandle = NULL;
    }
}

These are the errors I get:

error C2275: 'BTUINT32' : illegal use of this type as an expression 
error C2146: syntax error : missing ';' before identifier 'deviceClass'     
error C2065: 'deviceClass' : undeclared identifier
error C2275: 'BTUINT16' : illegal use of this type as an expression     
error C2146: syntax error : missing ';' before identifier 'maxDevices'      
error C2065: 'maxDevices' : undeclared identifier   
error C2275: 'BTUINT16' : illegal use of this type as an expression     
error C2146: syntax error : missing ';' before identifier 'maxDuration'     
error C2065: 'maxDuration' : undeclared identifier  

If I comment out the line containing the CreateEvent call the code compiles without errors:

void search()
{
    //deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");

    BTUINT32 deviceClass = 0; // 0 represents all classes 
    BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
    BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds
    Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);

    WaitForSingleObject(deviceEventHandle, INFINITE);

    if (deviceEventHandle != NULL) {
        CloseHandle(deviceEventHandle);
        deviceEventHandle = NULL;
    }


}

These are the headers I use:

#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include "Btsdk_ui.h"

I compile the code as C code (/TC) in Visual Studio 2005. The "Btsdk_ui.h" file is part of the BlueSoleil bluetooth stack and inlcudes another file that contains definitions of BTUINT32 and BTUINT16.

All suggestions are welcome.

Upvotes: 0

Views: 324

Answers (2)

Michael Burr
Michael Burr

Reputation: 340306

When compiling C code, MSVC doesn't allow declarations to be mixed with statements - declarations can only be at the start of a block (MSVC follows more closely to the ANSI/ISO C90 standard than the C99 standard).

Try:

void search()
{

    BTUINT32 deviceClass = 0; // 0 represents all classes 
    BTUINT16 maxDevices = 200; // 0 represents an unlimited number of responses
    BTUINT16 maxDuration = 45; // maxDuration * 1.28 = number of seconds

    deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");

    Btsdk_StartDeviceDiscovery(deviceClass, maxDevices, maxDuration);

    WaitForSingleObject(deviceEventHandle, INFINITE);

    if (deviceEventHandle != NULL) {
        CloseHandle(deviceEventHandle);
        deviceEventHandle = NULL;
    }


}

Upvotes: 1

Kaos
Kaos

Reputation: 1004

In C, you declare all your variables at the beginning of the block.

Move your deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice"); to after your block of BTUINT variables.

Upvotes: 3

Related Questions