Организация удаленного доступа к распределенным базам данных
Рефераты >> Программирование и компьютеры >> Организация удаленного доступа к распределенным базам данных

DWORD ServiceInitialization(DWORD argc, LPTSTR *argv,

DWORD *specificError);

VOID _CRTAPI1 main(int argc, char **argv)

{

int i;

SERVICE_TABLE_ENTRY DispatchTable[] = {

{ TEXT("SiTime"), ServiceStart },

{ NULL, NULL }

};

/* Allow the user to override settings with command line switches */

for ( i = 1; i < argc; i++) {

if ((*argv[i] == '-') || (*argv[i] == '/')) {

switch (tolower(*(argv[i]+1))) {

case 'p': // protocol sequence

pszProtocolSequence = argv[++i];

break;

case 'e': // endpoint

pszEndpoint = argv[++i];

break;

default: ;

}

}

}

if (!StartServiceCtrlDispatcher( DispatchTable)) {

/* Error Handling */

}

}

void WINAPI ServiceStart(DWORD argc, LPTSTR *argv)

{

DWORD status;

DWORD specificError;

ServiceStatus.dwServiceType = SERVICE_WIN32;

ServiceStatus.dwCurrentState = SERVICE_START_PENDING;

ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |

SERVICE_ACCEPT_PAUSE_CONTINUE;

ServiceStatus.dwWin32ExitCode = 0;

ServiceStatus.dwServiceSpecificExitCode = 0;

ServiceStatus.dwCheckPoint = 0;

ServiceStatus.dwWaitHint = 0;

ServiceStatusHandle = RegisterServiceCtrlHandler(

TEXT("SiTime"),

ServiceCtrlHandler);

if (ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) {

/* Error Handling */

return;

}

// Initialization code goes here.

status = ServiceInitialization(argc,argv, &specificError);

// Handle error condition

if (status != NO_ERROR) {

ServiceStatus.dwCurrentState = SERVICE_STOPPED;

ServiceStatus.dwCheckPoint = 0;

ServiceStatus.dwWaitHint = 0;

ServiceStatus.dwWin32ExitCode = status;

ServiceStatus.dwServiceSpecificExitCode = specificError;

SetServiceStatus (ServiceStatusHandle, &ServiceStatus);

return;

}

// Initialization complete - report running status

ServiceStatus.dwCurrentState = SERVICE_RUNNING;

ServiceStatus.dwCheckPoint = 0;

ServiceStatus.dwWaitHint = 0;

if (!SetServiceStatus (ServiceStatusHandle, &ServiceStatus)) {

status = GetLastError();

}

// This is where the service does its work. //

ServerProcess();

return;

}

/* stub initialization function */

DWORD ServiceInitialization(DWORD argc, LPTSTR *argv,

DWORD *specificError)

{

*specificError = ServerInit();

if (*specificError) return *specificError;

return(0);

}

void WINAPI ServiceCtrlHandler ( IN DWORD Opcode)

{

DWORD status;

switch(Opcode) {

case SERVICE_CONTROL_PAUSE:

/* Do whatever it takes to pause here. */

ServerDoPause();

ServiceStatus.dwCurrentState = SERVICE_PAUSED;

break;

case SERVICE_CONTROL_CONTINUE:

/* Do whatever it takes to continue here.*/

ServerDoContinue();

ServiceStatus.dwCurrentState = SERVICE_RUNNING;

break;

case SERVICE_CONTROL_STOP:

/* Do whatever it takes to stop here. */

ServerDoStop();

ServiceStatus.dwWin32ExitCode = 0;

ServiceStatus.dwCurrentState = SERVICE_STOPPED;

ServiceStatus.dwCheckPoint = 0;

ServiceStatus.dwWaitHint = 0;

if (!SetServiceStatus (ServiceStatusHandle, &ServiceStatus))

{

status = GetLastError();

}

return;

case SERVICE_CONTROL_INTERROGATE:

/* fall through to send current status */

break;

default:

/* Error handling */

break;

}

/* Send current status.*/

if (!SetServiceStatus (ServiceStatusHandle, &ServiceStatus)) {

status = GetLastError();

}

return;

}

