Thread: Problem starting a thread from a system service

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Problem starting a thread from a system service

    Using Visual Studio 2005 Im having a problem getting a thread started using the standard CreateThread() call. Im using the included system service template. In the section -

    Code:
    virtualvoid OnStart(array<String^>^ args) override
    {
    // TODO: Add code here to start your service.
    DWORD ThreadId;
    HANDLE hThread = CreateThread(NULL , 0 , (LPTHREAD_START_ROUTINE)ServiceThread , NULL , 0 , (LPDWORD)&ThreadId);
    }
     
    


    The compiler throws the error
    1>c:\blah blah blah\Cluster Node ServiceWinService.h(3) : error C3641: 'ServiceThread' : invalid calling convention '__stdcall ' for function compiled with /clrure or /clr:safe

    I havent worked with system services before. All I really need to do is get a thread running so it can handle some network connectivity and I/O stuff. I need to run it as a system service because it has to run even when noone is logged on.
    [/SIZE]

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    add "__cdecl" to your function declaration, e.g.
    Code:
    __cdecl void ServiceThread(void *p) ....
    The calling convention in a Service, apparently, is "__stdcall". It is different from __cdecl in that __cdecl pushes arguments from right to left, __stdcall from left to right. Arguments are cleared by the calling function in __cdecl, whilst it's cleared up by the callee in the __stdcall convention.

    --
    Mats

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    gah, dug into it and found that -

    Code:
    DWORD WINAPI MyFunc(LPVOID);
    translates into
    Code:
    unsigned long __stdcall MyFunc();
    seems WINAPI is #define WINAPI __stdcall

    removing the WINAPI and __cdecl fixed the problem. Thanks for your help, if I hadnt tried your suggestion I wouldnt have found the solution

    we need a points system here like they have on some other BB's

    BTW, when it switches the POP order, it doest change the endian-ness of the variables as they are stored in memory does it?
    Last edited by abachler; 08-23-2007 at 01:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Remote thread problem
    By RubbeR DuckY in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2006, 12:24 PM
  3. Directory and System Problem
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2006, 12:32 PM
  4. GTK+ Gnome desktop system tray app problem
    By BobS0327 in forum Linux Programming
    Replies: 2
    Last Post: 04-01-2006, 09:54 PM
  5. system() problem
    By Raulica in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2005, 08:38 PM