VC编写Windows服务程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

#include

TCHAR szServ iceN ame[] = _T("My TestSer v ice"); DWORD dwThreadID;

HANDLE hEvent = INVALID_HANDLE_VALUE; SERVICE_STATUS s tatus;

SERVICE_STATUS_HANDLE hServ iceStatus;

v oi d Init();

BOOL IsSer v iceInstalled();

BOOL InstallSer v ice();

BOOL U ninstallSer v ice();

BOOL Ser v iceCtrlStart();

BOOL Ser v iceCtrlStop();

v oi d WINAPI Ser v iceMai n();

v oi d WINAPI Ser v iceStrl(DWORD dwOpc ode); v oi d LogEv ent(LPCTSTR pszFormat, ...);

//Usuage

// Ins tall ser v ice:

//TestServ ic e.exe /i nstall

// Uninstall ser v ice:

//TestServ ic e.exe /unins tall

// Start ser v ice:

//TestServ ic e.exe /s tart

// Stop s erv ic e:

//TestServ ic e.exe /s top

int main( int argc, char* argv[] )

{

Init();

dwT hreadID = ::GetCurrentThreadId();

SERVICE_TABLE_ENTRY st[] =

{

{ szServ ic eName, (LPSERVICE_MAIN_FUNCTION)Serv ic eMain }, { NULL, NULL }

};

LPSTR lpC mdLine = argv[1];

if ( argc == 2 )

{

BOOL bRet;

if (stricmp(lpCmdLine, "/ins tall") == 0)

{

bRet = InstallSer v ice();

if ( bRet == TRUE )

{

printf( "Ins tall ser v ice %s succ ess", szServ ic eName );

}else

{

printf( "Ins tall ser v ice %s failed", sz Ser v iceName );

}

}

els e if (stricmp(lpCmdLine, "/uninstall") == 0)

{

bRet = Uni nstallServ ic e();

if ( bRet == TRUE )

{

printf( "Uninstall ser v ice %s succ ess", szServ ic eName ); }else

{

printf( "Uninstall ser v ice %s failed", szServ ic eName );

}

}else if (stricmp(lpC mdLine, "/start") == 0)

{

bRet = Ser v iceCtrlStart();

if ( bRet == TRUE )

{

printf( "Start ser v ice %s succ ess", szServ ic eName );

}else

{

printf( "Start ser v ice %s fail ed", sz Ser v iceName );

}

}else if (stricmp(lpC mdLine, "/stop") == 0)

{

bRet = Ser v iceCtrlStop();

if ( bRet == TRUE )

{

printf( "Stop s er v ice %s s uccess", szSer v iceN ame );

}else

{

printf( "Stop s er v ice %s failed", szServ iceN ame );

}

}

els e

{

if (!::StartSer v iceCtrlDispatc her(st))

{

LogEv ent(_T("Register Ser v ice Main Function Error!")); }

}

}else

{

if (!::StartSer v iceCtrlDispatc her(st))

{

LogEv ent(_T("Register Ser v ice M ain Function Error!"));

}

}

return 0;

}

//初始化相关结构

v oi d Init()

{

hSer v iceStatus = NULL;

status.dwServ ic eT y pe = SERVICE_WIN32_OWN_PROC ESS; status.dwCurrentState = SERVIC E_STOPPED;

status.dwControlsAcc epted = SERVICE_ACCEPT_STOP;

相关文档
最新文档