www.pudn.com > Hook-api-mir.rar > conime.cpp


// Copyright: www.pudn.com,程序员联合开发网,www.programsalon.com 
// 如果要使用或修改本程序,请保留次信息 
// 
#include "stdafx.h" 
#include "resource.h" 
#include  
#include  
#include "HookKB/sharemem.h" 
 
#define MAX_LOADSTRING 100 
 
HANDLE g_hMapFile =NULL; 
INPUT_INFO *g_pMapData =NULL; 
 
HANDLE g_hMapFileDebug =NULL; 
DEBUG_INFO *g_pMapDataDebug =NULL; 
 
// Global Variables: 
HINSTANCE hInst;								// current instance 
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text 
 
static INPUT_DATA g_buf_recved[1024]; 
static int g_buf_pos =0; 
 
static char g_debug_recved[1024][200]; 
static int g_debug_pos =0; 
 
// Foward declarations of functions included in this code module: 
ATOM				MyRegisterClass(HINSTANCE hInstance); 
BOOL				InitInstance(HINSTANCE, int); 
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM); 
 
#define MsgBox(msg) MessageBox(NULL, msg, "HookAPI - www.programsalon.com", MB_OK) 
 
void WriteLog(char *fmt,...) 
{ 
	va_list args; 
	char modname[200]; 
 
	char temp[5000]; 
	HANDLE hFile; 
 
	GetModuleFileName(NULL, modname, sizeof(modname)); 
 
	if((hFile =CreateFile("c:\\hookapi.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0) 
	{ 
		return; 
	} 
	 
	SetFilePointer(hFile, 0, NULL, FILE_END); 
 
	wsprintf(temp, "mydll.dll:%s:", modname); 
	DWORD dw; 
	WriteFile(hFile, temp, strlen(temp), &dw, NULL); 
	 
	va_start(args,fmt); 
	vsprintf(temp, fmt, args); 
	va_end(args); 
 
	WriteFile(hFile, temp, strlen(temp), &dw, NULL); 
 
	wsprintf(temp, "\r\n"); 
	WriteFile(hFile, temp, strlen(temp), &dw, NULL); 
 
	CloseHandle(hFile); 
} 
 
BOOL CALLBACK MainDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam); 
 
typedef int (WINAPI *FuncHookOneProcess2)(HWND hwndNotify, char *exe_name); 
typedef int (WINAPI *FuncUnhookOneProcess2)(char *exe_name); 
typedef int (WINAPI *FuncHookAllProcess)(); 
typedef int (WINAPI *FuncUnhookAllProcess)(); 
 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPrevInstance, 
                     LPSTR     lpCmdLine, 
                     int       nCmdShow) 
{ 
	WriteProfileString("HookAPI", "exe_name", "conime.exe"); 
 
	int isNT =false; 
	OSVERSIONINFO VersionInfo; 
	VersionInfo.dwOSVersionInfoSize =sizeof(OSVERSIONINFO); 
 
	if(!GetVersionEx(&VersionInfo)) 
		return -10; 
 
	if(VersionInfo.dwPlatformId ==VER_PLATFORM_WIN32_NT) 
		isNT =true; 
	else 
		return -11;	// not support win9x 
 
	g_hMapFile = CreateFileMapping( 
		INVALID_HANDLE_VALUE,               // Current file handle.   
	    NULL,                              // Default security.   
	    PAGE_READWRITE,                    // Read/write permission.   
	    0,                                 // Max. object size.   
	    sizeof(INPUT_INFO),                         // Size of hFile.   
		"hook_map_file"); 
	if (g_hMapFile == NULL)   
	{   
	    return -1;  
	}   
  
	g_pMapData = (INPUT_INFO *)MapViewOfFile(g_hMapFile, // Handle to mapping object.   
	    FILE_MAP_ALL_ACCESS,               // Read/write permission   
	    0,                                 // Max. object size.   
	    0,                                 // Size of hFile.   
	    sizeof(INPUT_INFO));          // Map entire file.   
   
	if (g_pMapData == NULL)   
	{   
		CloseHandle(g_hMapFile); 
	    return -2;  
	} 
	memset(g_pMapData, 0, sizeof(INPUT_INFO)); 
 
	g_hMapFileDebug = CreateFileMapping( 
		INVALID_HANDLE_VALUE,               // Current file handle.   
	    NULL,                              // Default security.   
	    PAGE_READWRITE,                    // Read/write permission.   
	    0,                                 // Max. object size.   
	    sizeof(DEBUG_INFO),                         // Size of hFile.   
		"debug_map_file"); 
	if (g_hMapFile == NULL)   
	{   
		UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    return -1;  
	}   
  
	g_pMapDataDebug = (DEBUG_INFO *)MapViewOfFile(g_hMapFileDebug, // Handle to mapping object.   
	    FILE_MAP_ALL_ACCESS,               // Read/write permission   
	    0,                                 // Max. object size.   
	    0,                                 // Size of hFile.   
	    sizeof(DEBUG_INFO));          // Map entire file.   
   
	if (g_pMapDataDebug == NULL)   
	{   
		UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
		CloseHandle(g_hMapFileDebug); 
	    return -2;  
	} 
	memset(g_pMapDataDebug, 0, sizeof(DEBUG_INFO)); 
 
	HINSTANCE hLib; 
	if(isNT) 
		hLib =LoadLibrary("HookAPINT.dll"); 
	else 
		hLib =LoadLibrary("HookAPI9x.dll"); 
	if(hLib ==NULL) 
	{ 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		return false; 
	} 
 
	FuncHookOneProcess2 HookOneProcess2 =(FuncHookOneProcess2)GetProcAddress(hLib, "HookOneProcess2"); 
	FuncUnhookOneProcess2 UnhookOneProcess2 =(FuncUnhookOneProcess2)GetProcAddress(hLib, "UnhookOneProcess2"); 
	FuncHookAllProcess HookAllProcess =(FuncHookAllProcess)GetProcAddress(hLib, "HookAllProcess"); 
	FuncUnhookAllProcess UnhookAllProcess =(FuncUnhookAllProcess)GetProcAddress(hLib, "UnhookAllProcess"); 
 
	if(HookAllProcess ==NULL) 
	{ 
		FreeLibrary(hLib); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		return 0; 
	} 
	 
	if(UnhookAllProcess ==NULL) 
	{ 
		MsgBox("UnhookAllProcess ==NULL"); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return 0; 
	} 
 
	// TODO: Place code here. 
	MSG msg; 
	HACCEL hAccelTable; 
 
	// Initialize global strings 
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
	LoadString(hInstance, IDC_CONIME, szWindowClass, MAX_LOADSTRING); 
	MyRegisterClass(hInstance); 
 
	// Perform application initialization: 
	if (!InitInstance (hInstance, nCmdShow))  
	{ 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return FALSE; 
	} 
 
	/*if(HookAllProcess() <0) 
	{ 
		MsgBox("HookAllProcesses error!"); 
		UnhookAllProcess(); 
		FreeLibrary(hLib); 
		return 0; 
	}*/ 
 
	// 测试只Hook几个正在运行的程序,下面的函数已经在1.6版本中取消,但还是很有用,所以以后版本中还是会提供 
 
/*	if(HookOneProcess2(NULL, "notepad.exe") <0) 
	{ 
		MsgBox("HookOneProcess failed!"); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return  0; 
	} 
*/ 
	if(HookOneProcess2(NULL, "mir.exe") <0) 
	{ 
		MsgBox("HookOneProcess failed!"); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return  0; 
	} 
 
/*	if(HookOneProcess2(NULL, "mir.dat") <0) 
	{ 
		MsgBox("HookOneProcess failed!"); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return  0; 
	} 
	 
	if(HookOneProcess2(NULL, "mir2.dat") <0) 
	{ 
		MsgBox("HookOneProcess failed!"); 
	    UnmapViewOfFile(g_pMapData); 
		CloseHandle(g_hMapFile); 
	    UnmapViewOfFile(g_pMapDataDebug); 
		CloseHandle(g_hMapFileDebug); 
		FreeLibrary(hLib); 
		return  0; 
	} 
*/ 
	/// HookAllProcesses2是针对无法截获按Ctrl-Alt_Del键时运行的任务管理器等程序的情况使用的。 
	/*if(HookAllProcesses2(1000) <0) 
	{ 
		MsgBox("HookAllProcesses error!"); 
 
		return 0; 
	}*/ 
 
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CONIME); 
 
	// Main message loop: 
	while (GetMessage(&msg, NULL, 0, 0))  
	{ 
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))  
		{ 
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		} 
	} 
 
	UnhookAllProcess(); 
	FreeLibrary(hLib); 
 
    UnmapViewOfFile(g_pMapData); 
	CloseHandle(g_hMapFile); 
    UnmapViewOfFile(g_pMapDataDebug); 
	CloseHandle(g_hMapFileDebug); 
 
	return msg.wParam; 
} 
 
