// TestTrunk.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "TestTrunk.h"
#define MAX_LOADSTRING 100
#pragma pack(push,1)
typedef struct _StdCallThunk
{
DWORD m_mov; // = 0x042444C7
DWORD m_this; // = this
BYTE m_jmp; // = 0xe9
DWORD m_relproc; // = relative distance
} StdCallThunk;
#pragma pack(pop)
class CMyWindow
{
public:
CMyWindow():_hwnd(NULL){}
~CMyWindow(){VirtualFree(_pStdthunk, sizeof(StdCallThunk), MEM_RELEASE);}
bool Create();
protected:
LRESULT CALLBACK WndProc(UINT message, WPARAM wParam, LPARAM lParam);
MSG _msg;
HWND _hwnd;
StdCallThunk *_pStdthunk;
static LRESULT CALLBACK TempWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
};
bool CMyWindow::Create()
{
WNDCLASSEX wcex;
LPCTSTR lpszClassName = _T("ClassName");
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = CMyWindow::TempWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = GetModuleHandle(NULL);
wcex.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TESTTRUNK));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTTRUNK);
wcex.lpszClassName = lpszClassName;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&wcex);
_pStdthunk = (StdCallThunk *)VirtualAlloc(NULL, sizeof(StdCallThunk), MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
_pStdthunk->m_mov = 0x042444c7;
_pStdthunk->m_jmp = 0xe9;
//CreateWindow的最后一个参数为this指针
CreateWindow(lpszClassName, NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL),this);
if (_hwnd == NULL)
{
MessageBox(NULL,TEXT("Error"),NULL,NULL);
return FALSE;
}
ShowWindow(_hwnd, SW_SHOW);
UpdateWindow(_hwnd);
return TRUE;
}
LRESULT CALLBACK CMyWindow::TempWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_CREATE)//此处使用WM_NCCREATE消息也可以
{
//提取出对象指针
CMyWindow *w = NULL;
w = (CMyWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
w->_hwnd = hWnd;
WNDPROC pWndProc = (WNDPROC)w->_pStdthunk;
w->_pStdthunk->m_this = (DWORD)w;
//计算跳转位置
w->_pStdthunk->m_relproc = (DWORD)&CMyWindow::StaticWndProc - ((DWORD)w->_pStdthunk + sizeof(StdCallThunk));
SetWindowLong(hWnd, GWL_WNDPROC, (LONG)pWndProc);
return pWndProc( hWnd, message, wParam, lParam);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
LRESULT CALLBACK CMyWindow::StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return ((CMyWindow *)hWnd)->WndProc(message, wParam, lParam);
}
LRESULT CALLBACK CMyWindow::WndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
MessageBox(NULL,TEXT("AboutDlg"),TEXT("关于"),NULL);
break;
case IDM_EXIT:
DestroyWindow(_hwnd);
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(_hwnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(_hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(_hwnd, message, wParam, lParam);
break;
}
return TRUE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
CMyWindow pMyWindow;
pMyWindow.Create();
MSG msg;
HACCEL hAccelTable = LoadAccelerators(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_TESTTRUNK));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课