Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct type cast to avoid a compiler warning using SERVICE_TABLE_ENTRY from winsvc.h
    primarykey
    data
    text
    <p>When trying to set the SERVICE_TABLE_ENTRY using the example at <a href="https://stackoverflow.com/questions/41699/creating-windows-service-without-visual-studio">Creating Windows service without Visual Studio</a> and compiling using the mingw32 cross-compiler on Fedora 14 I get a compiler warning.</p> <p>The shortest I can make the sample is</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;windows.h&gt; #include &lt;winsvc.h&gt; #define MY_SVC_NAME "My Service" int cont_running = 1; DWORD WINAPI ServiceHandlerProc(DWORD ControlCode, DWORD a, void *b, void *c) { switch (ControlCode) { case SERVICE_CONTROL_STOP : ; cont_running = 0; } return 0; } void WINAPI ServiceMain(int argc, char *argv[], char *envp[]) { int hServiceStatus; hServiceStatus = RegisterServiceCtrlHandlerEx(MY_SVC_NAME, ServiceHandlerProc, 0); } int main(int argc, char *argv[], char *envp[]) { SERVICE_TABLE_ENTRY ServiceStartTable[] = { { MY_SVC_NAME, ServiceMain }, { 0, 0 } }; } </code></pre> <p>but the compiler is complaining about MY_SVC_NAME being the wrong type.</p> <pre><code>$ /usr/bin/i686-pc-mingw32-gcc -o /tmp/test ~/c/sample.c sample.c: In function 'main': sample.c:31:9: warning: initialization from incompatible pointer type </code></pre> <p>I've tried assigning a variable and using type LPSTR LPWSTR char * and char [] and also tried casting the type within the curly braces, but its not working.</p> <p>Looking at /usr/i686-pc-mingw32/sys-root/mingw/include/winsvc.h I see its defined as</p> <pre><code>typedef struct _SERVICE_TABLE_ENTRYA { LPSTR lpServiceName; LPSERVICE_MAIN_FUNCTIONA lpServiceProc; } SERVICE_TABLE_ENTRYA,*LPSERVICE_TABLE_ENTRYA; typedef struct _SERVICE_TABLE_ENTRYW { LPWSTR lpServiceName; LPSERVICE_MAIN_FUNCTIONW lpServiceProc; } SERVICE_TABLE_ENTRYW,*LPSERVICE_TABLE_ENTRYW; ... typedef SERVICE_TABLE_ENTRYW SERVICE_TABLE_ENTRY,*LPSERVICE_TABLE_ENTRY; ... typedef SERVICE_TABLE_ENTRYA SERVICE_TABLE_ENTRY,*LPSERVICE_TABLE_ENTRY; </code></pre> <p>So surely using</p> <pre><code>LPSTR my_svc=MY_SVC_NAME; SERVICE_TABLE_ENTRY ServiceStartTable[] = { { my_svc, ServiceMain }, { 0, 0 } }; </code></pre> <p>Should work ???</p> <p>Sorry, another search revealed this example code at <a href="https://stackoverflow.com/questions/7734763/windows-service-c">Windows Service C</a></p> <p>I changed my code to</p> <pre><code> SERVICE_TABLE_ENTRY ServiceStartTable[2]; ServiceStartTable[0].lpServiceName = MY_SVC_NAME; ServiceStartTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceStartTable[1].lpServiceName = NULL; ServiceStartTable[1].lpServiceProc = NULL; </code></pre> <p>and now it compiles without warnings.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload