KHook

//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

 

 

 

 

 

 

發佈了56 篇原創文章 · 獲贊 5 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章