找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3830|回复: 0

[原]VC++信息安全编程(10)创建Windows服务

[复制链接]
发表于 2012-3-8 14:58:25 | 显示全部楼层 |阅读模式
Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务。请见代码注释分析Windows服务
  1. #include "stdafx.h"
  2. #include "myservice.h"
  3. #include "myserviceDlg.h"
  4. #include "winsvc.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CAboutDlg dialog used for App About
  12. class CAboutDlg : public CDialog
  13. {
  14. public:
  15.         CAboutDlg();
  16. // Dialog Data
  17.         //{{AFX_DATA(CAboutDlg)
  18.         enum { IDD = IDD_ABOUTBOX };
  19.         //}}AFX_DATA
  20.         // ClassWizard generated virtual function overrides
  21.         //{{AFX_VIRTUAL(CAboutDlg)
  22.         protected:
  23.         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  24.         //}}AFX_VIRTUAL
  25. // Implementation
  26. protected:
  27.         //{{AFX_MSG(CAboutDlg)
  28.         //}}AFX_MSG
  29.         DECLARE_MESSAGE_MAP()
  30. };
  31. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  32. {
  33.         //{{AFX_DATA_INIT(CAboutDlg)
  34.         //}}AFX_DATA_INIT
  35. }
  36. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  37. {
  38.         CDialog::DoDataExchange(pDX);
  39.         //{{AFX_DATA_MAP(CAboutDlg)
  40.         //}}AFX_DATA_MAP
  41. }
  42. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  43.         //{{AFX_MSG_MAP(CAboutDlg)
  44.                 // No message handlers
  45.         //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CMyserviceDlg dialog
  49. CMyserviceDlg::CMyserviceDlg(CWnd* pParent /*=NULL*/)
  50.         : CDialog(CMyserviceDlg::IDD, pParent)
  51. {
  52.         //{{AFX_DATA_INIT(CMyserviceDlg)
  53.                 // NOTE: the ClassWizard will add member initialization here
  54.         //}}AFX_DATA_INIT
  55.         // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  56.         m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  57. }
  58. void CMyserviceDlg::DoDataExchange(CDataExchange* pDX)
  59. {
  60.         CDialog::DoDataExchange(pDX);
  61.         //{{AFX_DATA_MAP(CMyserviceDlg)
  62.                 // NOTE: the ClassWizard will add DDX and DDV calls here
  63.         //}}AFX_DATA_MAP
  64. }
  65. BEGIN_MESSAGE_MAP(CMyserviceDlg, CDialog)
  66.         //{{AFX_MSG_MAP(CMyserviceDlg)
  67.         ON_WM_SYSCOMMAND()
  68.         ON_WM_PAINT()
  69.         ON_WM_QUERYDRAGICON()
  70.         ON_BN_CLICKED(IDC_BSERVICE, OnBservice)
  71.         ON_BN_CLICKED(IDC_BNOTROOT, OnBnotroot)
  72.         ON_BN_CLICKED(IDC_BDELSER, OnBdelser)
  73.         //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CMyserviceDlg message handlers
  77. BOOL CMyserviceDlg::OnInitDialog()
  78. {
  79.         CDialog::OnInitDialog();
  80.         // Add "About..." menu item to system menu.
  81.         // IDM_ABOUTBOX must be in the system command range.
  82.         ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83.         ASSERT(IDM_ABOUTBOX < 0xF000);
  84.         CMenu* pSysMenu = GetSystemMenu(FALSE);
  85.         if (pSysMenu != NULL)
  86.         {
  87.                 CString strAboutMenu;
  88.                 strAboutMenu.LoadString(IDS_ABOUTBOX);
  89.                 if (!strAboutMenu.IsEmpty())
  90.                 {
  91.                         pSysMenu->AppendMenu(MF_SEPARATOR);
  92.                         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93.                 }
  94.         }
  95.         // Set the icon for this dialog.  The framework does this automatically
  96.         //  when the application's main window is not a dialog
  97.         SetIcon(m_hIcon, TRUE);                        // Set big icon
  98.         SetIcon(m_hIcon, FALSE);                // Set small icon
  99.        
  100.         // TODO: Add extra initialization here
  101.         schSCManager=OpenSCManager(NULL,NULL,GENERIC_WRITE);
  102.         return TRUE;  // return TRUE  unless you set the focus to a control
  103. }
  104. void CMyserviceDlg::OnSysCommand(UINT nID, LPARAM lParam)
  105. {
  106.         if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  107.         {
  108.                 CAboutDlg dlgAbout;
  109.                 dlgAbout.DoModal();
  110.         }
  111.         else
  112.         {
  113.                 CDialog::OnSysCommand(nID, lParam);
  114.         }
  115. }
  116. // If you add a minimize button to your dialog, you will need the code below
  117. //  to draw the icon.  For MFC applications using the document/view model,
  118. //  this is automatically done for you by the framework.
  119. void CMyserviceDlg::OnPaint()
  120. {
  121.         if (IsIconic())
  122.         {
  123.                 CPaintDC dc(this); // device context for painting
  124.                 SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  125.                 // Center icon in client rectangle
  126.                 int cxIcon = GetSystemMetrics(SM_CXICON);
  127.                 int cyIcon = GetSystemMetrics(SM_CYICON);
  128.                 CRect rect;
  129.                 GetClientRect(&rect);
  130.                 int x = (rect.Width() - cxIcon + 1) / 2;
  131.                 int y = (rect.Height() - cyIcon + 1) / 2;
  132.                 // Draw the icon
  133.                 dc.DrawIcon(x, y, m_hIcon);
  134.         }
  135.         else
  136.         {
  137.                 CDialog::OnPaint();
  138.         }
  139. }
  140. // The system calls this to obtain the cursor to display while the user drags
  141. //  the minimized window.
  142. HCURSOR CMyserviceDlg::OnQueryDragIcon()
  143. {
  144.         return (HCURSOR) m_hIcon;
  145. }
  146. void CMyserviceDlg::OnBservice()
  147. {
  148.    
  149.     TCHAR szPath[512];
  150.         //CString aa;
  151.         //aa.Format("%s",szPath);
  152.         //CString *bb=aa;
  153.         ::GetModuleFileName(NULL,szPath,512);
  154.        
  155.         if(schSCManager)
  156.         {
  157.                 ::AfxMessageBox("open servicemanager succeed!");
  158.                 schService=::CreateService(schSCManager,"myservice","myservice",SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,szPath,NULL,NULL,NULL,NULL,NULL);
  159.         if(schService)
  160.         {
  161.                 ::AfxMessageBox("create service succeed!");
  162.                 /* SERVICE_TABLE_ENTRY DispatchTable[] =
  163.                  {
  164.                          {"myservice",(LPSERVICE_MAIN_FUNCTION)MyServiceMain}, //服务程序的名称和入口点;
  165.                          {NULL  ,NULL} //SERVICE_TABLE_ENTRY结构必须以"NULL"结束;
  166.                  };
  167.          StartServiceCtrlDispatcher(DispatchTable);
  168.                  ::QueryServiceStatus(schService,ssStatus);
  169.                  ServiceStatusHandle=RegisterServiceCtrlHandler("ntkrnl",CmdControl);*/
  170.                 ::CloseServiceHandle(schService);
  171.         }
  172.         else
  173.         {
  174.                 ::AfxMessageBox("create service failed!");
  175.         }
  176.         ::CloseServiceHandle(schSCManager);
  177.         }
  178.         else
  179.                 AfxMessageBox("open servicemanager failed!");
  180.        
  181. }
  182. void CMyserviceDlg::OnBnotroot()
  183. {
  184.         ::ChangeServiceConfig(schService,SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_DEMAND_START,NULL,NULL,NULL,NULL,NULL,NULL,"my service");       
  185. }
  186. void CMyserviceDlg::OnBdelser()
  187. {
  188.         schService=::OpenService(schSCManager,"myservice",DELETE);
  189.         if(schService)
  190.         {
  191.                 AfxMessageBox("Delete Service succeed!");
  192.             ::DeleteService(schService);
  193.         ::CloseServiceHandle(schService);
  194.         }
  195.         else
  196.                 AfxMessageBox("Delete Service failed!");
  197.         //::DeleteFile(bb);
  198. }
  199. /*void CMyserviceDlg::MyServiceMain(DWORD argc,LPTSTR *argv)
  200. {
  201.         HANDEL sshStatusHandle;
  202.         sshStatusHandle=RegisterServiceCtrlHandler("myservice",ServiceControlHandler);
  203.         if(!sshStatusHandle)
  204.                 goto clearnup:
  205.         m_ssStatus.
  206. }*/
复制代码

作者:yincheng01 发表于2011-12-14 23:52:19 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-3 19:15 , Processed in 0.038651 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表