青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

How to Create a Windows NT/ Windows 2000 Service(如何創建一個Windows NT/ Windows 2000 服務)

How to Create a Windows NT/ Windows 2000 Service
By
C.V Anish

How to create a Windows NT/ Windows 2000 Service using VC++.?

Introduction

A Windows service is an EXE specially designed to communicate with the SCM (Service Control Manager) of Windows NT/2000. The Service Control Manager (SCM) maintains a database of installed services and driver services, and provides a unified and secure means of controlling them. SCM is started at system boot and it is a remote procedure call (RPC) server. As a developer to try a simple service, we can divide the program into four parts.

  1. Main program of Win32 / Console Application.
  2. A so called ServiceMain(), main program of Service. Entry point of a service.
  3. A Service Control Handler, a function to communicate with SCM.
  4. A Service Installer/ Uninstaller, to register an EXE as a Service.

Firstly, let us take a look at the Main program of the Console application (it can also be a WinMain()).

#include? " Winsvc.h " ? // Header?file?for?Services.
main()
{
??SERVICE_TABLE_ENTRY?Table[]
= { { " Service1 " ,ServiceMain} , {NULL,NULL} } ;
??StartServiceCtrlDispatcher(Table);
}

The only thing done by the main() is to fill a SERVICE_TABLE_ENTRY array. The position [0][0] contains the name of the Service (any string you like). Position [0][1] contains the name of the Service Main function, I specified in the list earlier. It actually is a function pointer to the Service main function. The name can be any thing. Now we start the first step to a service by calling StartServiceCtrlDispatcher() with the SERVICE_TABLE_ENTRY array. Note that the function signature should be of the form. The [1][0] and [1][1] positions are NULL, just to say the end of the array (not a must). We can add more entries to the list if we have more than one service running from the same EXE.

The declaration of a typical ServiceMain():

? void ?WINAPI?ServiceMain(DWORD?argc,?LPTSTR? * argv) </ PRE >

Now, let us analyze our ServiceMain function.

The main steps of this function are:

  1. Fill the SERVICE_STATUS structure with appropriate values to communicate with the SCM.
  2. Register the Service Control Handler function said earlier in the list.
  3. Call the actual processing functions.

For proceeding, we need two global variables here:

  • SERVICE_STATUS m_ServiceStatus;
  • SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

The ServiceMain() can accept command line arguments just as any C++ main() function. The first parameter contains the number of arguments being passed to the service. There will always be at least one argument. The second parameter is a pointer to an array of string pointers. The first item in the array always points to the service name. The SERVICE_STATUS data structure is used to fill the current state of the Service and notify it to the SCM. We use an API function SetServiceStatus() for the purpose. The data members of SERVICE_STATUS to look for are:

< PRE > dwServiceType? = ?SERVICE_WIN32;?
dwCurrentState?
= ?SERVICE_START_PENDING;? // Means?Trying?To?Start(Initially)</PRE>

dwControlsAccepted = SERVICE_ACCEPT_STOP; accepts Stop/Start only in Service control program, usually in the Control Panel (NT) / Administrative tools (2000). We can also set our service to accept PAUSE and CONTINUE functionality.

In the beginning of the ServiceMain(), we should set the dwCurrentState of SERVICE_STATUS to SERVICE_START_PENDING. This signals the SCM that the service is starting. If any error occurs in the way, we should notify the SCM by passing SERVICE_STOPPED. By default, the SCM will look for an activity from the service and if it fails to show any progress within 2 minutes, SCM kills that service.

The API function RegisterServiceCtrlHandler() is used to set the Service Control Handler Function of the Service with the SCM. The function takes two parameters as earlier, one service name (string) and the pointer to the Service Control Handler Function. That function should be with the signature.

Once we get till here, we now set dwCurrentState as SERVICE_RUNNING to notify that the service has started to function. The next step is to call the actual processing steps.

Now, let us analyze our Service Control Handler function:

The Service Control Handler function is used by the SCM to communicate to the Service program about a user action on the service, like a start, stop, pause or continue. It basically contains a switch statement to deal with each case. Here, we will call appropriate steps to clean up and terminate the process. This function receives an opcode which can have values like SERVICE_CONTROL_PAUSE, SERVICE_CONTROL_CONTINUE, SERVICE_CONTROL_STOP, SERVICE_CONTROL_INTERROGATE etc. We have to write appropriate steps on each.

Now Service Installer/ Uninstaller

