Èñõîäíûå òåêñòû ïðèëîæåíèÿ SRVCTRL
Ãëàâíûé ôàéë èñõîäíûõ òåêñòîâ ïðèëîæåíèÿ SRVCTRL, ïðåäíàçíà÷åííîãî äëÿ óïðàâëåíèÿ ñåðâèñîì Sample of simple service ïðèâåäåí â ëèñòèíãå 5.3.
Ëèñòèíã 5.3. Ôàéë service/srvctrl.c
// ==================================================
// Ïðèëîæåíèå SRVCTRL
// Ðàáîòà ñ ñåðâèñîì "Sample of simple service"
//
// (Ñ) Ôðîëîâ À.Â., 1996
// Email: frolov@glas.apc.org
// ==================================================
#define STRICT
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "afxres.h"
#include "srvctrl.h"
HINSTANCE hInst;
char szAppName[] = "ServiceCtlApp";
char szAppTitle[] = "Simple Service Control";
// Ñîñòîÿíèå ñåðâèñà
SERVICE_STATUS ss;
// -----------------------------------------------------
// Ôóíêöèÿ WinMain
// -----------------------------------------------------
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;
// Ñîõðàíÿåì èäåíòèôèêàòîð ïðèëîæåíèÿ
hInst = hInstance;
// Ïðåâåðÿåì, íå áûëî ëè ýòî ïðèëîæåíèå çàïóùåíî ðàíåå
hWnd = FindWindow(szAppName, NULL);
if(hWnd)
{
// Åñëè áûëî, âûäâèãàåì îêíî ïðèëîæåíèÿ íà
// ïåðåäíèé ïëàí
if(IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
return FALSE;
}
// Ðåãèñòðèðóåì êëàññ îêíà
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIconSm = LoadImage(hInst,
MAKEINTRESOURCE(IDI_APPICONSM),
IMAGE_ICON, 16, 16, 0);
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadImage(hInst,
MAKEINTRESOURCE(IDI_APPICON),
IMAGE_ICON, 32, 32, 0);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_APPMENU);
wc.lpszClassName = szAppName;
if(!RegisterClassEx(&wc))
if(!RegisterClass((LPWNDCLASS)&wc.style))
return FALSE;
// Ñîçäàåì ãëàâíîå îêíî ïðèëîæåíèÿ
hWnd = CreateWindow(szAppName, szAppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInst, NULL);
if(!hWnd) return(FALSE);
// Îòîáðàæàåì îêíî è çàïóñêàåì öèêë
// îáðàáîòêè ñîîáùåíèé
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// -----------------------------------------------------
// Ôóíêöèÿ WndProc
// -----------------------------------------------------
LRESULT WINAPI
WndProc( HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
HANDLE_MSG(hWnd, WM_COMMAND, WndProc_OnCommand);
HANDLE_MSG(hWnd, WM_DESTROY, WndProc_OnDestroy);
default:
return(DefWindowProc(hWnd, msg, wParam, lParam));
}
}
// -----------------------------------------------------
// Ôóíêöèÿ WndProc_OnDestroy
// -----------------------------------------------------
#pragma warning(disable: 4098)
void WndProc_OnDestroy(HWND hWnd)
{
PostQuitMessage(0);
return 0L;
}
// -----------------------------------------------------
// Ôóíêöèÿ WndProc_OnCommand
// -----------------------------------------------------
#pragma warning(disable: 4098)
void WndProc_OnCommand(HWND hWnd, int id,
HWND hwndCtl, UINT codeNotify)
{
// Èäåíòèôèêàòîð ñåðâèñà
SC_HANDLE schService;
// Èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ ñåðâèñàìè
SC_HANDLE schSCManager;
LPQUERY_SERVICE_CONFIG lpBuf;
DWORD dwBytesNeeded;
char szBuf[1024];
switch (id)
{
case ID_FILE_EXIT:
{
// Çàâåðøàåì ðàáîòó ïðèëîæåíèÿ
PostQuitMessage(0);
return 0L;
break;
}
// Óñòàíîâêà ñåðâèñà â ñèñòåìó
case ID_SERVICE_INSTALL:
{
// Îòêðûâàåì ñèñòåìó óïðàâëåíèÿ ñåðâèñàìè
schSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if(!schSCManager)
break;
// Ñîçäàåì ñåðâèñ ñ èìåíåì MYServiceName
schService = CreateService(
schSCManager,
MYServiceName,
MYServiceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
"c:\\ntbk2\\src\\service\\small\\debug\\small.exe",
NULL,
NULL,
"",
NULL,
NULL);
// Çàêðûâàåì èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ
// ñåðâèñàìè
CloseServiceHandle(schSCManager);
break;
}
// Óäàëåíèå ñåðâèñà èç ñèñòåìû
case ID_SERVICE_REMOVE:
{
// Îòêðûâàåì ñèñòåìó óïðàâëåíèÿ ñåðâèñàìè
schSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if(!schSCManager)
break;
// Îòêðûâàåì ñåðâèñ ñ èìåíåì MYServiceName
schService = OpenService(
schSCManager, MYServiceName,
SERVICE_ALL_ACCESS);
if(!schService)
break;
// Îñòàíàâëèâàåì ñåðâèñ
ControlService(schService,
SERVICE_CONTROL_STOP, &ss);
// Âûçûâàåì ôóíêöèþ óäàëåíèÿ ñåðâèñà èç ñèñòåìû
DeleteService(schService);
// Çàêðûâàåì èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ
// ñåðâèñàìè
CloseServiceHandle(schSCManager);
break;
}
case ID_SERVICE_START:
{
// Îòêðûâàåì ñèñòåìó óïðàâëåíèÿ ñåðâèñàìè
schSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if(!schSCManager)
break;
// Îòêðûâàåì ñåðâèñ ñ èìåíåì MYServiceName
schService = OpenService(
schSCManager, MYServiceName,
SERVICE_ALL_ACCESS);
if(!schService)
break;
// Çàïóñêàåì ñåðâèñ
StartService(schService, 0, NULL);
// Çàêðûâàåì èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ
// ñåðâèñàìè
CloseServiceHandle(schSCManager);
break;
}
case ID_SERVICE_STOP:
{
// Îòêðûâàåì ñèñòåìó óïðàâëåíèÿ ñåðâèñàìè
schSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if(!schSCManager)
break;
// Îòêðûâàåì ñåðâèñ ñ èìåíåì MYServiceName
schService = OpenService(
schSCManager, MYServiceName,
SERVICE_ALL_ACCESS);
// Îñòàíàâëèâàåì ñåðâèñ
ControlService(schService,
SERVICE_CONTROL_STOP, &ss);
// Çàêðûâàåì èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ
// ñåðâèñàìè
CloseServiceHandle(schSCManager);
break;
}
case ID_SERVICE_GETCONFIGURATION:
{
// Îòêðûâàåì ñèñòåìó óïðàâëåíèÿ ñåðâèñàìè
schSCManager = OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);
if(!schSCManager)
break;
// Îòêðûâàåì ñåðâèñ ñ èìåíåì MYServiceName
schService = OpenService(
schSCManager, MYServiceName,
SERVICE_ALL_ACCESS);
if(!schService)
break;
// Ïîëó÷àåì áóôåð äëÿ ñîõðàíåíèÿ êîíôèãóðàöèè
lpBuf = (LPQUERY_SERVICE_CONFIG)malloc(4096);
if(lpBuf != NULL)
{
// Ñîõðàíÿåì êîíôèãóðàöèþ â áóôåðå
QueryServiceConfig(schService,
lpBuf, 4096, &dwBytesNeeded);
// Îòîáðàæàåì íåêîòîðûå ïîëÿ êîíôèãóðàöèè
wsprintf(szBuf, "Binary path: %s\n"
"Start Name: %s\n"
"Display Name: %s\n",
lpBuf->lpBinaryPathName,
lpBuf->lpServiceStartName,
lpBuf->lpDisplayName);
MessageBox(hWnd, szBuf, szAppTitle,
MB_OK | MB_ICONINFORMATION);
// Îñâîáîæäàåì áóôåð
free(lpBuf);
}
// Çàêðûâàåì èäåíòèôèêàòîð ñèñòåìû óïðàâëåíèÿ
// ñåðâèñàìè
CloseServiceHandle(schSCManager);
break;
}
case ID_HELP_ABOUT:
{
MessageBox(hWnd,
"Simple Service Control\n"
"(C) Alexandr Frolov, 1996\n"
"Email: frolov@glas.apc.org",
szAppTitle, MB_OK | MB_ICONINFORMATION);
return 0L;
break;
}
default:
break;
}
return FORWARD_WM_COMMAND(hWnd, id, hwndCtl, codeNotify,
DefWindowProc);
}
 ôàéëå srvctrl.h (ëèñòèíã 5.4) îïðåäåëåíî èìÿ ñåðâèñà è ïðîòîòèïû ôóíêöèé.
Ëèñòèíã 5.4. Ôàéë service/srvctrl.h
// Èìÿ ñåðâèñà
#define MYServiceName "Sample of simple service"
// -----------------------------------------------------
// Îïèñàíèå ôóíêöèé
// -----------------------------------------------------
LRESULT WINAPI
WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void WndProc_OnCommand(HWND hWnd, int id,
HWND hwndCtl, UINT codeNotify);
void WndProc_OnDestroy(HWND hWnd);
LRESULT WINAPI
DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL DlgProc_OnInitDialog(HWND hdlg, HWND hwndFocus,
LPARAM lParam);
void DlgProc_OnCommand(HWND hdlg, int id,
HWND hwndCtl, UINT codeNotify);
Ôàéë resource.h (ëèñòèíã 5.5) ñîäåðæèò îïèñàíèÿ êîíñòàíò, êîòîðûå èñïîëüçóþòñÿ â ôàéëå îïðåäåëåíèÿ ðåñóðñîâ ïðèëîæåíèÿ.
Ëèñòèíã 5.5. Ôàéë service/resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by srvctrl.rc
//
#define IDR_MENU1 101
#define IDR_APPMENU 101
#define IDI_APPICON 102
#define IDI_APPICONSM 103
#define ID_FILE_EXIT 40001
#define ID_HELP_ABOUT 40002
#define ID_SERVICE_INSTALL 40010
#define ID_SERVICE_REMOVE 40011
#define ID_SERVICE_START 40012
#define ID_SERVICE_STOP 40013
#define ID_SERVICE_GETCONFIGURATION 40014
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 105
#define _APS_NEXT_COMMAND_VALUE 40015
#define _APS_NEXT_CONTROL_VALUE 1006
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
Ôàéë îïðåäåëåíèÿ ðåñóðñîâ ïðèëîæåíèÿ ïðèâåäåí â ëèñòèíãå 5.6.
Ëèñòèíã 5.6. Ôàéë service/srvctrl.rc
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
//////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
//////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
//////////////////////////////////////////////////////////////
// Russian resources
#if !defined(AFX_RESOURCE_DLL) defined(AFX_TARG_RUS)
#ifdef _WIN32
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)
#endif //_WIN32
//////////////////////////////////////////////////////////////
//
// Menu
//
IDR_APPMENU MENU DISCARDABLE
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Service"
BEGIN
MENUITEM "&Install", ID_SERVICE_INSTALL
MENUITEM "&Remove", ID_SERVICE_REMOVE
MENUITEM SEPARATOR
MENUITEM "&Start", ID_SERVICE_START
MENUITEM "Sto&p", ID_SERVICE_STOP
MENUITEM SEPARATOR
MENUITEM "&Get configuration",
ID_SERVICE_GETCONFIGURATION
END
POPUP "&Help"
BEGIN
MENUITEM "&About...", ID_HELP_ABOUT
END
END
#ifdef APSTUDIO_INVOKED
//////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
//////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure
// application icon
// remains consistent on all systems.
IDI_APPICON ICON DISCARDABLE "srvctrl.ico"
IDI_APPICONSM ICON DISCARDABLE "srvctrsm.ico"
#endif // Russian resources
//////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
//////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
//////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED