#include <windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szAppName[] = "Hello world"; HWND hWnd; MSG msg; WNDCLASS wnd; wnd.cbClsExtra = NULL; wnd.cbWndExtra = NULL; wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wnd.hCursor = LoadCursor(NULL, IDC_ARROW); wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); wnd.hInstance = hInstance; wnd.lpfnWndProc = WndProc; wnd.lpszClassName = szAppName; wnd.lpszMenuName = NULL; wnd.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClass(&wnd)) { MessageBox(NULL, "Can not register window class", "Error", MB_OK | MB_ICONINFORMATION); return -1; } hWnd = CreateWindow(szAppName, "Hello world", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; switch (uMsg) { case WM_PAINT: hDC = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &rect); DrawText(hDC, "Hello world", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } |
class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } }; |
class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } }; |
ZWindow zwnd; WNDCLASS wnd; wnd.lpfnWndProc = wnd.WndProc; |
cannot convert from ''long (__stdcall ZWindow::*)(struct HWND__ *, unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWND__ *, unsigned int, unsigned int,long) |
#include <windows.h> class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } }; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szAppName[] = "Hello world"; HWND hWnd; MSG msg; WNDCLASS wnd; ZWindow zwnd; wnd.cbClsExtra = NULL; wnd.cbWndExtra = NULL; wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wnd.hCursor = LoadCursor(NULL, IDC_ARROW); wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); wnd.hInstance = hInstance; wnd.lpfnWndProc = ZWindow::WndProc; wnd.lpszClassName = szAppName; wnd.lpszMenuName = NULL; wnd.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClass(&wnd)) { MessageBox(NULL, "Can not register window class", "Error", MB_OK | MB_ICONINFORMATION); return -1; } hWnd = CreateWindow(szAppName, "Hello world", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); zwnd.Attach(hWnd); zwnd.ShowWindow(nCmdShow); zwnd.UpdateWindow(); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } |
switch (uMsg) { case WM_PAINT: hDC = ::BeginPaint(hWnd, &ps); ::GetClientRect(hWnd, &rect); ::DrawText(hDC, "Hello world", -1, &rect, DT_CENTER | DT_VCENTER DT_SINGLELINE); ::EndPaint(hWnd, &ps); break; case WM_DESTROY: ::PostQuitMessage(0); break; } |
#include <iostream> using namespace std; class C { public: void NonStaticFunc() { cout << "NonStaticFun" << endl; } static void StaticFun(C* pC) { cout << "StaticFun" << endl; pC->NonStaticFunc(); } }; int main() { C objC; C::StaticFun(&objC); return 0; } |
StaticFun NonStaticFun |
#include <windows.h> class ZWindow; ZWindow* g_pWnd = NULL; class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } inline HDC BeginPaint(LPPAINTSTRUCT ps) { return ::BeginPaint(m_hWnd, ps); } inline BOOL EndPaint(LPPAINTSTRUCT ps) { return ::EndPaint(m_hWnd, ps); } inline BOOL GetClientRect(LPRECT rect) { return ::GetClientRect(m_hWnd, rect); } BOOL Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent = 0, DWORD dwStyle = WS_OVERLAPPEDWINDOW, DWORD dwExStyle = 0, HMENU hMenu = 0) { m_hWnd = ::CreateWindowEx(dwExStyle, szClassName, szTitle, dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWndParent, hMenu, hInstance, NULL); return m_hWnd != NULL; } static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ZWindow* pThis = g_pWnd; HDC hDC; PAINTSTRUCT ps; RECT rect; switch (uMsg) { case WM_PAINT: hDC = pThis->BeginPaint(&ps); pThis->GetClientRect(&rect); ::DrawText(hDC, "Hello world", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); pThis->EndPaint(&ps); break; case WM_DESTROY: ::PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } }; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szAppName[] = "Hello world"; MSG msg; WNDCLASS wnd; ZWindow zwnd; wnd.cbClsExtra = NULL; wnd.cbWndExtra = NULL; wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wnd.hCursor = LoadCursor(NULL, IDC_ARROW); wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); wnd.hInstance = hInstance; wnd.lpfnWndProc = zwnd.WndProc; wnd.lpszClassName = szAppName; wnd.lpszMenuName = NULL; wnd.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClass(&wnd)) { MessageBox(NULL, "Can not register window class", "Error", MB_OK | MB_ICONINFORMATION); return -1; } g_pWnd = &zwnd; zwnd.Create(szAppName, "Hell world", hInstance); zwnd.ShowWindow(nCmdShow); zwnd.UpdateWindow(); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } |
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ZWindow* pThis = g_pWnd; switch (uMsg) { case WM_CREATE: pThis->OnCreate(wParam, lParam); break; case WM_PAINT: pThis->OnPaint(wParam, lParam); break; case WM_DESTROY: ::PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } |
#include <windows.h> class ZWindow; ZWindow* g_pWnd = NULL; class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } inline HDC BeginPaint(LPPAINTSTRUCT ps) { return ::BeginPaint(m_hWnd, ps); } inline BOOL EndPaint(LPPAINTSTRUCT ps) { return ::EndPaint(m_hWnd, ps); } inline BOOL GetClientRect(LPRECT rect) { return ::GetClientRect(m_hWnd, rect); } BOOL Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent = 0, DWORD dwStyle = WS_OVERLAPPEDWINDOW, DWORD dwExStyle = 0, HMENU hMenu = 0) { m_hWnd = ::CreateWindowEx(dwExStyle, szClassName, szTitle, dwStyle, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWndParent, hMenu, hInstance, NULL); return m_hWnd != NULL; } virtual LRESULT OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; hDC = BeginPaint(&ps); GetClientRect(&rect); ::DrawText(hDC, "Hello world", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } virtual LRESULT OnCreate(WPARAM wParam, LPARAM lParam) { return 0; } static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ZWindow* pThis = g_pWnd; switch (uMsg) { case WM_CREATE: pThis->OnCreate(wParam, lParam); break; case WM_PAINT: pThis->OnPaint(wParam, lParam); break; case WM_DESTROY: ::PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } }; class ZDriveWindow : public ZWindow { public: LRESULT OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; hDC = BeginPaint(&ps); GetClientRect(&rect); SetBkMode(hDC, TRANSPARENT); DrawText(hDC, "Hello world From Drive", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } }; |
#include <windows.h> class ZWindow; ZWindow* g_pWnd = NULL; class ZWindow { public: HWND m_hWnd; ZWindow(HWND hWnd = 0) : m_hWnd(hWnd) { } inline void Attach(HWND hWnd) { m_hWnd = hWnd; } inline BOOL ShowWindow(int nCmdShow) { return ::ShowWindow(m_hWnd, nCmdShow); } inline BOOL UpdateWindow() { return ::UpdateWindow(m_hWnd); } inline HDC BeginPaint(LPPAINTSTRUCT ps) { return ::BeginPaint(m_hWnd, ps); } inline BOOL EndPaint(LPPAINTSTRUCT ps) { return ::EndPaint(m_hWnd, ps); } inline BOOL GetClientRect(LPRECT rect) { return ::GetClientRect(m_hWnd, rect); } BOOL Create(LPCTSTR szClassName, LPCTSTR szTitle, HINSTANCE hInstance, HWND hWndParent = 0, DWORD dwStyle = WS_OVERLAPPEDWINDOW, DWORD dwExStyle = 0, HMENU hMenu = 0, int x = CW_USEDEFAULT, int y = CW_USEDEFAULT, int nWidth = CW_USEDEFAULT, int nHeight = CW_USEDEFAULT) { m_hWnd = ::CreateWindowEx(dwExStyle, szClassName, szTitle, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, NULL); return m_hWnd != NULL; } virtual LRESULT OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; hDC = BeginPaint(&ps); GetClientRect(&rect); ::DrawText(hDC, "Hello world", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } virtual LRESULT OnLButtonDown(WPARAM wParam, LPARAM lParam) { return 0; } virtual LRESULT OnCreate(WPARAM wParam, LPARAM lParam) { return 0; } virtual LRESULT OnKeyDown(WPARAM wParam, LPARAM lParam) { return 0; } static LRESULT CALLBACK StartWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ZWindow* pThis = g_pWnd; if (uMsg == WM_NCDESTROY) ::PostQuitMessage(0); switch (uMsg) { case WM_CREATE: pThis->OnCreate(wParam, lParam); break; case WM_PAINT: pThis->OnPaint(wParam, lParam); break; case WM_LBUTTONDOWN: pThis->OnLButtonDown(wParam, lParam); break; case WM_KEYDOWN: pThis->OnKeyDown(wParam, lParam); break; case WM_DESTROY: ::PostQuitMessage(0); break; } return ::DefWindowProc(hWnd, uMsg, wParam, lParam); } }; class ZDriveWindow1 : public ZWindow { public: LRESULT OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; hDC = BeginPaint(&ps); GetClientRect(&rect); ::SetBkMode(hDC, TRANSPARENT); ::DrawText(hDC, "ZDriveWindow1", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } LRESULT OnLButtonDown(WPARAM wParam, LPARAM lParam) { ::MessageBox(NULL, "ZDriveWindow1::OnLButtonDown", "Msg", MB_OK); return 0; } }; class ZDriveWindow2 : public ZWindow { public: LRESULT OnPaint(WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; RECT rect; hDC = BeginPaint(&ps); GetClientRect(&rect); ::SetBkMode(hDC, TRANSPARENT); ::Rectangle(hDC, rect.left, rect.top, rect.right, rect.bottom); ::DrawText(hDC, "ZDriveWindow2", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(&ps); return 0; } LRESULT OnLButtonDown(WPARAM wParam, LPARAM lParam) { ::MessageBox(NULL, "ZDriveWindow2::OnLButtonDown", "Msg", MB_OK); return 0; } }; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szAppName[] = "Hello world"; MSG msg; WNDCLASS wnd; ZDriveWindow1 zwnd1; ZDriveWindow2 zwnd2; wnd.cbClsExtra = NULL; wnd.cbWndExtra = NULL; wnd.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); wnd.hCursor = LoadCursor(NULL, IDC_ARROW); wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); wnd.hInstance = hInstance; wnd.lpfnWndProc = ZWindow::StartWndProc; wnd.lpszClassName = szAppName; wnd.lpszMenuName = NULL; wnd.style = CS_HREDRAW | CS_VREDRAW; if (!RegisterClass(&wnd)) { MessageBox(NULL, "Can not register window class", "Error", MB_OK | MB_ICONINFORMATION); return -1; } g_pWnd = &zwnd1; zwnd1.Create(szAppName, "Hell world", hInstance); zwnd1.ShowWindow(nCmdShow); zwnd1.UpdateWindow(); g_pWnd = &zwnd2; zwnd2.Create(szAppName, "Hello world", hInstance, zwnd1.m_hWnd, WS_VISIBLE | WS_CHILD | ES_MULTILINE, NULL, NULL, 0, 0, 150, 150); while (GetMessage(&msg, NULL, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } |
|
#include <iostream> using namespace std; struct S { char ch; int i; }; int main() { cout << "Size of character = " << sizeof(char) << endl; cout << "Size of integer = " << sizeof(int) << endl; cout << "Size of structure = " << sizeof(S) << endl; return 0; } |
Size of character = 1 Size of integer = 4 Size of structure = 8 |
#include <iostream> using namespace std; struct S { char ch1; char ch2; int i; }; int main() { cout << "Size of character = " << sizeof(char) << endl; cout << "Size of integer = " << sizeof(int) << endl; cout << "Size of structure = " << sizeof(S) << endl; return 0; } |
#include <iostream> using namespace std; struct S { char ch1; char ch2; int i; }s; int main() { cout << "Address of ch1 = " << (int)&s.ch1 << endl; cout << "Address of ch2 = " << (int)&s.ch2 << endl; cout << "Address of int = " << (int)&s.i << endl; return 0; } |
Address of ch1 = 4683576 Address of ch2 = 4683577 Address of int = 4683580 |
#include <iostream> using namespace std; struct S { char ch1; char ch2; int i; }; int main() { S s = { ''A'', ''B'', 10}; void* pVoid = (void*)&s; char* pChar = (char*)pVoid; cout << (char)*(pChar + 0) << endl; cout << (char)*(pChar + 1) << endl; cout << (char)*(pChar + 2) << endl; cout << (char)*(pChar + 3) << endl; cout << (int)*(pChar + 4) << endl; return 0; } |
A B … … 10 |
|
#include <iostream> using namespace std; #pragma pack(push, 1) struct S { char ch; int i; }; #pragma pack(pop) int main() { cout << "Size of structure = " << sizeof(S) << endl; return 0; } |
|
#pragma pack(push,1) // 存储机器代码的结构 struct Thunk { BYTE m_jmp; // jmp指令的操作码 DWORD m_relproc; // 相对jmp }; #pragma pack(pop) |
#include <iostream> #include <windows.h> using namespace std; class C; C* g_pC = NULL; typedef void(*pFUN)(); #pragma pack(push,1) // 存储机器代码的结构 struct Thunk { BYTE m_jmp; // jmp指令的操作码 DWORD m_relproc; // 相对jmp }; #pragma pack(pop) class C { public: Thunk m_thunk; void Init(pFUN pFun, void* pThis) { // 跳转指令的操作码 m_thunk.m_jmp = 0xe9; // 相应函数的地址 m_thunk.m_relproc = (int)pFun - ((int)this+sizeof(Thunk)); FlushInstructionCache(GetCurrentProcess(), &m_thunk, sizeof(m_thunk)); } // 这是回调函数 static void CallBackFun() { C* pC = g_pC; // 初始化thunk pC->Init(StaticFun, pC); // 获得thunk代码地址 pFUN pFun = (pFUN)&(pC->m_thunk); // 开始执行thunk代码,调用StaticFun pFun(); cout << "C::CallBackFun" << endl; } static void StaticFun() { cout << "C::StaticFun" << endl; } }; int main() { C objC; g_pC = &objC; C::CallBackFun(); return 0; } |
C::StaticFun C::CallBackFun |
|
#pragma pack(push,1) struct _WndProcThunk { DWORD m_mov; // mov dword ptr [esp+0x4], pThis (esp+0x4 is hWnd) DWORD m_this; BYTE m_jmp; // jmp WndProc DWORD m_relproc; // 相对jmp }; #pragma pack(pop) |
void Init(WNDPROC proc, void* pThis) { thunk.m_mov = 0x042444C7; //C7 44 24 04 thunk.m_this = (DWORD)pThis; thunk.m_jmp = 0xe9; thunk.m_relproc = (int)proc - ((int)this+sizeof(_WndProcThunk)); FlushInstructionCache(GetCurrentProcess(), &thunk, sizeof(thunk)); } |
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { ZWindow* pThis = (ZWindow*)hWnd; if (uMsg == WM_NCDESTROY) PostQuitMessage(0); if (!pThis->ProcessWindowMessage(pThis->m_hWnd, uMsg, wParam, lParam)) return ::DefWindowProc(pThis->m_hWnd, uMsg, wParam, lParam); else return 0; } |
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=292976