[英]Error when compiling C code with CreateEvent call
如果我在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定義的文件。
歡迎所有建議。
在C語言中,您可以在塊的開頭聲明所有變量。
移動您的deviceEventHandle = CreateEvent(NULL, FALSE, FALSE, "foundDevice");
到BTUINT變量塊之后。
編譯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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.