⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hookdeviceiocontrol.cpp

📁 一个可以拦截DeviceIoControl的程序
💻 CPP
字号:
// HookDeviceIoControl.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "SystemTraySDK.h"
#include "commdlg.h"
#include "..\testdll\testdll.h"

// This segment must be defined as SHARED in the .DEF
#pragma data_seg (".HookSection")		
// Shared instance for all processes.
TCHAR SIGN[] = _T("作者:吉林大学 王长春 Lingtu.Inc boyachang@sina.com");
#pragma data_seg ()

enum OperationState{
	NoneOperation,
	SimulateOperation,
	SpyDataOperation
};

#define MAX_LOADSTRING 100
#define	WM_ICON_NOTIFY			WM_APP+10
#define NIIF_WARNING 0

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text
CSystemTray m_TrayIcon;
OperationState m_operationState = NoneOperation;

//
const UINT WM_TASKBARCREATED = 
    ::RegisterWindowMessage(_T("TaskbarCreated"));

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void GetConfigFile(TCHAR* buff, int nBuffLen);
void GetBasePath(TCHAR* buff, int nBuffLen);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_HOOKDEVICEIOCONTROL, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HOOKDEVICEIOCONTROL);

	TCHAR szConfigFile[MAX_PATH];
	GetConfigFile( szConfigFile, MAX_PATH );

//	TCHAR szBasePath[MAX_PATH];
//	GetBasePath( szBasePath, MAX_PATH );
//	WritePrivateProfileString( _T("Config"),
//		_T("BasePath"), szBasePath, szConfigFile );

	InstallHook( szConfigFile );
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	RemoveHook();
	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_HOOKDEVICEIOCONTROL);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)NULL;
	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,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

//   ShowWindow(hWnd, nCmdShow);
//   UpdateWindow(hWnd);

   return TRUE;
}

BOOL PopupMenu(HINSTANCE hInst, HWND hWnd, int nID)
{
	HMENU hMenu = ::LoadMenu( hInst, MAKEINTRESOURCE(nID) );
	if (!hMenu)
		return FALSE;
	
	HMENU hSubMenu = ::GetSubMenu(hMenu, 0);
	if (!hSubMenu)
	{
		::DestroyMenu(hMenu);        //Be sure to Destroy Menu Before Returning
		return FALSE;
	}
	
	if ( m_operationState == SimulateOperation )
	{
		CheckMenuItem( hSubMenu, IDM_SIMULATE, MF_BYCOMMAND | MF_CHECKED );
		SetMenuDefaultItem( hSubMenu, IDM_SIMULATE, FALSE );
	}
	else if ( m_operationState == SpyDataOperation )
	{
		CheckMenuItem( hSubMenu, IDM_SPYDATA, MF_BYCOMMAND | MF_CHECKED );		
		SetMenuDefaultItem( hSubMenu, IDM_SPYDATA, FALSE );
	}

	// Display and track the popup menu
	POINT pos;
#ifdef _WIN32_WCE
	DWORD messagepos = ::GetMessagePos();
	pos.x = GET_X_LPARAM(messagepos);
	pos.y = GET_Y_LPARAM(messagepos);
#else
	GetCursorPos(&pos);
#endif
	
	::SetForegroundWindow( hWnd );  
	::TrackPopupMenu(hSubMenu, 0, pos.x, pos.y, 0, hWnd, NULL);
	
	// BUGFIX: See "PRB: Menus for Notification Icons Don't Work Correctly"
	::PostMessage(hWnd, WM_NULL, 0, 0);
	
	DestroyMenu(hMenu);
	return TRUE;
}