For installing a service, we need to make some entries in the system registry. Windows has some APIs to do these steps, instead of using the registry functions. They are CreateService() and DeleteService(). For both these functions, we need to open the SCM database with appropriate rights. I prefer SC_MANAGER_ALL_ACCESS. For installing a service, first open the SCM by OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS). Then invoke the CreateService() with appropriate binary file path of our service. Here also, we have to give the name of our service. We need this name if we want to delete a particular service. In deleting a service, we need to open the specific service first by its name and then invoke the DeleteService() on it. That’s all what we need. Take a look at the code given with it for more details.

Thank You

Anish C.V.

The Code Goes Here:

??#include? " stdafx.h "
#include?
" Windows.h "
#include?
" Winsvc.h "
#include?
" time.h "

SERVICE_STATUS?m_ServiceStatus;
SERVICE_STATUS_HANDLE?m_ServiceStatusHandle;
BOOL?bRunning
= true ;
void ?WINAPI?ServiceMain(DWORD?argc,?LPTSTR? * argv);
void ?WINAPI?ServiceCtrlHandler(DWORD?Opcode);
BOOL?InstallService();
BOOL?DeleteService();
int ?main( int ?argc,? char * ?argv[])
{
??
if (argc > 1 )
??
{
????
if (strcmp(argv[ 1 ], " -i " ) == 0 )
????
{
??????
if (InstallService())
????????printf(
" \n\nService?Installed?Sucessfully\n " );
??????
else
????????printf(
" \n\nError?Installing?Service\n " );
????}

????
if (strcmp(argv[ 1 ], " -d " ) == 0 )
????
{
??????
if (DeleteService())
????????printf(
" \n\nService?UnInstalled?Sucessfully\n " );
??????
else
????????printf(
" \n\nError?UnInstalling?Service\n " );
????}

????
else
????
{
??????printf(
" \n\nUnknown?Switch?Usage\n\nFor?Install?
???????????use?Srv1? - i\n\nFor?UnInstall?use?Srv1? - d\n " );
????}

??}

??
else
??
{
????SERVICE_TABLE_ENTRY?DispatchTable[]
=
????????????????
{ { " Service1 " ,ServiceMain} , {NULL,NULL} } ;
????StartServiceCtrlDispatcher(DispatchTable);
??}

??
return ? 0 ;
}


void ?WINAPI?ServiceMain(DWORD?argc,?LPTSTR? * argv)
{
??DWORD?status;
??DWORD?specificError;
??m_ServiceStatus.dwServiceType?
= ?SERVICE_WIN32;
??m_ServiceStatus.dwCurrentState?
= ?SERVICE_START_PENDING;
??m_ServiceStatus.dwControlsAccepted?
= ?SERVICE_ACCEPT_STOP;
??m_ServiceStatus.dwWin32ExitCode?
= ? 0 ;
??m_ServiceStatus.dwServiceSpecificExitCode?
= ? 0 ;
??m_ServiceStatus.dwCheckPoint?
= ? 0 ;
??m_ServiceStatus.dwWaitHint?
= ? 0 ;

??m_ServiceStatusHandle?
= ?RegisterServiceCtrlHandler( " Service1 " ,?
????????????????????????????????????????????ServiceCtrlHandler);?
??
if ?(m_ServiceStatusHandle? == ?(SERVICE_STATUS_HANDLE) 0 )
??
{
????
return ;
??}

??m_ServiceStatus.dwCurrentState?
= ?SERVICE_RUNNING;
??m_ServiceStatus.dwCheckPoint?
= ? 0 ;
??m_ServiceStatus.dwWaitHint?
= ? 0 ;
??
if ?( ! SetServiceStatus?(m_ServiceStatusHandle,? & m_ServiceStatus))
??
{
??}


??bRunning
= true ;
??
while (bRunning)
??
{
????Sleep(
3000 );
????
// Place?Your?Code?for?processing?here.
??}

??
return ;
}


void ?WINAPI?ServiceCtrlHandler(DWORD?Opcode)
{
??
switch (Opcode)
??
{
????
case ?SERVICE_CONTROL_PAUSE:?
??????m_ServiceStatus.dwCurrentState?
= ?SERVICE_PAUSED;
??????
break ;
????
case ?SERVICE_CONTROL_CONTINUE:
??????m_ServiceStatus.dwCurrentState?
= ?SERVICE_RUNNING;
??????
break ;
????
case ?SERVICE_CONTROL_STOP:
??????m_ServiceStatus.dwWin32ExitCode?
= ? 0 ;
??????m_ServiceStatus.dwCurrentState?
= ?SERVICE_STOPPED;
??????m_ServiceStatus.dwCheckPoint?
= ? 0 ;
??????m_ServiceStatus.dwWaitHint?
= ? 0 ;

??????SetServiceStatus?(m_ServiceStatusHandle,
& m_ServiceStatus);
??????bRunning
= false ;
??????
break ;
????
case ?SERVICE_CONTROL_INTERROGATE:
??????
break ;?
??}

??
return ;
}


BOOL?InstallService()
{
??
char ?strDir[ 1024 ];
??HANDLE?schSCManager,schService;
??GetCurrentDirectory(
1024 ,strDir);
??strcat(strDir,
" \\Srv1.exe " );?
??schSCManager?
= ?OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

??
if ?(schSCManager? == ?NULL)?
????
return ? false ;
??LPCTSTR?lpszBinaryPathName
= strDir;

??schService?
= ?CreateService(schSCManager, " Service1 " ,?
????????
" The?Display?Name?Needed " ,? // ?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);
??
return ? true ;
}


BOOL?DeleteService()
{
??HANDLE?schSCManager;
??SC_HANDLE?hService;
??schSCManager?
= ?OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);

??
if ?(schSCManager? == ?NULL)
????
return ? false ;
??hService
= OpenService(schSCManager, " Service1 " ,SERVICE_ALL_ACCESS);
??
if ?(hService? == ?NULL)
????
return ? false ;
??
if (DeleteService(hService) == 0 )
????
return ? false ;
??
if (CloseServiceHandle(hService) == 0 )
????
return ? false ;

return ? true ;
}

About C.V Anish


A Developer from India. Concentrating on the Microsoft Technologies. VC++ and VB.

Click here to view C.V Anish's

posted on 2006-04-14 21:38 楊粼波 閱讀(548) 評論(1)  編輯 收藏 引用 所屬分類: 文章收藏

評論

# re: How to Create a Windows NT/ Windows 2000 Service(如何創建一個Windows NT/ Windows 2000 服務) 2006-04-14 21:39 天下奇毒

原文請見:http://www.codeproject.com/system/windows_nt_service.asp

我會把它翻譯出來的,呵呵....  回復  更多評論   

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲一区日韩在线| 91久久精品视频| 欧美成人精品一区| 久久不射网站| 欧美乱在线观看| 欧美国产在线观看| 很黄很黄激情成人| 午夜精品av| 亚洲主播在线观看| 欧美风情在线| 欧美成人中文字幕| 韩日在线一区| 欧美一区中文字幕| 午夜在线电影亚洲一区| 欧美日韩国产区| 亚洲国产精品成人一区二区| 韩曰欧美视频免费观看| 欧美一级大片在线免费观看| 午夜精品视频在线观看| 欧美天堂亚洲电影院在线观看 | 美女主播视频一区| 国产视频在线观看一区| 亚洲欧美激情四射在线日| 亚洲综合日韩中文字幕v在线| 欧美另类变人与禽xxxxx| 亚洲国产精品高清久久久| 亚洲国产成人高清精品| 麻豆精品91| 亚洲高清视频一区| 亚洲人体1000| 欧美日韩精品三区| 在线一区二区三区四区| 亚洲综合精品四区| 国产偷自视频区视频一区二区| 亚洲一区二区三区免费观看 | 久久精品国产精品亚洲| 国产精品久久久一区二区| 亚洲小说欧美另类婷婷| 午夜精品视频| 国产一区二区三区高清| 久久久久久久综合色一本| 欧美国产日产韩国视频| 亚洲美女精品久久| 欧美视频福利| 午夜一级久久| 女同一区二区| 一本色道久久88精品综合| 欧美午夜一区二区福利视频| 亚洲欧美日韩国产一区| 久久久精品视频成人| 亚洲国产日韩美| 欧美日韩在线播放一区| 午夜视频精品| 欧美激情第8页| 亚洲一区二区三区久久| 国产日韩欧美黄色| 牛牛精品成人免费视频| 亚洲午夜久久久久久久久电影院| 久久不见久久见免费视频1| 亚洲国产美女| 欧美性感一类影片在线播放| 欧美在线一二三四区| 亚洲大胆人体视频| 亚洲欧美日韩在线| 国模私拍一区二区三区| 欧美激情aⅴ一区二区三区| 亚洲综合二区| 亚洲国产精品久久久久秋霞蜜臀 | 亚洲国产cao| 午夜精品久久久久久久99黑人| 国产日韩欧美黄色| 欧美人与性动交cc0o| 欧美在线国产| 99视频精品免费观看| 蜜桃久久精品一区二区| 午夜精品久久久久久久99黑人| 亚洲缚视频在线观看| 国产精品一区一区| 欧美激情无毛| 久久男女视频| 性娇小13――14欧美| 99av国产精品欲麻豆| 久久久亚洲成人| 亚洲免费在线视频一区 二区| 亚洲国产精品一区二区三区| 国产麻豆精品theporn| 欧美午夜免费影院| 欧美国产精品中文字幕| 久久久久久9999| 欧美在线二区| 亚洲在线国产日韩欧美| 99国内精品久久| 亚洲激情成人网| 欧美成人日本| 欧美www视频| 久久亚洲精品视频| 久久成人av少妇免费| 欧美一区视频| 性视频1819p久久| 亚洲免费影院| 亚洲在线网站| 亚洲中字黄色| 午夜久久tv| 新67194成人永久网站| 午夜激情综合网| 香蕉久久精品日日躁夜夜躁| 亚洲一区二区三区午夜| 亚洲一区二区三区免费观看 | 久久国产一区二区三区| 午夜欧美大尺度福利影院在线看| 一区二区三区久久久| 亚洲天堂久久| 亚洲香蕉网站| 亚洲欧美制服另类日韩| 亚洲欧美日韩在线| 久久国产精品99国产精| 久久久国产精品一区二区三区| 久久激情视频| 免费观看日韩| 欧美巨乳在线| 国产精品成人一区二区艾草| 国产精品美女一区二区| 国产欧美一区在线| 国内精品久久久久久影视8| 伊人久久大香线蕉av超碰演员| 亚洲电影观看| 99精品国产热久久91蜜凸| 亚洲少妇中出一区| 欧美一区91| 欧美不卡一卡二卡免费版| 亚洲国产精品黑人久久久| av不卡免费看| 欧美一级午夜免费电影| 久久―日本道色综合久久| 欧美国产精品中文字幕| 欧美午夜不卡| 国产资源精品在线观看| 亚洲精品乱码久久久久久| 亚洲视频在线观看| 久久手机免费观看| 亚洲精品久久久久久久久久久久久| 一区二区三区不卡视频在线观看 | 欧美一区二区免费观在线| 久久婷婷色综合| 欧美日韩国内自拍| 国产亚洲欧美另类一区二区三区| 在线国产精品播放| 亚洲婷婷综合久久一本伊一区| 久久黄色级2电影| 亚洲人成小说网站色在线| 亚洲曰本av电影| 欧美成人精品一区二区| 国产区精品视频| 日韩一区二区福利| 久久久av网站| 日韩一二三区视频| 美女日韩在线中文字幕| 国产精品日韩在线观看| 亚洲日本中文字幕| 久久久久久久综合狠狠综合| 亚洲人成77777在线观看网| 欧美一区=区| 欧美性事在线| 亚洲精品免费网站| 老**午夜毛片一区二区三区| 一区二区三区www| 欧美va亚洲va香蕉在线| 国产亚洲精品资源在线26u| 亚洲天堂激情| 亚洲欧洲日本专区| 久久夜色精品国产亚洲aⅴ| 国产日韩欧美精品一区| 亚洲午夜成aⅴ人片| 欧美成人性网| 久久精品一区二区三区四区 | 欧美精品国产| 亚洲高清在线观看一区| 久久影视精品| 欧美一区二视频在线免费观看| 欧美午夜视频网站| 99精品免费网| 亚洲大片免费看| 久久久噜噜噜久噜久久| 国产亚洲一区二区三区在线播放| 亚洲制服少妇| 亚洲另类在线一区| 欧美精品一区二区三区久久久竹菊| 在线播放不卡| 免费不卡在线观看| 久久久久久成人| 亚洲电影成人| 欧美不卡三区| 欧美成人精品福利| 91久久精品美女| 亚洲观看高清完整版在线观看| 蜜臀a∨国产成人精品| 亚洲激情在线观看| 亚洲国产一区二区三区在线播| 欧美成人免费全部| 99视频超级精品|