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

牽著老婆滿街逛

嚴以律己,寬以待人. 三思而后行.
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国内精品久久| 国产日韩综合| 亚洲第一精品夜夜躁人人爽| 欧美精品v日韩精品v国产精品| 亚洲图片在线| 久久久激情视频| 亚洲天堂av综合网| 久久精品国产久精国产爱| 亚洲美女网站| 欧美一级理论性理论a| 亚洲国产高清视频| 亚洲天堂网在线观看| 亚洲福利在线视频| 亚洲网站在线播放| 亚洲精品国产精品乱码不99按摩| 亚洲午夜一区二区三区| 亚洲成色999久久网站| 亚洲私人影院在线观看| 亚洲国产精品久久精品怡红院| 99国产精品久久久久老师| 激情自拍一区| 亚洲天堂第二页| 亚洲理论在线观看| 久久黄金**| 亚洲欧美一区二区三区在线| 女同性一区二区三区人了人一| 性欧美超级视频| 欧美日韩免费在线| 亚洲二区在线| 国内成人精品视频| 亚洲淫性视频| 亚洲一区二区视频| 欧美精品色综合| 欧美高清成人| 激情久久久久久久| 欧美一区二区精品久久911| 亚洲视频电影在线| 欧美精品自拍偷拍动漫精品| 免费中文日韩| 一区在线免费观看| 欧美在线视频不卡| 久久国产精品亚洲va麻豆| 欧美日韩一区二| 91久久久久久国产精品| 亚洲国产美女| 蜜桃精品久久久久久久免费影院| 美国十次成人| 在线观看欧美日韩国产| 久久成人资源| 久久婷婷成人综合色| 国内精品久久久久影院 日本资源| 亚洲一区二区黄色| 欧美一区=区| 国产精品一区二区你懂得| 亚洲视频你懂的| 午夜在线电影亚洲一区| 国产模特精品视频久久久久 | 在线观看日韩精品| 久久久国产91| 亚洲高清在线观看| 日韩一级片网址| 欧美视频福利| 午夜精品久久久久久久99黑人| 欧美一区激情| 在线播放日韩专区| 蜜臀久久99精品久久久画质超高清| 欧美sm视频| 一区二区国产日产| 国产精品久久久久久久久久久久久久| 亚洲视频你懂的| 久久久www成人免费无遮挡大片| 国产综合精品一区| 欧美高清在线一区| 一区二区三区日韩欧美精品| 久久国产一区二区| 91久久国产综合久久| 欧美日韩亚洲一区二| 亚洲欧美日韩国产中文| 美日韩精品免费观看视频| 亚洲精品欧美一区二区三区| 国产精品久久久久久久久免费| 欧美一区二区播放| 亚洲人成在线观看| 欧美在线观看视频| 91久久中文| 国产美女扒开尿口久久久| 久久夜精品va视频免费观看| 夜夜爽av福利精品导航| 久久精品官网| 99亚洲视频| 国产日韩一区| 欧美金8天国| 欧美中文字幕第一页| 亚洲伦伦在线| 米奇777超碰欧美日韩亚洲| 亚洲天堂av高清| 亚洲高清在线播放| 国产老肥熟一区二区三区| 美日韩精品免费| 亚洲欧美综合国产精品一区| 亚洲国产精品久久久久婷婷老年| 久久精品国产视频| 亚洲影院色无极综合| 亚洲国产成人精品久久久国产成人一区 | 亚洲欧美日韩网| 亚洲国产三级| 久久久福利视频| 亚洲香蕉在线观看| 亚洲丰满在线| 国产一区二区三区无遮挡| 欧美日韩亚洲一区二区三区四区| 久久国产色av| 亚洲欧美日韩国产中文 | 久久久久久久尹人综合网亚洲| 一区二区三区视频在线看| 亚洲福利小视频| 影音先锋久久久| 国产丝袜美腿一区二区三区| 欧美三级特黄| 欧美日韩在线播放一区二区| 蜜臀99久久精品久久久久久软件 | 午夜精品美女自拍福到在线| 日韩一级欧洲| 亚洲免费观看视频| 91久久中文| 91久久国产综合久久蜜月精品 | 亚洲午夜影视影院在线观看| 日韩一区二区高清| 亚洲三级观看| 亚洲日本一区二区| 亚洲理伦电影| 99re6热在线精品视频播放速度| 亚洲国产导航| 亚洲欧洲一区二区三区久久| 亚洲国产精品久久久久秋霞影院 | 欧美一级久久久久久久大片| 午夜精品久久久久久久99热浪潮| 亚洲一区二区成人| 亚洲欧美在线一区二区| 欧美一区二区三区成人| 欧美在线视频在线播放完整版免费观看| 午夜精品亚洲| 久久精品国产亚洲一区二区| 久久久综合视频| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美 日韩 国产在线| 欧美国产精品久久| 欧美午夜视频| 国产日韩三区| 亚洲国产女人aaa毛片在线| 亚洲人成人99网站| 亚洲一区免费网站| 久久久精品午夜少妇| 欧美成人精品福利| 日韩视频在线一区二区三区| 亚洲一区二区三区欧美| 欧美在线视频一区| 女人色偷偷aa久久天堂| 欧美午夜精彩| 国产综合网站| 中日韩视频在线观看| 欧美一区二区三区在线免费观看| 久久久噜噜噜久久| 亚洲国产毛片完整版| 亚洲天堂激情| 蜜臀av性久久久久蜜臀aⅴ| 欧美日韩免费观看一区二区三区 | 99热精品在线观看| 久久久久久电影| 亚洲日韩欧美一区二区在线| 亚洲一区在线播放| 牛人盗摄一区二区三区视频| 国产精品入口尤物| 最新69国产成人精品视频免费| 亚洲欧美在线播放| 亚洲高清视频中文字幕| 亚洲影视综合| 欧美精品大片| 激情综合在线| 午夜日本精品| 亚洲欧洲一区二区三区| 久久激情综合网| 国产精品久久久久久影视| 亚洲激情在线播放| 久久精品首页| 亚洲天堂成人在线视频| 欧美激情成人在线视频| 激情久久五月| 久久精品女人的天堂av| 中文精品在线| 欧美日韩亚洲视频| 亚洲美女区一区| 欧美91福利在线观看|