现想做一个类似win2000的服务进程,请多多指教!!!

解决方案 »

  1.   

    MSDN光盘下面的
    Samples\VC98\sdk\winbase\winnt\service有sdk源码。
      

  2.   

    MSDN里有 你去看看例子 你可以查找service
      

  3.   


    typedef DWORD (WINAPI *REGSERVPROC)(DWORD, DWORD);
    HINSTANCE hLibrary;
    REGSERVPROC regproc;
    hLibrary = LoadLibrary("kernel32.dll");
    regproc = (REGSERVPROC) GetProcAddress(hLibrary, "RegisterServiceProcess");
    if((int)GetVersion() < 0)
         regproc (NULL,1);
      

  4.   

    是把.exe文件还是.dll文件注册?如是.dll怎么生成???
      

  5.   

    小弟的mail: [email protected]
    如有解决之道,请告知,谢谢,关注
      

  6.   


    //使用函数指针
    typedef DWORD (__stdcall * LPREGSERVICEPROC)(DWORD,DWORD);  
    LPREGSERVICEPROC lpfnRegServiceProc=NULL;
    HKEY hKey=NULL;
    DWORD dwDisposition ;
    DWORD dwValueType=REG_SZ;
    DWORD dwStrcb=MAX_PATH;
    CString pSubkey=TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices");
      if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,LPCTSTR(pSubkey),0,NULL,
    REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDispo     sition)!=ERROR_SUCCESS)
     {
              return ;
     }
     TCHAR FileName[MAX_PATH],Path[MAX_PATH];
     GetModuleFileName(NULL,Path,MAX_PATH);
     GetFileTitle(Path,FileName,MAX_PATH); 
     if(RegQueryValueEx(hKey,FileName,0,&dwValueType,(LPBYTE)               Path,&dwStrcb)!=ERROR_SUCCESS)
     {
           RegSetValueEx(hKey,FileName,0,REG_SZ,(CONST BYTE *)            Path,dwStrcb);
     }
      if(hKey!=NULL)
        RegCloseKey(hKey);
     lpfnRegServiceProc=(LPREGSERVICEPROC)GetProcAddress    
            (GetModuleHandle(TEXT("Kernel32.dll")),
        TEXT("RegisterServiceProcess"));
      if(lpfnRegServiceProc!=NULL) 
     {
        if(lpfnRegServiceProc(NULL,1)==0)
        {
      AfxMessageBox("can't regiser service process!");
        }
     }
      

  7.   

    http://www.naughter.com/serv.html
    CNTService v1.21 A class framework for developing NT services in MFC
      

  8.   

    我想使用以下的代码install,请帮忙指教!以下是main.cpp// main.cpp,v 1.5 2001/04/21 22:43:23 coryan Exp// ============================================================================
    //
    // = LIBRARY
    //    examples/NT_Service
    //
    // = FILENAME
    //    main.cpp
    //
    // = DESCRIPTION
    //    This is the main program - it just hands control off to the
    //    process instance to figure out what to do.  This program only
    //    runs on Win32.
    //
    // = AUTHOR
    //    Gonzalo Diethelm <[email protected]>
    //    and Steve Huston <[email protected]>
    //
    // ============================================================================#include "ace/Get_Opt.h"
    #include "ntsvc.h"// Default for the -i (install) option
    #define DEFAULT_SERVICE_INIT_STARTUP     SERVICE_AUTO_STARTclass Process
    {
    public:
      Process (void);
      ~Process (void);  int run(int argc, ACE_TCHAR* argv[]);private:
      void parse_args (int argc,
                       ACE_TCHAR* argv[]);
      void print_usage_and_die (void);private:
      char progname[128];  int opt_install;
      int opt_remove;
      int opt_start;
      int opt_kill;
      int opt_type;
      int opt_debug;  int opt_startup;
    };typedef ACE_Singleton<Process, ACE_Mutex> PROCESS;Process::Process (void)
      : opt_install (0),
        opt_remove (0),
        opt_start (0),
        opt_kill (0),
        opt_type (0),
        opt_debug (0),
        opt_startup (0)
    {
      ACE_OS::strcpy (progname,
                      "service");
      ACE::init ();
    }Process::~Process (void)
    {
      ACE::fini ();
    }void
    Process::print_usage_and_die (void)
    {
      ACE_DEBUG ((LM_INFO,
                  "Usage: %s"
                  " -in -r -s -k -tn -d\n"
                  "  -i: Install this program as an NT service, with specified startup\n"
                  "  -r: Remove this program from the Service Manager\n"
                  "  -s: Start the service\n"
                  "  -k: Kill the service\n"
                  "  -t: Set startup for an existing service\n"
                  "  -d: Debug; run as a regular application\n",
                  progname,
                  0));
      ACE_OS::exit(1);
    }void
    Process::parse_args (int argc, ACE_TCHAR* argv[])
    {
      ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("i:rskt:d"));
      int c;  while ((c = get_opt ()) != -1)
        switch (c)
        {
        case 'i':
          opt_install = 1;
          opt_startup = ACE_OS::atoi (get_opt.optarg);
          if (opt_startup <= 0)
             print_usage_and_die ();
          break;
        case 'r':
          opt_remove = 1;
          break;
        case 's':
          opt_start = 1;
          break;
        case 'k':
          opt_kill = 1;
          break;
        case 't':
          opt_type = 1;
          opt_startup = ACE_OS::atoi (get_opt.optarg);
          if (opt_startup <= 0)
             print_usage_and_die ();
          break;
        case 'd':
          opt_debug = 1;
          break;
        default:
          // -i can also be given without a value - if so, it defaults
          // to defined value.
          if (ACE_OS::strcmp (get_opt.argv_[get_opt.optind-1], ACE_TEXT ("-i")) == 0)
          {
            opt_install = 1;
            opt_startup = DEFAULT_SERVICE_INIT_STARTUP;
          }
          else
          {
            print_usage_and_die ();
          }
          break;
      }
    }// Define a function to handle Ctrl+C to cleanly shut this down.static BOOL __stdcall
    ConsoleHandler (DWORD ctrlType)
    {
      ACE_UNUSED_ARG (ctrlType);
      SERVICE::instance ()->handle_control (SERVICE_CONTROL_STOP);
      return TRUE;
    }ACE_NT_SERVICE_DEFINE (Beeper,
                           Service,
                           ACE_TEXT ("Annoying Beeper Service"));int
    Process::run (int argc, ACE_TCHAR* argv[])
    {
      SERVICE::instance ()->name (ACE_TEXT ("Beeper"),
                                  ACE_TEXT ("Annoying Beeper Service"));  parse_args (argc, argv);  if (opt_install && !opt_remove)
        return SERVICE::instance ()->insert (opt_startup);  if (opt_remove && !opt_install)
        return SERVICE::instance ()->remove ();  if (opt_start && opt_kill)
        print_usage_and_die ();  if (opt_start)
        return SERVICE::instance ()->start_svc ();  if (opt_kill)
        return SERVICE::instance ()->stop_svc ();  if (opt_type)
        return SERVICE::instance ()->startup (opt_startup);  // If we get here, we either run the app in debug mode (-d) or are
      // being called from the service manager to start the service.  if (opt_debug)
        {
          SetConsoleCtrlHandler (&ConsoleHandler, 1);
          SERVICE::instance ()->svc ();
        }
      else
        {
          ACE_NT_SERVICE_RUN (Beeper,
                              SERVICE::instance (),
                              ret);
          if (ret == 0)
            ACE_ERROR ((LM_ERROR,
                        "%p\n",
                        "Couldn't start service"));
        }  return 0;
    }int
    main (int argc, ACE_TCHAR* argv[])
    {
      return PROCESS::instance ()->run (argc, argv);
    }#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
    template class ACE_Singleton<Process, ACE_Mutex>;
    #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
    #pragma instantiate ACE_Singleton<Process, ACE_Mutex>
    #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
      

  9.   


    以下是ntsvc.cpp
    // ntsvc.cpp,v 1.3 2001/04/21 22:43:23 coryan Exp// ============================================================================
    //
    // = LIBRARY
    //    examples/NT_Service
    //
    // = FILENAME
    //    ntsvc.cpp
    //
    // = DESCRIPTION
    //      This is the implementation of the NT service.  It beeps every 2
    //      seconds until the service is stopped.
    //
    // = AUTHOR
    //    Gonzalo Diethelm <[email protected]>
    //    and Steve Huston <[email protected]>
    //
    // ============================================================================#include "ace/OS.h"
    #include "ace/Reactor.h"
    #include "ntsvc.h"Service::Service (void)
    {
      // Remember the Reactor instance.
      reactor (ACE_Reactor::instance ());  // Schedule a timer every two seconds.
      ACE_Time_Value tv (2, 0);
      ACE_Reactor::instance ()->schedule_timer (this, 0, tv, tv);
    }// This method is called when the service gets a control request.  It
    // handles requests for stop and shutdown by calling terminate ().
    // All others get handled by calling up to inherited::handle_control.void
    Service::handle_control (DWORD control_code)
    {
      if (control_code == SERVICE_CONTROL_SHUTDOWN
          || control_code == SERVICE_CONTROL_STOP)
        {
          report_status (SERVICE_STOP_PENDING);      ACE_DEBUG ((LM_INFO,
                      "Service control stop requested\n"));
          stop_ = 1;
          reactor ()->notify (this,
                              ACE_Event_Handler::EXCEPT_MASK);
        }
      else
        inherited::handle_control (control_code);
    }// This is just here to be the target of the notify from above... it
    // doesn't do anything except aid on popping the reactor off its wait
    // and causing a drop out of handle_events.int
    Service::handle_exception (ACE_HANDLE)
    {
      return 0;
    }// Beep every two seconds.  This is what this NT service does...int
    Service::handle_timeout (const ACE_Time_Value &tv,
                             const void *)
    {
      ACE_UNUSED_ARG (tv);
      MessageBeep (MB_OK);
      return 0;
    }// This is the main processing function for the Service.  It sets up
    // the initial configuration and runs the event loop until a shutdown
    // request is received.int
    Service::svc (void)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Service::svc\n"));  // As an NT service, we come in here in a different thread than the
      // one which created the reactor.  So in order to do anything, we
      // need to own the reactor. If we are not a service, report_status
      // will return -1.
      if (report_status (SERVICE_RUNNING) == 0)
        reactor ()->owner (ACE_Thread::self ());  stop_ = 0;  while (!stop_)
        reactor ()->handle_events ();  // Cleanly terminate connections, terminate threads.
      ACE_DEBUG ((LM_DEBUG,
                  "Shutting down\n"));
      return 0;
    }#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
    template class ACE_Singleton<Service, ACE_Mutex>;
    #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
    #pragma instantiate ACE_Singleton<Service, ACE_Mutex>
    #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
    以下是ntsvc.h
    /* -*- C++ -*- */
    // ntsvc.h,v 1.2 1999/01/11 18:57:34 schmidt Exp// ============================================================================
    //
    // = LIBRARY
    //    examples/NT_Service
    //
    // = FILENAME
    //    ntsvc.h
    //
    // = DESCRIPTION
    //      This is the definition of the sample NT Service class.  This example
    //      only runs on Win32 platforms.
    //
    // = AUTHOR
    //    Gonzalo Diethelm and Steve Huston
    //
    // ============================================================================#ifndef NTSVC_H_
    #define NTSVC_H_#include "ace/Event_Handler.h"
    #include "ace/NT_Service.h"
    #include "ace/Singleton.h"
    #include "ace/Synch.h"class Service : public ACE_NT_Service
    {
    public:
      Service (void);  virtual void handle_control (DWORD control_code);
      // We override <handle_control> because it handles stop requests
      // privately.  virtual int  handle_exception (ACE_HANDLE h);
      // We override <handle_exception> so a 'stop' control code can pop
      // the reactor off of its wait.  virtual int svc (void);
      // This is a virtual method inherited from ACE_NT_Service.  virtual int handle_timeout (const ACE_Time_Value& tv,
                                  const void *arg = 0);
      // Where the real work is done:private:
      typedef ACE_NT_Service inherited;private:
      int stop_;
    };// Define a singleton class as a way to insure that there's only one
    // Service instance in the program, and to protect against access from
    // multiple threads.  The first reference to it at runtime creates it,
    // and the ACE_Object_Manager deletes it at run-down.typedef ACE_Singleton<Service, ACE_Mutex> SERVICE;#endif /* #ifndef NTSVC_H_ */
    请指导,怎么通过以上程序把一个.exe文件注册!!!