Ошибка при компиляции кода C с вызовом CreateEvent - PullRequest
0 голосов
/ 14 сентября 2011

Я получаю несколько ошибок компиляции, если компилирую следующую функцию в 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;
    }
}

Вот ошибки, которые я получаю:

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  

Если я закомментирую строку, содержащую вызов CreateEvent, код компилируется без ошибок:

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


}

Вот заголовки, которые я использую:

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

Я компилирую код как код C (/ TC) в Visual Studio 2005. Файл "Btsdk_ui.h" является частью стека Bluetooth BlueSoleil и включает в себя другой файл, который содержит определения BTUINT32 и BTUINT16.

Все предложения приветствуются.

Ответы [ 2 ]

3 голосов
/ 14 сентября 2011

В C вы объявляете все свои переменные в начале блока.

Переместите deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice"); на после вашего блока переменных BTUINT.

1 голос
/ 14 сентября 2011

При компиляции кода C MSVC не допускает смешивания объявлений с операторами - объявления могут быть только в начале блока (MSVC более близко соответствует стандарту ANSI / ISO C90, чем стандарту C99). *

Попытка:

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


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...