热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[程序设计]谁能告诉我这是什么呀(转载)

Listing1CodethatimplementsthesimplestpossibleNTservice//Fromthebook

Listing 1

  Code that implements the simplest possible NT service

  // From the book “Win32 System Services: The Heart of Windows NT“

  // by Marshall Brain

  // Published by Prentice Hall

  // This code implements the simplest possible service.

  // It beeps every 2 seconds, or at a user specified interval.

  // beepserv.cpp

  #include

  #include

  #include

  #include

  #define DEFAULT_BEEP_DELAY 2000

  // Global variables

  // The name of the service

  char *SERVICE_NAME = “BeepService“;

  // Event used to hold ServiceMain from completing

  HANDLE terminateEvent = NULL;

  // Handle used to communicate status info with

  // the SCM. Created by RegisterServiceCtrlHandler

  SERVICE_STATUS_HANDLE serviceStatusHandle;

  // The beep interval in ms.

  int beepDelay = DEFAULT_BEEP_DELAY;

  // Flags holding current state of service

  BOOL pauseService = FALSE;

  BOOL runningService = FALSE;

  // Thread for the actual work

  HANDLE threadHandle = 0;

  void ErrorHandler(char *s, DWORD err)

   cout <

   cout <<“Error number: “ <

   ExitProcess(err);

  // This function consolidates the activities of

  // updating the service status with

  // SetServiceStatus

  BOOL SendStatusToSCM (DWORD dwCurrentState,

   DWORD dwWin32ExitCode,

   DWORD dwServiceSpecificExitCode,

   DWORD dwCheckPoint,

   DWORD dwWaitHint)

   BOOL success;

   SERVICE_STATUS serviceStatus;

   // Fill in all of the SERVICE_STATUS fields

   serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

   serviceStatus.dwCurrentState = dwCurrentState;

   // If in the process of doing something, then accept

   // no control events, else accept anything

   if (dwCurrentState == SERVICE_START_PENDING)

   serviceStatus.dwCOntrolsAccepted= 0;

   else

   serviceStatus.dwCOntrolsAccepted=

   SERVICE_ACCEPT_STOP |

   SERVICE_ACCEPT_PAUSE_CONTINUE |

[程序设计]谁能告诉我这是什么呀(转载)

   SERVICE_ACCEPT_SHUTDOWN;

   // if a specific exit code is defined, set up

   // the win32 exit code properly

   if (dwServiceSpecificExitCode == 0)

   serviceStatus.dwWin32ExitCode = dwWin32ExitCode;

   else

   serviceStatus.dwWin32ExitCode =

   ERROR_SERVICE_SPECIFIC_ERROR;

   serviceStatus.dwServiceSpecificExitCode =

   dwServiceSpecificExitCode;

   serviceStatus.dwCheckPoint = dwCheckPoint;

   serviceStatus.dwWaitHint = dwWaitHint;

   // Pass the status record to the SCM

   success = SetServiceStatus (serviceStatusHandle,

   &serviceStatus);

   return success;

  DWORD ServiceThread(LPDWORD param)

[程序设计]谁能告诉我这是什么呀(转载)

   while (1)

   Beep(200,200);

   Sleep(beepDelay);

   return 0;

  // Initializes the service by starting its thread

  BOOL InitService()

   DWORD id;

   // Start the service’s thread

   threadHandle = CreateThread(0, 0,

   (LPTHREAD_START_ROUTINE) ServiceThread,

   0, 0, &id);

   if (threadHandle==0)

   return FALSE;

   else

   runningService = TRUE;

   return TRUE;

  // Dispatches events received from the SCM

  VOID Handler (DWORD controlCode)

   DWORD currentState = 0;

   BOOL success;

   switch(controlCode)

   // There is no START option because

   // ServiceMain gets called on a start

   // Stop the service

   case SERVICE_CONTROL_STOP:

   // Tell the SCM what’s happening

   success = SendStatusToSCM(SERVICE_STOP_PENDING,

   NO_ERROR, 0, 1, 5000);

   runningService=FALSE;

   // Set the event that is holding ServiceMain

   // so that ServiceMain can return

   SetEvent(terminateEvent);

   return;

   // Pause the service

   case SERVICE_CONTROL_PAUSE:

   if (runningService && !pauseService)

   // Tell the SCM what’s happening

   success = SendStatusToSCM(

   SERVICE_PAUSE_PENDING,

   NO_ERROR, 0, 1, 1000);

   pauseService = TRUE;

   SuspendThread(threadHandle);

   currentState = SERVICE_PAUSED;

   break;

   // Resume from a pause

   case SERVICE_CONTROL_CONTINUE:

   if (runningService && pauseService)

   // Tell the SCM what’s happening

   success = SendStatusToSCM(

   SERVICE_CONTINUE_PENDING,

   NO_ERROR, 0, 1, 1000);

   pauseService=FALSE;

   ResumeThread(threadHandle);

   currentState = SERVICE_RUNNING;

   break;

   // Update current status

   case SERVICE_CONTROL_INTERROGATE:

   // it will fall to bottom and send status

   break;

   // Do nothing in a shutdown. Could do cleanup

   // here but it must be very quick.

   case SERVICE_CONTROL_SHUTDOWN:

   return;

   default:

   break;

   SendStatusToSCM(currentState, NO_ERROR, 0, 0, 0);

  // Handle an error from ServiceMain by cleaning up

  // and telling SCM that the service didn’t start.

  VOID terminate(DWORD error)

   // if terminateEvent has been created, close it.

   if (terminateEvent) CloseHandle(terminateEvent);

   // Send a message to the scm to tell about stopage

   if (serviceStatusHandle)

   SendStatusToSCM(SERVICE_STOPPED, error,

   0, 0, 0);

   // If the thread has started, kill it off

   if (threadHandle) CloseHandle(threadHandle);

   // Do not need to close serviceStatusHandle

  // ServiceMain is called when the SCM wants to

  // start the service. When it returns, the service

  // has stopped. It therefore waits on an event

  // just before the end of the function, and

  // that event gets set when it is time to stop.

  // It also returns on any error because the

  // service cannot start if there is an eror.

  VOID ServiceMain(DWORD argc, LPTSTR *argv)

   BOOL success;

   // immediately call Registration function

   serviceStatusHandle =

   RegisterServiceCtrlHandler(

   SERVICE_NAME, (LPHANDLER_FUNCTION)Handler);

   if (!serviceStatusHandle) {terminate(GetLastError()); return;}

   // Notify SCM of progress

   success = SendStatusToSCM(SERVICE_START_PENDING,

   NO_ERROR, 0, 1, 5000);

   if (!success) {terminate(GetLastError()); return;}

   // create the termination event

   terminateEvent = CreateEvent (0, TRUE, FALSE, 0);

   if (!terminateEvent) {terminate(GetLastError()); return;}

   // Notify SCM of progress

   success = SendStatusToSCM(SERVICE_START_PENDING,

   NO_ERROR, 0, 2, 1000);

   if (!success) {terminate(GetLastError()); return;}

   // Check for startup params

   if (argc == 2)

   int temp = atoi(argv[1]);

   if (temp <1000)

   beepDelay = DEFAULT_BEEP_DELAY;

   else

   beepDelay = temp;

   // Notify SCM of progress

   success = SendStatusToSCM(SERVICE_START_PENDING,

   NO_ERROR, 0, 3, 5000);

   if (!success) {terminate(GetLastError()); return;}

   // Start the service itself

   success = InitService();

   if (!success) {terminate(GetLastError()); return;}

   // The service is now running.

   // Notify SCM of progress

   success = SendStatusToSCM(SERVICE_RUNNING,

   NO_ERROR, 0, 0, 0);

   if (!success) {terminate(GetLastError()); return;}

   // Wait for stop signal, and then terminate

   WaitForSingleObject (terminateEvent, INFINITE);

   terminate(0);

  VOID main(VOID)

   SERVICE_TABLE_ENTRY serviceTable[] =

   { SERVICE_NAME,

   (LPSERVICE_MAIN_FUNCTION) ServiceMain},

   { NULL, NULL }

   BOOL success;

   // Register with the SCM

   success =

   StartServiceCtrlDispatcher(serviceTable);

   if (!success)

   ErrorHandler(“In StartServiceCtrlDispatcher“,

   GetLastError());


推荐阅读
  • 我首先提高本程序的权限,然后成功得到服务程序(exe)的读写权限,开辟新的远程内存空间,然后拷贝程序执行的代码,但写入我的dll后,服务程序什么反应也没有。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • Linux 中使用 clone 函数来创建线程
    2019独角兽企业重金招聘Python工程师标准Linux上创建线程一般使用的是pthread库实际上libc也给我们提供了创建线程的函数那就是cloneintclone(i ... [详细]
  • 作者一直强调的一个概念叫做oneloopperthread,撇开多线程不谈,本篇博文将学习,怎么将传统的IO复用pollepoll封装到C++类中。1.IO复用复习使用p ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了一个线程多次调用一个函数相关的知识,希望对你有一定的参考价值。 ... [详细]
  • pthread_mutex_lockpthread_mutex_lock(pthread_mutex_t*mutex);intpthread_mutex_trylock(pthre ... [详细]
  • 近期看见一篇来自Intel的很有意思的分析文章,作者提到在他向45名与会的各公司程序员开发经理战略师提问“什么是实施并行编程的最大障碍”时,下面五个因素 ... [详细]
author-avatar
董福强
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有