Windows NT ( 2)

       

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_STATUSss;

// -----------------------------------------------------

// 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_HANDLEschService;

//

SC_HANDLEschSCManager;

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