[Win32utils-devel] [Fwd: [ruby] win32-service : RegisterServiceCtrlHandlerEx]

Patrick Hurley phurley at gmail.com
Wed Jun 28 09:54:23 EDT 2006


Since the proof is in the pudding, below is some code to show what I
meant. The down side to this approach is that it requires us to put
some "nasty" constants in our code (stuff that can only be known by
looking in a dev studio header file or some serious reverse
engineering). As there is really no way Microsoft can change these
constants without breaking loads of code it is not a problem, but it
does go against the grain.

If this is a desired approach (it could be a while before we get a
Ruby compiling on a newer tool chain), I would be happy to make a
patch that also softens up the SERVICE_CONTROL_PARAMCHANGE,
SERVICE_CONTROL_NETBINDXXX etc. and HAVE_ENUMSERVICESSTATUSEX. As
these are API (windows version) issues and not really tied to the
compiler we can support them as long as the version of windows does. I
will wait until you complete the revision separating service code from
daemon code, as that is big enough to wreck an easy patch.

pth

#include <stdio.h>
#include <windows.h>

typedef DWORD (WINAPI *LPHANDLER_FUNCTION_EX) (
  DWORD dwControl,     // requested control code
  DWORD dwEventType,   // event type
  LPVOID lpEventData,  // event data
  LPVOID lpContext     // user-defined context data
);

typedef SERVICE_STATUS_HANDLE (WINAPI *REGISTER_SERVICE_CTRL_HANDLER_EX) (
   LPCTSTR lpServiceName,                // name of service
   LPHANDLER_FUNCTION_EX lpHandlerProc,  // handler function
   LPVOID lpContext                      // user data
);

DWORD WINAPI MyCallBack(DWORD dwControl, DWORD dwEventType, LPVOID
lpEventData, LPVOID lpContext)
{
   return 0;
}

int main()
{
   HMODULE hAdvapi32 = NULL;
   REGISTER_SERVICE_CTRL_HANDLER_EX RegisterServiceCtrlHandlerEx = NULL;

   if ((hAdvapi32 = LoadLibrary("Advapi32.dll")) == NULL)
   {
      printf("Unable to load Advapi32.dll\n");
      exit(1);
   }

   if ((RegisterServiceCtrlHandlerEx =
(REGISTER_SERVICE_CTRL_HANDLER_EX)GetProcAddress(hAdvapi32,
"RegisterServiceCtrlHandlerExA")) == NULL)
   {
      printf("Unable to locate RegisterServiceCtrlHandlerEx\n");
      exit(2);
   }

   printf("It's all good\n");
   RegisterServiceCtrlHandlerEx("ServName", MyCallBack, 0);

   return 0;
}


More information about the win32utils-devel mailing list