Pārlūkot izejas kodu

添加:打印日志函数

unknown 6 gadi atpakaļ
vecāks
revīzija
16d3743a1b
2 mainītis faili ar 68 papildinājumiem un 62 dzēšanām
  1. 64 61
      source/hook/hook/dllmain.cpp
  2. 4 1
      source/hook/hook/stdafx.h

+ 64 - 61
source/hook/hook/dllmain.cpp

@@ -1,6 +1,7 @@
 // dllmain.cpp : 定义 DLL 应用程序的入口点。
 #include "stdafx.h"
 
+HMODULE g_hModule = NULL;
 /************************************************************************/
 /*  函数:WriteTextLog[7/28/2016 IT];
 /*  描述:写文本日志;
@@ -14,76 +15,78 @@
 /*  日期:;
 /*  内容:;
 /************************************************************************/
-//void WriteTextLog(const TCHAR *format, ...)
-//{
-//	//static ThreadSection _critSection;
-//	//AutoThreadSection aSection(&_critSection);
-//	// 解析出日志路径;
-//	TCHAR szlogpath[MAX_PATH] = { 0 };
-//	static TCHAR szModulePath[MAX_PATH] = { 0 };
-//	static TCHAR szFna[MAX_PATH] = { 0 };
-//	if (szModulePath[0] == _T('\0'))
-//	{
-//		TCHAR szDrive[MAX_PATH] = { 0 };
-//		TCHAR szDir[MAX_PATH] = { 0 };
-//		TCHAR szExt[MAX_PATH] = { 0 };
-//		::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
-//		_tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
-//		_tcscpy_s(szModulePath, szDrive);
-//		_tcscat_s(szModulePath, szDir);
-//	}
-//
-//	_stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
-//
-//									 // 打开或创建文件;
-//	FILE *fp = NULL;
-//	if (_taccess(szlogpath, 0) != -1)
-//	{// 存在;
-//		fp = _tfopen(szlogpath, _T("a+"));
-//		// 移动到末尾;
-//		fseek(fp, 0, SEEK_END);
-//	}
-//	else
-//	{// 不存在;
-//		fp = _tfopen(szlogpath, _T("w+"));
-//	}
-//
-//	if ( fp == NULL )
-//		return;
-//
-//	// 格式化前设置语言区域;
-//	TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
-//	_tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
-//
-//	// 格式化日志内容;
-//	va_list		args = NULL;
-//	int			len = 0;
-//	TCHAR		*buffer = NULL;
-//	va_start(args, format);
-//	// _vscprintf doesn't count. terminating '\0'
-//	len = _vsctprintf(format, args) + 1;
-//	buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
-//	_vstprintf_s(buffer, len, format, args);
-//	// 将日志内容输入到文件中;
-//	_ftprintf(fp, _T("%s%s \n"), CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")), buffer);
-//
-//	// 关闭文件,释放资源并设置回原语言区域;
-//	free(buffer);
-//	fclose(fp);
-//	_tsetlocale(LC_CTYPE, old_locale);
-//	free(old_locale);//还原区域设定;
-//}
+void WriteTextLog(const TCHAR *format, ...)
+{
+	// 解析出日志路径;
+	TCHAR szlogpath[MAX_PATH] = { 0 };
+	static TCHAR szModulePath[MAX_PATH] = { 0 };
+	static TCHAR szFna[MAX_PATH] = { 0 };
+	if (szModulePath[0] == _T('\0'))
+	{
+		TCHAR szDrive[MAX_PATH] = { 0 };
+		TCHAR szDir[MAX_PATH] = { 0 };
+		TCHAR szExt[MAX_PATH] = { 0 };
+		::GetModuleFileName(g_hModule, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
+		_tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
+		_tcscpy_s(szModulePath, szDrive);
+		_tcscat_s(szModulePath, szDir);
+	}
+
+	_stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
+	// 打开或创建文件;
+	FILE *fp = NULL;
+	if (_taccess(szlogpath, 0) != -1)
+	{// 存在;
+		fp = _tfopen(szlogpath, _T("a+"));
+		// 移动到末尾;
+		fseek(fp, 0, SEEK_END);
+	}
+	else
+	{// 不存在;
+		fp = _tfopen(szlogpath, _T("w+"));
+	}
+
+	if ( fp == NULL )
+		return;
+
+	// 格式化前设置语言区域;
+	TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
+	_tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
+
+	// 格式化日志内容;
+	va_list		args = NULL;
+	int			len = 0;
+	TCHAR		*buffer = NULL;
+	va_start(args, format);
+	// _vscprintf doesn't count. terminating '\0'
+	len = _vsctprintf(format, args) + 1;
+	buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
+	_vstprintf_s(buffer, len, format, args);
+	// 将日志内容输入到文件中;
+	// 获取今年年份;
+	__time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
+	struct tm gmtm = {0};
+	localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
+	_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year+1990, gmtm.tm_mon+1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
+
+	// 关闭文件,释放资源并设置回原语言区域;
+	free(buffer);
+	fclose(fp);
+	_tsetlocale(LC_CTYPE, old_locale);
+	free(old_locale);//还原区域设定;
+}
 
 BOOL APIENTRY DllMain( HMODULE hModule,
                        DWORD  ul_reason_for_call,
                        LPVOID lpReserved
 					 )
 {
+	g_hModule = hModule;
 	switch (ul_reason_for_call)
 	{
 	case DLL_PROCESS_ATTACH:
 		//MessageBox(NULL, _T("dll已成功注入"), _T("注入"), MB_OK);
-		//WriteTextLog(_T("dll已成功注入"));
+		WriteTextLog(_T("dll已成功注入"));
 		break;
 	case DLL_THREAD_ATTACH:
 		break;
@@ -91,7 +94,7 @@ BOOL APIENTRY DllMain( HMODULE hModule,
 		break;
 	case DLL_PROCESS_DETACH:
 		//MessageBox(NULL, _T("dll已成功卸载"), _T("注入"), MB_OK);
-		//WriteTextLog(_T("dll已成功卸载"));
+		WriteTextLog(_T("dll已成功卸载"));
 		break;
 	default:
 		break;

+ 4 - 1
source/hook/hook/stdafx.h

@@ -11,6 +11,9 @@
 // Windows 头文件:
 #include <windows.h>
 #include <tchar.h>
-
+#include <stdio.h>
+#include <locale.h>
+#include <stdlib.h>
+#include <time.h> //或者 #include <ctime>
 
 // TODO: 在此处引用程序需要的其他头文件