WindowsNT Service的写法!(抄的)
bskay 2002-01-12 06:08:09 #include <stdio.h>
#include <Windows.h>
#include <Winsvc.h>
//WritePrivateProfileString
//GetPrivateProfileString
SERVICE_STATUS MyStatus;
SERVICE_STATUS_HANDLE MyServiceStatusHandle;
VOID WINAPI MyServiceMain(
DWORD dwArgc, // number of arguments
LPTSTR *lpszArgv // array of arguments
);
VOID MyServiceCtrlHandler (DWORD opcode);
DWORD MyServiceInitialization (DWORD argc, LPTSTR *argv,
DWORD *specificError);
VOID SvcDebugOut(LPSTR String, DWORD Status);
BOOL MyInstallService()
{
SC_HANDLE schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database SERVICES_ACTIVE_DATABASE
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == NULL)
return FALSE;
SC_HANDLE schService = OpenService(
schSCManager, // SCM database
"__Svc__", // service name
SERVICE_ALL_ACCESS);
if (schService != NULL)
{
if(!DeleteService(schService))
return FALSE;
CloseServiceHandle(schService);
}
LPCTSTR lpszBinaryPathName =
"E:\\cdmapp\\test\\MyService\\Debug\\MyService.exe";
schService = CreateService(
schSCManager, // SCManager database
"__Svc__", // name of service
"A B C D E Service", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
return FALSE;
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return TRUE;
}
BOOL MyStartService()
{
SC_HANDLE schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database SERVICES_ACTIVE_DATABASE
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == NULL)
return FALSE;
SC_HANDLE schService = OpenService(
schSCManager, // SCM database
"__Svc__", // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
return FALSE;
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
return FALSE;
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
void DoTest()
{
WritePrivateProfileString(
"App",
"key",
"String",
"C:\\ppp.ini");
TCHAR buf[255];
GetPrivateProfileString(
"App",
"key",
"String",
buf,
254,
"C:\\ppp.ini");
}
void main( )
{
DoTest();
if ( 0 )
MyInstallService();
if ( 0 )
MyStartService();
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ "__Svc__", MyServiceMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher( DispatchTable))
{
SvcDebugOut(
" [MY_SERVICE] StartServiceCtrlDispatcher error = %d\n",
GetLastError());
}
}
VOID SvcDebugOut(LPSTR String, DWORD Status)
{
CHAR Buffer[1024];
if (strlen(String) < 1000)
{
sprintf(Buffer, String, Status);
OutputDebugStringA(Buffer);
}
}
void WINAPI MyServiceMain (DWORD argc, LPTSTR *argv)
{
DWORD status;
DWORD specificError;
MyServiceStatus.dwServiceType = SERVICE_WIN32;
MyServiceStatus.dwCurrentState = SERVICE_START_PENDING;
MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_PAUSE_CONTINUE;
MyServiceStatus.dwWin32ExitCode = 0;
MyServiceStatus.dwServiceSpecificExitCode = 0;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
MyServiceStatusHandle = RegisterServiceCtrlHandler(
"MyService",
(LPHANDLER_FUNCTION)MyServiceCtrlHandler);
if (MyServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
SvcDebugOut(" [MY_SERVICE] RegisterServiceCtrlHandler failed %d\n", GetLastError());
return;
}
// Initialization code goes here.
status = MyServiceInitialization(argc,argv, &specificError);
// Handle error condition
if (status != NO_ERROR)
{
MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
MyServiceStatus.dwWin32ExitCode = status;
MyServiceStatus.dwServiceSpecificExitCode = specificError;
SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus);
return;
}
// Initialization complete - report running status.
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
{
status = GetLastError();
SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status);
}
// This is where the service does its work.
SvcDebugOut(" [MY_SERVICE] Returning the Main Thread \n",0);
return;
}
// Stub initialization function.
DWORD MyServiceInitialization(DWORD argc, LPTSTR *argv,
DWORD *specificError)
{
argv;
argc;
specificError;
return(0);
}
VOID MyServiceCtrlHandler (DWORD Opcode)
{
DWORD status;
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
// Do whatever it takes to pause here.
MyServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
// Do whatever it takes to continue here.
MyServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
// Do whatever it takes to stop here.
MyServiceStatus.dwWin32ExitCode = 0;
MyServiceStatus.dwCurrentState = SERVICE_STOPPED;
MyServiceStatus.dwCheckPoint = 0;
MyServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (MyServiceStatusHandle,
&MyServiceStatus))
{
status = GetLastError();
SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status);
}
SvcDebugOut(" [MY_SERVICE] Leaving MyService \n",0);
return;
case SERVICE_CONTROL_INTERROGATE:
// Fall through to send current status.
break;
default:
SvcDebugOut(" [MY_SERVICE] Unrecognized opcode %ld\n",
Opcode);
}
// Send current status.
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
{
status = GetLastError();
SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status);
}
return;
}