// 
//  FUNCTION: MyRegisterClass() 
// 
//  PURPOSE: Registers the window class. 
// 
//  COMMENTS: 
// 
//    This function and its usage is only necessary if you want this code 
//    to be compatible with Win32 systems prior to the 'RegisterClassEx' 
//    function that was added to Windows 95. It is important to call this function 
//    so that the application will get 'well formed' small icons associated 
//    with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
	WNDCLASSEX wcex; 
 
	wcex.cbSize = sizeof(WNDCLASSEX);  
 
	wcex.style			= CS_HREDRAW | CS_VREDRAW; 
	wcex.lpfnWndProc	= (WNDPROC)WndProc; 
	wcex.cbClsExtra		= 0; 
	wcex.cbWndExtra		= 0; 
	wcex.hInstance		= hInstance; 
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_CONIME); 
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW); 
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1); 
	wcex.lpszMenuName	= (LPCSTR)IDC_CONIME; 
	wcex.lpszClassName	= szWindowClass; 
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); 
 
	return RegisterClassEx(&wcex); 
} 
 
// 
//   FUNCTION: InitInstance(HANDLE, int) 
// 
//   PURPOSE: Saves instance handle and creates main window 
// 
//   COMMENTS: 
// 
//        In this function, we save the instance handle in a global variable and 
//        create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
   HWND hWnd; 
 
   hInst = hInstance; // Store instance handle in our global variable 
 
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL, 
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 
 
   if (!hWnd) 
   { 
      return FALSE; 
   } 
 
   ShowWindow(hWnd, nCmdShow); 
   UpdateWindow(hWnd); 
 
   return TRUE; 
} 
 
