Problem starting a thread from a system service

This is a discussion on Problem starting a thread from a system service within the Windows Programming forums, part of the Platform Specific Boards category; Using Visual Studio 2005 Im having a problem getting a thread started using the standard CreateThread() call. Im using the ...

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

    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]
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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,189
    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 02:15 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 06:22 PM
  2. Remote thread problem
    By RubbeR DuckY in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2006, 01:24 PM
  3. Directory and System Problem
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2006, 01: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, 09:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21