使用CreateEvent调用编译C代码时出错

时间:2011-09-14 08:24:31

标签: c compiler-errors

如果在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"

我在Visual Studio 2005中将代码编译为C代码(/ TC)。“Btsdk_ui.h”文件是BlueSoleil蓝牙堆栈的一部分,并包含另一个包含BTUINT32和BTUINT16定义的文件。

欢迎所有建议。

2 个答案:

答案 0 :(得分:3)

在C中,您在块的开头声明所有变量。

将您的deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");移到您的BTUINT变量块之后。

答案 1 :(得分:1)

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


}