Error C2275: Illegal use of this type as an expression

Since yesterday, I encountered a compilation error for my C project. The project itself consists in creating a service that will perform some tasks.

I am not what has changed since yesterday, but this morning my code is no longer compiling.

Here are the errors that I have:

c:\path\main.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression c:\program files\microsoft sdks\windows\v7.0a\include\winsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY' c:\path\main.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable' c:\path\main.c(56): error C2065: 'DispatchTable' : undeclared identifier c:\path\main.c(56): error C2059: syntax error : ']' c:\path\main.c(57): error C2065: 'DispatchTable' : undeclared identifier c:\path\main.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int' c:\path\main.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal and actual parameter 1 

Here is the code associated with these errors (from lines 45-58):

 int main(int ac, char *av[]) { if (ac > 1) { if (!parse_args(ac, av)) { aff_error(ARGUMENTS); return EXIT_FAILURE; } } SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}}; StartServiceCtrlDispatcher(DispatchTable); return EXIT_SUCCESS; } 

And here is the code of my ServiceMain function:

 void WINAPI ServiceMain(DWORD ac, LPTSTR *av) { gl_ServiceStatus.dwServiceType = SERVICE_WIN32; gl_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; gl_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; gl_ServiceStatus.dwWin32ExitCode = 0; gl_ServiceStatus.dwServiceSpecificExitCode = 0; gl_ServiceStatus.dwCheckPoint = 0; gl_ServiceStatus.dwWaitHint = 0; gl_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler); if (gl_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) return; gl_ServiceStatus.dwCurrentState = SERVICE_RUNNING; gl_ServiceStatus.dwCheckPoint = 0; gl_ServiceStatus.dwWaitHint = 0; SetServiceStatus(gl_ServiceStatusHandle, &gl_ServiceStatus); } 

I was not able to find the answers that fit my problem, can anyone help? Thank!

+49
c winapi visual-studio-2010 compiler-errors service
Mar 28 '12 at 8:18
source share
4 answers

When you name the source *.c files, MSVC assumes compiling C, which means C89. All block-local variables must be declared at the beginning of the block.

Temporary solutions:

  • Declaring / initializing all local variables at the beginning of a code block (immediately after opening the bracket { )
  • rename the source files to *.cpp or equivalent and compile as C ++.
  • upgrade to VS 2013, which mitigates this limitation .
+118
Mar 28 '12 at 8:25
source share

Perhaps you are using a version of C that does not allow you to declare variables in the middle of a block. C used to declare variables at the top of a block after opening {and before executing operations.

+5
Mar 28 '12 at 8:25
source share

Place brackets around the code in which the variable is used.

In your case, this means:

 if (ac > 1) { if (!parse_args(ac, av)) { aff_error(ARGUMENTS); return EXIT_FAILURE; } } { SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}}; StartServiceCtrlDispatcher(DispatchTable); } 
+2
Aug 20 '13 at
source share

This error occurred when transferring a project from one installation to another (VS2015 => VS2010).
The C code was actually compiled as C ++ on the source computer, on the target machine, the "Default" parameter in Project Properties\C/C++\Advanced\Compile as somehow pointed to C, although the source file was of type * .cpp.
In my small program, errors occurred regarding the placement of certain types in the code, for example. HWND and HRESULT , as well as in different formats of for and C ++ loops, such as LPCTSTR, size_t , StringCbPrintf and BOOL . Comparison
Changing Compile As from Default to Compile as C++ Code (/TP) enabled it.

+2
Oct. 15 '15 at 6:24
source share



All Articles