BOOL GetFileName(TCHAR *buffer,int buflen)
{
	int i = 0;
	OPENFILENAME ofn;
	
	memset(&ofn,0,sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hInstance = NULL;
	ofn.hwndOwner = NULL;
	ofn.lpstrFile = buffer;
	ofn.nMaxFile = buflen;
	ofn.lpstrTitle = _T("新建/打开数据文件");
	ofn.nFilterIndex = 2;
	ofn.lpstrDefExt = _T("dat");
	_tcscpy(buffer,"*.dat");

	ofn.Flags = OFN_HIDEREADONLY;
	ofn.lpstrFilter = _T("All files\0*.*\0Data Files\0*.dat\0");
	return GetOpenFileName(&ofn);
	
}
void GetConfigFile(TCHAR* buff, int nBuffLen)
{
	GetModuleFileName( NULL, buff, nBuffLen );
	LPTSTR lpsz = _tcsrchr( buff, _T('\\') );
	_tcscpy( lpsz, _T("\\config.ini") );
}
void GetBasePath(TCHAR* buff, int nBuffLen)
{
	GetModuleFileName( NULL, buff, nBuffLen );
	LPTSTR lpsz = _tcsrchr( buff, _T('\\') );
	lpsz[1] = _T('\0');
}
//
//  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)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	if ( message == WM_TASKBARCREATED )
	{
			HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_HOOKDEVICEIOCONTROL) );

			if (!m_TrayIcon.Create(
				hInst,                            // Let icon deal with its own messages
				hWnd,
				WM_ICON_NOTIFY,                  // Icon notify message to use
				_T("This is a Tray Icon - Right click on me!"),  // tooltip
				hIcon,
				IDR_LEFT_POPUP_MENU,                  // ID of tray icon
				FALSE,
				_T("Here's a cool new Win2K balloon!"), // balloon tip
				_T("Look at me!"),               // balloon title
				NIIF_WARNING,                    // balloon icon
				20 ))                            // balloon timeout
			{
				return -1;
			}
			
			m_TrayIcon.SetTargetWnd( hWnd );		// Send all menu messages here.
	}
	switch (message) 
	{
	case WM_CREATE:
		{
			HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_HOOKDEVICEIOCONTROL) );

			if (!m_TrayIcon.Create(
				hInst,                            // Let icon deal with its own messages
				hWnd,
				WM_ICON_NOTIFY,                  // Icon notify message to use
				_T("This is a Tray Icon - Right click on me!"),  // tooltip
				hIcon,
				IDR_LEFT_POPUP_MENU,                  // ID of tray icon
				FALSE,
				_T("Here's a cool new Win2K balloon!"), // balloon tip
				_T("Look at me!"),               // balloon title
				NIIF_WARNING,                    // balloon icon
				20 ))                            // balloon timeout
			{
				return -1;
			}
			
			m_TrayIcon.SetTargetWnd( hWnd );		// Send all menu messages here.

			TCHAR szConfigFile[MAX_PATH];
			GetConfigFile( szConfigFile, MAX_PATH );
			
			TCHAR buff[256];
			GetPrivateProfileString( _T("Config"),
				_T("Action"), _T(""), buff, 256, szConfigFile );

			if ( _tcscmp( buff, _T("simulate") ) == 0 )
			{
				m_operationState = SimulateOperation;
			}
			else if ( _tcscmp( buff, _T("spydata") ) == 0 )
			{
				m_operationState = SpyDataOperation;
			}
			
		}
		break;
	case WM_ICON_NOTIFY:
		if ( LOWORD(lParam) == WM_LBUTTONUP )
		{
			PopupMenu( hInst, hWnd, IDR_RIGHT_POPUP_MENU );
		}
		else if ( LOWORD(lParam) == WM_RBUTTONUP )
		{
			PopupMenu( hInst, hWnd, IDR_RIGHT_POPUP_MENU );
		}
		break;
		
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
			case IDM_NEW:
				{
					TCHAR buff[MAX_PATH];
					if ( GetFileName( buff, MAX_PATH ) )
					{
						TCHAR szConfigFile[MAX_PATH];
						GetConfigFile( szConfigFile, MAX_PATH );

						TCHAR szBasePath[MAX_PATH];
						GetBasePath( szBasePath, MAX_PATH );
						
						if ( _tcsstr( buff, szBasePath ) == buff )
						{
							_tcscpy( buff, buff + _tcslen( szBasePath ) );
						}
						
						WritePrivateProfileString( _T("Config"),
							_T("File"), buff, szConfigFile );
					}
				}
				break;
			case IDM_SIMULATE:
				m_operationState = SimulateOperation;
				{
					TCHAR szConfigFile[MAX_PATH];
					GetConfigFile( szConfigFile, MAX_PATH );
					
					WritePrivateProfileString( _T("Config"),
						_T("Action"), _T("simulate"), szConfigFile );
				}
				break;
			case IDM_SPYDATA:
				m_operationState = SpyDataOperation;
				{
					TCHAR szConfigFile[MAX_PATH];
					GetConfigFile( szConfigFile, MAX_PATH );
					
					WritePrivateProfileString( _T("Config"),
						_T("Action"), _T("spydata"), szConfigFile );
				}
				break;
				case IDM_ABOUT:
				   MessageBox( NULL, _T("王长春\n lingu.Inc\n2005.09.10"), _T("关于"), MB_OK | MB_ICONINFORMATION );
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -