• 一个简单的服务程序


    //////////////////////////////////////////////////////////////////////////////////////
    //                                                                                    //
    // 日期:    2007/08/10                                                                //
    // 作者:    冷风                                                                    //
    // 说明:    服务程序框架 请改写mainfun函数                                                            //
    // 联系:    QQ 121121606                                             //
    //////////////////////////////////////////////////////////////////////////////////////

    #include 
    <windows.h>
    #include 
    <tlhelp32.h>
    #include 
    <winsvc.h>
    #include 
    <stdio.h>
    #include 
    <winsock.h>
    #include 
    "stdafx.h"
    #include 
    <iostream>
    #include 
    <windows.h>
    #pragma comment (lib,"ws2_32")                        //加载库函数

    SERVICE_STATUS          ServiceStatus;
    SERVICE_STATUS_HANDLE   hStatus;


    VOID    WINAPI ServiceMain( DWORD dwArgc, LPTSTR 
    *lpszArgv );
    VOID    WINAPI ServiceHandler( DWORD fdwControl );
    DWORD    WINAPI mainfun(LPVOID lpParam);



    void EmptyPipe(HANDLE hReadPipe1)
    {
        BOOL ret;
        DWORD bytesRead;
        
    char *buffer=new char [1024];
        
    while(true)
        {
            memset(buffer,
    0,1024);
            ret
    =PeekNamedPipe(hReadPipe1,buffer,1024,&bytesRead,0,0);
            
    if (bytesRead==0||!ret)
            {
                delete buffer;
                
    return;
            }
            ReadFile(hReadPipe1,buffer,bytesRead,
    &bytesRead,0);
        }



    int main(int argc,char *argv[])
    {   
        SERVICE_TABLE_ENTRY ServiceTable[
    2];
        ServiceTable[
    0].lpServiceName = "IIS Angent";
        ServiceTable[
    0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
        ServiceTable[
    1].lpServiceName = NULL;
        ServiceTable[
    1].lpServiceProc = NULL;
        StartServiceCtrlDispatcher(ServiceTable);     
    // 启动服务的控制分派机线程
        
        
    return 0;
    }



    VOID WINAPI ServiceHandler(DWORD fdwControl) 

        
    switch(fdwControl)
        {
        
    case SERVICE_CONTROL_PAUSE:
            ServiceStatus.dwCurrentState 
    = SERVICE_PAUSED;
            
    break;
            
        
    case SERVICE_CONTROL_CONTINUE:
            ServiceStatus.dwCurrentState 
    = SERVICE_RUNNING;
            
    break;
            
        
    case SERVICE_CONTROL_STOP: 
        
    case SERVICE_CONTROL_SHUTDOWN:
            ServiceStatus.dwCurrentState  
    = SERVICE_STOPPED;
            ServiceStatus.dwWin32ExitCode 
    = 0;
            ServiceStatus.dwCheckPoint    
    = 0;
            ServiceStatus.dwWaitHint      
    = 0;
            SetServiceStatus(hStatus,
    &ServiceStatus);
            
    return ;
            
        
    case SERVICE_CONTROL_INTERROGATE:
            
    break;
            
        
    default:
            
    break;
        }
        
        SetServiceStatus(hStatus,
    &ServiceStatus);
        
    return ;
    }

    VOID WINAPI ServiceMain( DWORD dwArgc, LPTSTR 
    *lpszArgv )
    {
        DWORD   status 
    = 0
        DWORD   specificError 
    = 0xfffffff
        
        ServiceStatus.dwServiceType        
    = SERVICE_WIN32; 
        ServiceStatus.dwCurrentState       
    = SERVICE_START_PENDING; 
        ServiceStatus.dwControlsAccepted   
    = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE; 
        ServiceStatus.dwWin32ExitCode      
    = 0
        ServiceStatus.dwServiceSpecificExitCode 
    = 0
        ServiceStatus.dwCheckPoint         
    = 0
        ServiceStatus.dwWaitHint           
    = 0
        
        hStatus 
    = RegisterServiceCtrlHandler("IIS Angent",(LPHANDLER_FUNCTION)ServiceHandler); 
        
    if (hStatus==0
            
    return
        
        
    // Handle error condition 
        status = GetLastError(); 
        
    if (status!=NO_ERROR) 
        { 
            ServiceStatus.dwCurrentState       
    = SERVICE_STOPPED; 
            ServiceStatus.dwCheckPoint         
    = 0
            ServiceStatus.dwWaitHint           
    = 0
            ServiceStatus.dwWin32ExitCode      
    = status; 
            ServiceStatus.dwServiceSpecificExitCode 
    = specificError; 
            SetServiceStatus(hStatus, 
    &ServiceStatus); 
            
    return
        } 
        
        
    // Initialization complete - report running status 
        ServiceStatus.dwCurrentState       = SERVICE_RUNNING; 
        ServiceStatus.dwCheckPoint         
    = 0
        ServiceStatus.dwWaitHint           
    = 0;  
        SetServiceStatus(hStatus, 
    &ServiceStatus);
        
        
    //do my job
        HANDLE hThread=CreateThread(NULL,0,mainfun,NULL,0,NULL);
        
    if(hThread==NULL)
            
    return;
        WaitForSingleObject(hThread,INFINITE);
    }


    DWORD WINAPI mainfun(LPVOID lpParam)
    {

      
    ////你想要实现功能的代码填入即可
            return 0;
    }

    安装服务程序:

    #include <windows.h>
    #include 
    <winsvc.h>
    #include 
    <stdio.h>
    main()
    {
        
    char name[100];
        
    char info[200];
        
    char path[300];
        printf(
    "请输入服务名\n\n");
        scanf (
    "%s",&name);
        printf(
    "请输入服务描述\n\n");
        scanf (
    "%s",&info);
        printf(
    "请输入程序路径\n\n");
        scanf (
    "%s",&path);
        SC_HANDLE manager
    =NULL;
        SC_HANDLE service
    =NULL;
        
    if((manager=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
        {
            printf(
    "OpenSCManager Error");
        }
        service
    =CreateService(
                manager,name,info, 
                SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, 
                SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
                path, 
    00000 );
        
    if(service)
        printf(
    "服务创建成功\n\n");
        
    else
        printf(
    "服务创建失败\n\n");
        CloseServiceHandle(service);
        CloseServiceHandle(manager);
    }

    删除服务程序:

    #include <windows.h>
    #include 
    <winsvc.h>
    #include 
    <stdio.h>
    void main()
    {
        
    char name[100];
        SC_HANDLE scm;
            SC_HANDLE service;
        SERVICE_STATUS status;
        printf(
    "请输入要删除的服务名\n\n");
        scanf (
    "%s",&name);

        
    if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
        {
            printf(
    "OpenSCManager Error\n");
        }
        service
    =OpenService(scm,name,SERVICE_ALL_ACCESS|DELETE);
        
    if (!service)
        {
            printf(
    "OpenService error!\n");
            
    return;
        }
            BOOL isSuccess
    =QueryServiceStatus(service,&status);
        
    if (!isSuccess)
        {
            printf(
    "QueryServiceStatus error!\n");
            
    return;
        }
           
    if ( status.dwCurrentState!=SERVICE_STOPPED )
        {
            isSuccess
    =ControlService(service,SERVICE_CONTROL_STOP,&status);
            
    if (!isSuccess )
            printf(
    "Stop Service error!\n");
            Sleep( 
    500 );
        }
        isSuccess
    =DeleteService(service);
        
    if (!isSuccess)
             printf(
    "删除服务失败!\n");
        
    else
             printf(
    "删除服务成功!\n");
        CloseServiceHandle(service );
        CloseServiceHandle(scm);
    }


     /Files/tt_mc/0712-diy666666.rar

  • 相关阅读:
    Atitit 图像金字塔原理与概率 attilax的理解总结qb23
    Atitit.软件中见算法 程序设计五大种类算法
    百度之星
    linux下编程IDE环境
    c++多态的实现 VC++消息映射的实现
    static_cast, const_cast,dynamic_cast和reinterpret_cast
    X Window研究笔记(1)~22
    关于串口发送短信
    模式识别中的核方法
    IC设计,verilog学习链接
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1655958.html
Copyright © 2020-2023  润新知