最近比较忙,呵呵,有一段时间没上线了,碰到一个问题,请大家帮帮忙,谢谢!!
我用VC写了一个DLL服务,服务可以正常加载,但是弹不出对话框是为什么?
源代码如下:
service.dll
#include "stdafx.h"
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#include <winbase.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
VOID WINAPI MyHandler(DWORD fdwControl);
SERVICE_STATUS service_status;
SERVICE_STATUS_HANDLE handle;
BOOL initService()
{
return true;
}
VOID WINAPI MyHandler(DWORD fdwControl)
{
if(fdwControl == SERVICE_CONTROL_STOP){
service_status.dwWin32ExitCode = 0;
service_status.dwCurrentState = SERVICE_STOPPED;
}
else if(fdwControl == SERVICE_CONTROL_SHUTDOWN){
service_status.dwWin32ExitCode = 0;
service_status.dwCurrentState = SERVICE_STOPPED;
}
SetServiceStatus(handle,&service_status);
}
VOID WINAPI ServiceMain(DWORD dwArgc,LPTSTR* lpszArgv)
{
service_status.dwCheckPoint = 0;
service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN|SERVICE_ACCEPT_STOP;
service_status.dwCurrentState = SERVICE_START_PENDING;
service_status.dwServiceSpecificExitCode = 0;
service_status.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
service_status.dwWaitHint = 0;
service_status.dwWin32ExitCode = 0;
handle = ::RegisterServiceCtrlHandler("DHCP Server",MyHandler);
if(handle ==(SERVICE_STATUS_HANDLE)0)
{
return;
}
if(!initService())
{
service_status.dwCurrentState = SERVICE_STOPPED;
service_status.dwWin32ExitCode = -1;
::SetServiceStatus(handle,&service_status);
return;
}
service_status.dwCurrentState = SERVICE_RUNNING;
::SetServiceStatus(handle,&service_status);
OutputDebugString("服务启动");
while(service_status.dwCurrentState == SERVICE_RUNNING)
{
OutputDebugString("输出开始");
MessageBox(NULL,"哈哈","提示",MB_OK);
OutputDebugString("输出完毕");
}
}
VOID __stdcall Installer()
{
SC_HANDLE handle = NULL;
handle = OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if(NULL == handle)
{
return;
}
if(NULL==::CreateService(handle,"DHCP Server","DHCP Server",SC_MANAGER_ALL_ACCESS,SERVICE_WIN32_SHARE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,"%SystemRoot%\\system32\\svchost -k ripss",NULL,NULL,NULL,NULL,NULL))
{
}
HKEY hkey = NULL;
HKEY pkey = NULL;
::RegOpenKey(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\DHCP Server",&hkey);
if(NULL !=hkey)
{
char buff[] = "%SystemRoot%\\system32\\service.dll";
::RegCreateKey(hkey,"Parameters",&pkey);
if(ERROR_SUCCESS != (RegSetValueEx(pkey, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned char*)buff, strlen(buff)+1)))
{
}
::RegCloseKey(pkey);
::RegCloseKey(hkey);
}
hkey = NULL;
RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SvcHost",&hkey);
if(hkey != NULL)
{
if(ERROR_SUCCESS != ::RegSetValueEx(hkey,"ripss",0,REG_MULTI_SZ,(BYTE*)"DHCP Server",strlen("DHCP Server")+1))
{
}
::RegCloseKey(hkey);
}
::CloseHandle(handle);
}
VOID __stdcall Unstaller()
{
SC_HANDLE handle = NULL;
SC_HANDLE handle_sv = NULL;
__try{
handle = OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if(NULL == handle)
{
return;
}
handle_sv = OpenService(handle,"DHCP Server",SC_MANAGER_ALL_ACCESS);
if(NULL == handle_sv)
{
return;
}
if(NULL==::DeleteService(handle_sv))
{
return;
}
HKEY hkey = NULL;
RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SvcHost",&hkey);
if(hkey != NULL)
{
if(ERROR_SUCCESS != ::RegDeleteValue(hkey,"DHCP Server"))
{
}
::RegCloseKey(hkey);
}
}__finally
{
if(NULL != handle_sv)
{
::CloseHandle(handle_sv);
}
if(NULL != handle)
{
::CloseHandle(handle);
}
}
}
my.def
LIBRARY "service"
EXPORTS
ServiceMain @1
Installer @2
Unstaller @3
把文件放到系统目录下,然后rundll32 service.dll Installer之后,服务可以正常加载,然后启动服务,但是却不能弹出对话框是为什么?如果把MessageBox那行去掉,在重新加载启动服务,用DebugView看也只能看到“输出开始”和“输出完毕”这两行,却看不到“服务启动”这行,是为什么?很是郁闷啊!!
请大哥们帮小弟看看,我试过用汇编却可以运行,不知道为什么?
谢谢大家都来讨论讨论!
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课