// 
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG) 
// 
//  PURPOSE:  Processes messages for the main window. 
// 
//  WM_COMMAND	- process the application menu 
//  WM_PAINT	- Paint the main window 
//  WM_DESTROY	- post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
	switch (message)  
	{ 
	case WM_CREATE: 
		SetTimer(hWnd, 1, 2000, NULL); 
		break; 
	case WM_APP+200: 
		InvalidateRect(hWnd, NULL, TRUE); 
		break; 
	case WM_COMMAND: 
		switch (LOWORD(wParam)) 
		{ 
			case IDM_EXIT: 
			   DestroyWindow(hWnd); 
			   break; 
			default: 
			   return DefWindowProc(hWnd, message, wParam, lParam); 
		} 
		break; 
	case WM_PAINT: 
		PAINTSTRUCT ps; 
		HDC hdc; 
		char temp[400]; 
		INPUT_DATA *pdata; 
		hdc = BeginPaint(hWnd, &ps); 
		pdata =&g_pMapData->data[g_pMapData->cur_times]; 
		sprintf(temp, "stat:%d, areaType:%d, areadName:%s, serverName:%s", 
				pdata->stat, pdata->area_type, pdata->areaName, pdata->serverName); 
		TextOut(hdc, 2, 2, temp, strlen(temp)); 
		sprintf(temp, " id:%s, pass:%s, mb_t1:%u, mb_t2:%u", 
				pdata->login_id, pdata->password, pdata->mb_t1, pdata->mb_t2); 
		TextOut(hdc, 2, 22, temp, strlen(temp)); 
		sprintf(temp, " mb_pos:%d, mb:%d", 
				pdata->mb_pos[0], pdata->mb_pos[1], pdata->mb_pos[2], 
				pdata->mb[0], pdata->mb[1], pdata->mb[2]); 
		TextOut(hdc, 2, 42, temp, strlen(temp)); 
		EndPaint(hWnd, &ps); 
		break; 
	case WM_DESTROY: 
		PostQuitMessage(0); 
		break; 
	default: 
		return DefWindowProc(hWnd, message, wParam, lParam); 
   } 
   return 0; 
}