CommonConfig.c

Файл CommonConfig.c - Управление конфигурацией

#include <windows.h>

#include "CommonConfig.h"

#include "EventLog.h"

#define REGVALUENAME_LENGTH 255

DWORD ConfigWatchingThread;

HANDLE hConfigMutex = NULL;

HANDLE hTaskMutex = NULL;

unsigned char * pszProtocolSequence = "ncacn_np";

unsigned char * pszSecurity = NULL;

unsigned char * pszEndpoint = "\\pipe\\CommServ";

unsigned int cMinCalls = 1;

unsigned int cMaxCalls = 20;

unsigned int fDontWait = FALSE;

struct TASKENTRY TaskTable[TASK_COUNT];

int EntryCount = 0;

DWORD TaskThreads[TASK_COUNT];

int TaskCount = 0;

void UpdateVariables()

{

HKEY hKey;

DWORD dwIndex = 0;

DWORD VNameLength = REGVALUENAME_LENGTH;

char VName[REGVALUENAME_LENGTH];

DWORD dwLength = sizeof(struct TASKENTRY);

int i;

try {

WaitForSingleObject(hConfigMutex, INFINITE);

// Инициализация таблицы задач

for (i = 0; i < TASK_COUNT; i++) {

TaskTable[i].Active = FALSE;

TaskTable[i].ExecTime = 0;

ZeroMemory(&TaskTable[i].DllName, sizeof(TaskTable[i].DllName));

TaskTable[i].TermProc = NULL;

TaskTable[i].TaskThread = 0;

}

// Загрузка таблицы задач из реестра

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,

REGISTRY_TASKS_PATH,

0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) {

dwIndex = 0;

EntryCount = 0;

while (RegEnumValue(hKey,

dwIndex,

(char *)&VName,

&VNameLength,

NULL,

NULL,

(LPVOID)&TaskTable[dwIndex],

&dwLength) == ERROR_SUCCESS) {

if (dwLength != sizeof(struct TASKENTRY)) {

LogEvent(EVENTLOG_ERROR_TYPE, "Invalid Task Parameter");

break;

}

EntryCount+=1;

dwIndex+=1;

}

RegCloseKey(hKey);

} else LogEvent(EVENTLOG_ERROR_TYPE, "Error Loading Configuration");

}

finally {

ReleaseMutex(hConfigMutex);

}

}

DoService.c

#include <windows.h>

#include "DoService.h"

#include " \Comm.h"

#include "CommonConfig.h"

#include "ClientHandler.h"

#include "EventLog.h"

#include "ShedulerServ.h"

void ServerProcess() {

hConfigMutex = CreateMutex(NULL, FALSE, NULL);

hTaskMutex = CreateMutex(NULL, FALSE, NULL);

CreateThread(NULL, 0, ShedulingProc, NULL, 0, &ShedulingThread);

CreateThread(NULL, 0, RPCClientHandling, NULL, 0, &ClientHandlingThread);

}

DWORD ServerInit() {

RPC_STATUS status;

status = RpcServerUseProtseqEp(

pszProtocolSequence,

cMaxCalls,

pszEndpoint,

pszSecurity); // Security descriptor

if (status != NO_ERROR) {

return(1);

}

status = RpcServerRegisterIf(

CommService_ServerIfHandle, // !!!

NULL, // MgrTypeUuid

NULL); // MgrEpv; null means use default

if (status != NO_ERROR) {

return(2);

}

LogEvent(EVENTLOG_INFORMATION_TYPE, "\"Svyazinform\" Communicatin Service Initialized");

return(0);

}

void ServerDoPause()

{

SuspendThread(&ShedulingThread);

SuspendThread(&ClientHandlingThread);


Страница: