【发布时间】:2012-02-22 20:50:06
【问题描述】:
可能重复:
Why does VS2010 give syntax errors when syntax is correct?
我正在尝试使用 Visual Studio 2010 用 C 语言开发一种 Windows 32 服务。
我创建了一个新项目,并插入了 .c 文件:
- main.c
- service.c
- misc.c
我还有两个头文件:
- myerrors.h
- 我的.h
这是我的代码(请注意,这只是一个草稿)。
main.c:
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "my.h"
#include "myerrors.h"
static int parse_args(int ac, char **av)
{
int i = 0;
while (++i < ac)
if (strcmp(av[i], "-i") && !InstallMyService())
return false;
else if (strcmp(av[i], "-d") && !UninstallMyService())
return false;
else if (strcmp(av[i], "-p"))
if (!av[i + 1])
return false;
else
{
if (!InsertPathInRegistry(av[i + 1]))
return false;
i++;
}
else
return false;
return true;
}
int main(int ac, char **av)
{
HANDLE hLogFile;
if ((hLogFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
aff_error(CANT_CREATE_FILE);
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(BAD_ARGUMENTS);
return EXIT_FAILURE;
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{DC_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
getchar();
if (!CloseHandle(hLogFile))
aff_error(CLOSE_FILE_FAILED);
return EXIT_SUCCESS;
}
misc.c:
#include <Windows.h>
#include <stdio.h>
#include "my.h"
#include "myerrors.h"
void aff_error(char *error_str)
{
fprintf(stderr, "ERROR: %s\n", error_str);
}
bool InsertPathInRegistry(char *path)
{
printf("LOG: Inserting %s as ", path);
}
void WriteInLogFile(HANDLE hLogFile, char *log_string)
{
printf("WriteInLogFile function");
}
service.c:
#include <Windows.h>
#include "my.h"
bool InstallMyService()
{
return true;
}
bool UninstallMyService()
{
return true;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
}
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
}
我的标题只是一些函数声明和宏,例如:
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
我的.h
#ifndef _MY_H_
# define _MY_H_
#include <Windows.h>
#include <strsafe.h>
/* Macros */
# define LOG_FILE_PATH "c:\\my_log_file.txt"
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
/* SERVICE functions */
void WINAPI ServiceMain(DWORD ac, LPTSTR *av);
bool InstallMyService();
bool UninstallMyService();
bool InsertPathInRegistry(char *path);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
#endif /*!MY_H_ */
在尝试编译项目时,我遇到了一些奇怪的错误:
my.h(19): error C2061: syntax error : identifier 'InstallMyService'
my.h(19): error C2059: syntax error : ';'
my.h(19): error C2059: syntax error : ')'
或者:
my.h(21): error C2061: syntax error : identifier 'InsertPathInRegistry'
my.h(21): error C2059: syntax error : ';'
my.h(21): error C2059: syntax error : 'type'
我在一些论坛上查看说这些错误通常是包含错误放置的错误,但我真的不知道在这种情况下,我不认为我在包含错误时犯了错误......
谁能照亮我?
谢谢。
【问题讨论】:
-
想告诉我们
my.h? -
@JamesMcNellis:啊,错过了。就此而言,它是
_Bool而不是booliirc。 -
我编辑了我的帖子以显示
my.h但似乎我会在你的 cmets 中得到我的答案:) -
@BillyONEal:是的。
_Bool是新的 C99 基本布尔类型的名称。<stdbool.h>将bool定义为_Bool的同义词。 -
好像@JamesMcNellis 明白了:-)
标签: c windows visual-studio-2010 visual-c++ compiler-errors