QQ1321.CN 努力为您提供一个时尚的QQ空间代码交流平台!
一生愛妳QQ空间站
您当前的位置:QQ空间代码|QQ空间模块|QQ图标|QQ空间互踩|QQ免费资源 >> QQ空间代码 >> 文章内容 ╰☆╮文字广告位 ☏非主流同城缘分 ☜网友自由发布空间模块平台☞
创建SvcHost.exe 调用的服务原理与实践(5)
作者:郁郁小蝎 来源:中国站长学院(QQ1321)原创Www.QQ1321.Cn 收藏到QQ书签 【添加到百度搜藏】  推荐好友
8. 代码
// SvcHostDLL.cpp : Demo for a service dll used by svchost.exe to host it.
//
// for detail comment see articles.
// by bingle_at_email.com.cn
//www.BingleSite.net
//
/* save following as a .def file to export function, only ServiceMain is needed.
other used to install & uninstall service.
or use /EXPORT: link option to export them.

EXPORTS
ServiceMain
InstallService
UninstallService
RundllUninstallA
RundllInstallA
*/
/*
To compile & link:
cl /MD /GX /LD svchostdll.cpp /link advapi32.lib /DLL /base:0x71000000 /export:ServiceMain EXPORT:RundllUninstallA /EXPORT:RundllInstallA /EXPORT:InstallService /EXPORT:UninstallService
*/

//
//Articles:
// 1. HOWTO Create a service dll used by svchost.exe by bingle, at: http://www.BingleSite.net/article/svchost-dll-service.html
// 2. Inside Win32 Services, Part 2 by: Mark Russinovich, at: http://www.winnetmag.com/Articles/Index.cfm?ArticleID=8943&pg=3
// 3. Platform SDK: Tools - Rundll32, at: http://msdn.microsoft.com/library/en-us/tools/tools/rundll32.asp

#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <windows.h>

#define DEFAULT_SERVICE "IPRIP"
#define MY_EXECUTE_NAME "SvcHostDLL.exe"

//main service process function
void __stdcall ServiceMain( int argc, wchar_t* argv[] );
//report service stat to the service control manager
int TellSCM( DWORD dwState, DWORD dwExitCode, DWORD dwProgress );
//service control handler, call back by service control manager
void __stdcall ServiceHandler( DWORD dwCommand );
//RealService just create a process
int RealService(char *cmd, int bInteract);

//Install this dll as a Service host by svchost.exe, service name is given by caller
int InstallService(char *name);
//unInstall a Service, be CARE FOR call this to delete a service
int UninstallService(char *name);
//Install this dll as a Service host by svchost.exe, used by RUNDLL32.EXE to call
void CALLBACK RundllInstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);
//unInstall a Service used by RUNDLL32.EXE to call, be CARE FOR call this to delete a service
void CALLBACK RundllUninstallA(HWND hwnd, HINSTANCE hinst, char *param, int nCmdShow);

//output the debug infor into log file(or stderr if a console program call me) & DbgPrint
void OutputString( char *lpFmt, ... );


//dll module handle used to get dll path in InstallService
HANDLE hDll = NULL;
//Service HANDLE & STATUS used to get service state
SERVICE_STATUS_HANDLE hSrv;
DWORD dwCurrState;


BOOL APIENTRY DllMain( HANDLE hModule,
 DWORDul_reason_for_call,
 LPVOID lpReserved
 )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDll = hModule;
#ifdef _DEBUG
AllocConsole();
OutputString("SvcHostDLL: DllMain called DLL_PROCESS_ATTACH");
break;

 case DLL_THREAD_ATTACH:
OutputString("SvcHostDLL: DllMain called DLL_THREAD_ATTACH");
case DLL_THREAD_DETACH:
OutputString("SvcHostDLL: DllMain called DLL_THREAD_DETACH");
case DLL_PROCESS_DETACH:
TellSCM( SERVICE_STOP_PENDING, 0, 0 );
Sleep(1500);
TellSCM( SERVICE_STOPPED, 0, 0 );
OutputString("SvcHostDLL: DllMain called DLL_PROCESS_DETACH");
#endif
break;
}

 return TRUE;
}

上篇内容:文章:创建SvcHost.exe 调用的服务原理与实践(6)
下篇内容:文章:创建SvcHost.exe 调用的服务原理与实践(4)
将本文收藏到QQ书签中,以便日后查看或与好友分享
代码阅读排行
· 入侵检测及网络安全发展技术探讨(3)...
· 在Windows XP中如何取得文件或文件夹...
· Win2003“安全事件ID”分析(2)
· Win2003“安全事件ID”分析(3)
· 清除Security列表中的未知用户
· 电脑高手常用的五个按钮
站长精彩推荐
精彩推荐内容
· 何谓HTTP 500内部服务器错误


版权所有:一生愛妳┆QQ空间代码酷站 最佳分辨率 IE6.0 1024×768 备案中 站长(信仰自由)QQ:271443991
未经本站授权严禁建立镜像.本站部分资源来源于网络,版权归原作者所有 欢迎任何建议、批评和鼓励
Copyright © 2007-2008 信仰自由 All Rights Reserved .
收藏此页 - 推荐好友 - 友情连接 - - 信仰自由 - 联系我们 - 网站地图 - 关于我们