2012-02-22 109 views
1

可能重複:
Why does VS2010 give syntax errors when syntax is correct?錯誤C2059 C2061和C語言中

我試圖開發在C語言中的一種的Windows 32的服務,使用Visual Studio 2010

我創建了一個新項目,並插入了.c文件:

  • main.c中
  • service.c
  • misc.c

我也有兩個頭文件:

  • myerrors.h
  • my.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); 

my.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' 

我在一些論壇上檢查了說,這些錯誤通常包含錯誤的錯誤,但我不知道在這種情況下,我不認爲我在包含錯誤的情況下...

任何人都可以照亮我嗎?

謝謝。

+0

小心向我們展示'my.h'? – cnicutar 2012-02-22 20:52:30

+0

@JamesMcNellis:啊,錯過了。就此而言,它是'_Bool'而不是'bool' iirc。 – 2012-02-22 20:54:49

+0

我編輯我的帖子來顯示'my.h',但似乎我會在你的意見中有我的答案:) – 2012-02-22 20:55:57

回答

4

bool不是ANSI C的數據類型,它是語言的版本C99數據類型,只有<stdbool.h>包括,但Visual Studio不支持C99,C89只有(C99還增加了_Bool數據類型,可以在不包含任何頭文件的情況下使用)。

我建議你用另一種類型的替代boolint,或使用typedefintunsigned char或東西別名它。

+0

我用'int'取代了所有'bool',它效果很棒!謝謝 ! – 2012-02-22 21:00:02

+0

@downvoter:爲什麼downvote? – 2012-02-22 21:10:39