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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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>
            欧美一区二区成人6969| 久久久国产一区二区| 在线观看欧美一区| 亚洲一区二区三区四区五区午夜| 激情欧美日韩一区| 亚洲综合色网站| 亚洲深爱激情| 欧美精品免费在线观看| 免费在线欧美视频| 国产伊人精品| 欧美一二三视频| 亚洲自拍偷拍视频| 欧美丝袜第一区| 日韩亚洲一区在线播放| 夜夜嗨av一区二区三区| 欧美sm极限捆绑bd| 欧美国产一区二区在线观看| 精久久久久久| 久久久久久午夜| 久久夜色精品国产欧美乱| 国产亚洲综合在线| 欧美专区在线| 免费成人在线观看视频| 在线观看精品视频| 蜜臀91精品一区二区三区| 欧美成人国产| 亚洲黄色在线| 欧美华人在线视频| 91久久精品美女高潮| 最新国产成人av网站网址麻豆| 久久久亚洲人| 亚洲成人在线网| 久久国产免费| 夜夜嗨av一区二区三区网站四季av| 男同欧美伦乱| 亚洲乱亚洲高清| 亚洲综合不卡| 国产深夜精品福利| 老司机免费视频一区二区三区 | 最近中文字幕日韩精品 | 欧美a级在线| 最近中文字幕mv在线一区二区三区四区 | 欧美精品情趣视频| 日韩亚洲在线观看| 欧美一区二区啪啪| 一区视频在线播放| 欧美极品一区| 亚洲欧美在线免费观看| 久久这里只有| 一区二区三区国产在线| 国产精品揄拍500视频| 久久久久久久久久看片| 亚洲激情啪啪| 欧美专区亚洲专区| 亚洲国产影院| 国产精品午夜在线观看| 老司机精品福利视频| 一区二区国产在线观看| 久久精品中文字幕免费mv| 亚洲电影在线播放| 午夜精品国产更新| 欧美黄在线观看| 亚洲欧美综合网| 亚洲国产视频直播| 国产精品揄拍500视频| 牛人盗摄一区二区三区视频| 亚洲午夜一区| 亚洲人成毛片在线播放| 久久精品国产一区二区电影| 国产精品视频网| 免费成人黄色| 性色av一区二区怡红| 亚洲激情在线播放| 久久久天天操| 午夜影视日本亚洲欧洲精品| 亚洲美女黄色片| 国模精品娜娜一二三区| 欧美日韩中文字幕在线视频| 久久久天天操| 先锋亚洲精品| 亚洲视频综合| 亚洲黄色一区二区三区| 久久久天天操| 欧美在线网站| 性欧美xxxx视频在线观看| 99热免费精品在线观看| 亚洲福利视频网| 含羞草久久爱69一区| 欧美深夜影院| 欧美日韩亚洲一区二区三区在线 | 国产中文一区| 国产久一道中文一区| 欧美日韩一本到| 欧美美女福利视频| 欧美高清视频一区二区| 卡一卡二国产精品| 久久婷婷丁香| 久久久久久亚洲精品中文字幕| 亚洲欧美国产精品va在线观看| 一级日韩一区在线观看| 亚洲精品视频在线播放| 亚洲欧洲美洲综合色网| 亚洲国产精品成人精品| 欧美国产另类| 亚洲亚洲精品三区日韩精品在线视频 | 99re热这里只有精品视频| 亚洲国产日韩一区二区| 欧美黄在线观看| 欧美国产乱视频| 欧美国产亚洲另类动漫| 欧美激情麻豆| 亚洲国产美女| 亚洲每日更新| 一区二区三区免费网站| 亚洲视频www| 午夜精品亚洲一区二区三区嫩草| 亚洲一区美女视频在线观看免费| 亚洲一区二区精品| 香蕉成人久久| 久久精品视频在线免费观看| 久久精品在线免费观看| 老司机成人在线视频| 欧美激情一区二区三区四区| 欧美日韩无遮挡| 国产精品手机视频| 狠狠色伊人亚洲综合成人| 亚洲国产人成综合网站| 日韩图片一区| 欧美亚洲一区| 欧美大尺度在线观看| 亚洲欧洲综合| 亚洲欧美日韩在线| 久久精品国产精品| 牛夜精品久久久久久久99黑人| 欧美日韩一级大片网址| 国产欧美日韩在线| 伊甸园精品99久久久久久| 日韩一二三区视频| 香蕉久久夜色| 亚洲国产裸拍裸体视频在线观看乱了中文 | 欧美激情一区二区三区成人 | 久久婷婷国产综合精品青草| 欧美人与禽猛交乱配| 国产农村妇女精品| 亚洲欧洲在线视频| 亚洲综合丁香| 欧美电影电视剧在线观看| 夜色激情一区二区| 久久久精品国产99久久精品芒果| 欧美精品在线网站| 精东粉嫩av免费一区二区三区| 艳女tv在线观看国产一区| 久久精品一本久久99精品| 亚洲高清二区| 性欧美大战久久久久久久免费观看| 欧美成人免费在线观看| 国产亚洲aⅴaaaaaa毛片| 夜夜爽夜夜爽精品视频| 久久久视频精品| 中文在线资源观看视频网站免费不卡| 欧美伊人久久大香线蕉综合69| 欧美欧美在线| 欲香欲色天天天综合和网| 午夜精品久久久久久99热| 亚洲第一精品夜夜躁人人爽| 欧美在线视频一区二区三区| 欧美猛交免费看| 亚洲国产婷婷香蕉久久久久久99 | 一二三区精品| 欧美精品亚洲二区| 亚洲福利视频一区二区| 久久国产夜色精品鲁鲁99| 亚洲精品一区在线观看| 狼人社综合社区| 国产亚洲一区二区三区在线播放| 亚洲一级黄色片| 亚洲每日更新| 欧美日本韩国一区二区三区| 亚洲激情欧美激情| 免费观看一区| 久久九九精品| 精品99一区二区| 老司机免费视频一区二区三区| 亚洲欧美日韩一区在线观看| 国产精品久久久亚洲一区| 亚洲一区二区三区免费视频| 日韩一本二本av| 欧美日韩视频第一区| 99国产精品久久| 亚洲区欧美区| 欧美精品乱人伦久久久久久| 亚洲免费精彩视频| 亚洲精品国产系列| 欧美精品一区二区蜜臀亚洲| 亚洲精品一二区| 日韩亚洲欧美在线观看| 国产精品v欧美精品v日韩 | 在线视频一区二区| 亚洲免费成人av| 欧美午夜片欧美片在线观看|