📄 创建svchost_exe调用的服务原理与实践.htm
字号:
name of service
<BR> NULL,
// service name to display
<BR> SERVICE_ALL_ACCESS, //
desired access
<BR> SERVICE_WIN32_SHARE_PROCESS,
// service type
<BR> SERVICE_AUTO_START, //
start type
<BR> SERVICE_ERROR_NORMAL, //
error control type
<BR> bin, //
service's binary
<BR> NULL, //
no load ordering group
<BR> NULL, //
no tag identifier
<BR> NULL, //
no dependencies
<BR> NULL, //
LocalSystem account
<BR> NULL);
// no password <BR><BR> if (schService ==
NULL)<BR> {<BR> OutputString("CreateService(%s)
error %d", svcname, rc =
GetLastError());<BR> throw
"";<BR> }<BR> OutputString("CreateService(%s)
SUCCESS. Config it", svcname);
<BR><BR> CloseServiceHandle(schService);
<BR> CloseServiceHandle(hscm);
<BR><BR> //config
service<BR> hkRoot =
HKEY_LOCAL_MACHINE;<BR> strncpy(buff,
"SYSTEM\\CurrentControlSet\\Services\\", sizeof
buff);<BR> strncat(buff, svcname,
100);<BR> rc = RegOpenKeyEx(hkRoot, buff, 0,
KEY_ALL_ACCESS, &hkRoot);<BR> if(ERROR_SUCCESS
!=
rc)<BR> {<BR> OutputString("RegOpenKeyEx(%s)
KEY_SET_VALUE error %d.", svcname, rc);
<BR> throw
"";<BR> }<BR><BR> rc =
RegCreateKey(hkRoot, "Parameters",
&hkParam);<BR> SetLastError(rc);<BR> if(ERROR_SUCCESS
!= rc)<BR> throw
"RegCreateKey(Parameters)";<BR><BR> if(!GetModuleFileName(HMODULE(hDll),
buff, sizeof
buff))<BR> throw
"GetModuleFileName() get dll path";<BR><BR> rc =
RegSetValueEx(hkParam, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned
char*)buff,
strlen(buff)+1);<BR> SetLastError(rc);<BR> if(ERROR_SUCCESS
!= rc)<BR> throw
"RegSetValueEx(ServiceDll)";<BR><BR> OutputString("Config
service %s ok.", svcname); <BR> }catch(char
*str)<BR> {<BR> if(str
&&
str[0])<BR> {<BR> rc
=
GetLastError();<BR> OutputString("%s
error %d", str,
rc);<BR> }<BR> }<BR><BR> RegCloseKey(hkRoot);<BR> RegCloseKey(hkParam);<BR> CloseServiceHandle(schService);
<BR> CloseServiceHandle(hscm);
<BR><BR> return rc;<BR>}<BR><BR>/*<BR>used to
install by rundll32.exe<BR>Platform SDK: Tools - Rundll32<BR>The Run DLL
utility (Rundll32.exe) included in Windows enables you to call functions
exported from a 32-bit DLL. These functions must have the following
syntax:<BR>*/<BR>void CALLBACK RundllInstallA(<BR> HWND
hwnd, // handle to owner
window<BR> HINSTANCE hinst, // instance handle for
the DLL<BR> char
*param, // string the DLL
will parse<BR> int
nCmdShow // show
state<BR>)<BR>{<BR> InstallService(param);<BR>}<BR><BR><BR>int
UninstallService(char *name)<BR>{<BR> int rc =
0;<BR> SC_HANDLE
schService;<BR> SC_HANDLE
hscm;<BR><BR> __try{<BR> hscm
= OpenSCManager(NULL, NULL,
SC_MANAGER_ALL_ACCESS);<BR> if (hscm == NULL)
<BR> {<BR> OutputString("OpenSCManager()
error %d", rc = GetLastError() );
<BR> return
rc;<BR> }<BR><BR> char
*svcname = DEFAULT_SERVICE;<BR> if(name &&
name[0]) svcname = name;<BR><BR> schService =
OpenService(hscm, svcname, DELETE);<BR> if
(schService == NULL)
<BR> {<BR> OutputString("OpenService(%s)
error %d", svcname, rc = GetLastError() );
<BR> return
rc;<BR> }<BR><BR> if
(!DeleteService(schService) )
<BR> {<BR> OutputString("OpenService(%s)
error %d", svcname, rc = GetLastError() );
<BR> return
rc;<BR> }<BR><BR> OutputString("DeleteService(%s)
SUCCESS.", svcname);
<BR> }__except(1)<BR> {<BR> OutputString("Exception
Catched 0x%X",
GetExceptionCode());<BR> }<BR><BR> CloseServiceHandle(schService);
<BR> CloseServiceHandle(hscm);<BR> return
rc;<BR>}<BR><BR>/*<BR>used to uninstall by rundll32.exe<BR>Platform SDK:
Tools - Rundll32<BR>The Run DLL utility (Rundll32.exe) included in Windows
enables you to call functions exported from a 32-bit DLL. These functions
must have the following syntax:<BR>*/<BR>void CALLBACK
RundllUninstallA(<BR> HWND
hwnd, // handle to owner
window<BR> HINSTANCE hinst, // instance handle for
the DLL<BR> char
*param, // string the DLL
will parse<BR> int
nCmdShow // show
state<BR>)<BR>{<BR> UninstallService(param);<BR>}<BR><BR>//output
the debug infor into log file & DbgPrint<BR>void OutputString( char
*lpFmt, ... )<BR>{<BR> char
buff[1024];<BR> va_list arglist;<BR> va_start(
arglist, lpFmt );<BR> _vsnprintf( buff, sizeof
buff, lpFmt, arglist );<BR> va_end( arglist
);<BR><BR> DWORD
len;<BR> HANDLE herr =
GetStdHandle(STD_OUTPUT_HANDLE);<BR> if(herr !=
INVALID_HANDLE_VALUE)<BR> {<BR> WriteFile(herr,
buff, strlen(buff), &len,
NULL);<BR> WriteFile(herr,
"\r\n", 2, &len,
NULL);<BR> }else<BR> {<BR> FILE
*fp = fopen("SvcHost.DLL.log",
"a");<BR> if(fp)<BR> {<BR> char
date[20],
time[20];<BR> fprintf(fp,
"%s %s - %s\n", _strdate(date), _strtime(time),
buff);<BR> if(!stderr)
fclose(fp);<BR> }<BR> }<BR><BR> OutputDebugString(buff);<BR>}
</TD></TR></TBODY></TABLE>
<DIV class=footer>Copyright © 1998-2003 XFOCUS Team. All Rights Reserved
</DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -