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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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 楊粼波 閱讀(537) 評論(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>
            亚洲福利在线视频| 美女视频黄免费的久久| 亚洲精品在线一区二区| 欧美国产专区| 国产精品99久久久久久久久| 在线视频亚洲一区| 国产欧美日韩一区二区三区在线| 午夜精品久久久久久久久久久久| 亚洲网友自拍| 好看的av在线不卡观看| 亚洲国产精品成人一区二区| 欧美日韩一区二区三区在线 | 欧美午夜免费电影| 欧美在线日韩精品| 麻豆av一区二区三区| 一区二区三区**美女毛片| 亚洲自拍高清| 亚洲国产99| 一区二区三区四区五区精品| 国产亚洲欧美日韩日本| 亚洲片区在线| 国产亚洲一区二区三区在线播放| 欧美高清视频www夜色资源网| 欧美片在线播放| 久久精品日产第一区二区| 老牛嫩草一区二区三区日本| 亚洲淫片在线视频| 久久综合九色综合网站| 午夜视黄欧洲亚洲| 免费一级欧美在线大片| 性欧美大战久久久久久久免费观看 | 久久久999精品视频| 欧美大片一区| 久久天堂精品| 国产精品久久久久aaaa九色| 欧美激情一区二区久久久| 国产精品日韩高清| 亚洲精品日日夜夜| 好吊妞**欧美| 亚洲综合日韩中文字幕v在线| 亚洲人午夜精品免费| 欧美一区二区三区的| 一区二区欧美日韩| 欧美高清视频| 老妇喷水一区二区三区| 国产精品爽爽爽| 日韩一级成人av| 一本大道久久精品懂色aⅴ | 欧美电影打屁股sp| 国产视频一区在线| 在线视频你懂得一区| 亚洲免费电影在线观看| 久久综合给合| 欧美.com| 亚洲第一二三四五区| 久久久精品日韩| 久久九九热re6这里有精品| 国产精品三级视频| 亚洲一区二区高清视频| 亚洲一区二区欧美日韩| 欧美日韩国产欧美日美国产精品| 亚洲电影天堂av| 亚洲区一区二| 欧美jjzz| 亚洲九九爱视频| 99天天综合性| 国产精品盗摄一区二区三区| 99精品热视频只有精品10| aⅴ色国产欧美| 欧美色网一区二区| av成人免费| 欧美亚洲视频在线看网址| 国产欧美日韩一级| 久久精品国产一区二区三| 欧美成人精品一区二区| 亚洲狠狠婷婷| 欧美日韩不卡合集视频| 亚洲一级网站| 久久精品人人做人人爽| 在线看欧美日韩| 欧美成人精品不卡视频在线观看| 91久久精品视频| 性感少妇一区| 精品1区2区| 欧美精品九九99久久| 一区二区三区日韩欧美| 欧美综合第一页| 亚洲国产cao| 欧美视频在线观看一区| 午夜精彩国产免费不卡不顿大片| 久久一本综合频道| 亚洲精品在线二区| 国产精品一区二区三区免费观看| 久久手机精品视频| 日韩午夜一区| 久久免费黄色| 亚洲最新视频在线播放| 国产午夜精品一区理论片飘花 | 日韩一级片网址| 久久漫画官网| 一区二区日韩精品| 国产亚洲精品久久久久久| 久久综合电影一区| 亚洲在线视频观看| 亚洲视频axxx| 99精品久久久| 欧美一级大片在线免费观看| 韩国av一区二区三区在线观看| 在线免费高清一区二区三区| 在线观看成人网| 老**午夜毛片一区二区三区| 亚洲婷婷综合色高清在线| 免费高清在线一区| 欧美一区二区在线免费观看| 亚洲精品自在久久| 国产一区二区激情| 欧美日韩一区二区免费视频| 麻豆91精品| 久久精品欧美| 亚洲欧美精品| 一本一本久久a久久精品综合麻豆| 免费人成精品欧美精品| 久久国产一区二区三区| 这里只有精品丝袜| 亚洲精品日产精品乱码不卡| 在线观看视频亚洲| 国模 一区 二区 三区| 国产精品视频网址| 欧美午夜欧美| 欧美视频在线观看视频极品| 欧美日韩高清在线观看| 欧美激情久久久久久| 久热精品视频在线| 久久蜜桃精品| 蜜臀久久99精品久久久久久9| 久久精品国产亚洲一区二区| 久久精品99| 久久久精品免费视频| 久久久久免费视频| 久久人91精品久久久久久不卡| 久久国产精品免费一区| 欧美伊久线香蕉线新在线| 亚洲女性喷水在线观看一区| 亚洲自拍高清| 欧美一区二区三区在线播放| 亚久久调教视频| 久久精品国产成人| 久久久久久久一区二区三区| 久久人人97超碰人人澡爱香蕉 | 久久久久九九九九| 久久精品理论片| 欧美专区在线观看一区| 久久久久天天天天| 老**午夜毛片一区二区三区| 欧美激情亚洲国产| 亚洲美女在线观看| 亚洲无亚洲人成网站77777| 亚洲综合电影| 在线看视频不卡| 一区二区欧美激情| 欧美一级视频精品观看| 久久精品亚洲热| 亚洲高清免费在线| 99精品国产在热久久| 性xx色xx综合久久久xx| 久久亚洲捆绑美女| 欧美日本高清| 国产欧美综合在线| 亚洲国产一区二区精品专区| 一区二区三区高清在线观看| 性欧美video另类hd性玩具| 久久午夜精品| 亚洲免费观看在线视频| 欧美一区二区视频观看视频| 欧美成人在线网站| 国产精品一区二区三区乱码| 亚洲国产精品久久久久秋霞蜜臀 | 在线播放不卡| 亚洲视频高清| 美女主播精品视频一二三四| 亚洲人成欧美中文字幕| 欧美一区二区三区视频免费| 欧美激情综合五月色丁香| 国产欧美一区二区三区在线看蜜臀 | 亚洲精品一区二区三| 香蕉亚洲视频| 亚洲国产欧美一区二区三区丁香婷| 在线视频日韩| 欧美黄色一区二区| 激情综合视频| 欧美一激情一区二区三区| 最新69国产成人精品视频免费| 欧美在线免费看| 国产精品国产三级国产普通话三级 | 欧美1区2区| 性高湖久久久久久久久| 国产精品久久久久久久久婷婷| 亚洲国产另类 国产精品国产免费| 欧美一区二区三区免费视频| 9l视频自拍蝌蚪9l视频成人|