C++易不可能为可能 – .NET Windows Form 改变窗体类名(Class Name)有多麻烦?续篇

  发布《 .NET Windows Form 改变窗体类名(Class
Name)有多麻烦?》转眼大半年过去了,要无是当前边几乎天发园友对当时篇稿子展开评价,基本上已经挺少关心其了,毕竟那无非是一个回的钻,在支付中没什么实际的用途。但是出于Squares园友的评说,结合近年祥和息息相关的干活,灵感一现,却真的找到了解决的法,不得不感慨一下,“问题总是会产生解决办法的,只是自己力量不敷或一时从不悟出而已”。好了,前奏写了,进入正题。

前不久系工作

  最近一段时间,重新拾打以前比较熟悉的界面UI开发,由于要,了解了一部分
HOOK API 的知。HOOK API C++ 已经发出比好之开源资源,MHook和
MinHook。而
HOOK API 就是缓解 “Windows Form 改变窗体类名(Class Name)”的重要。

灵感和思路

  还记得上同篇稿子里关系为什么不克转Windows
Form窗体类名的故吧?就是微软的代码里才认系统登记的
ClassName,只要我们于 CreateParams 属性里设置的 ClassName 不是网登记的
ClassName,就见面报错。所以,设置好好的 ClassName,只能依照窗口创建的长河,自己实现一个窗口。而实现一个窗口的过程为杀简短:

  1. 动 API 函数 RegisterClass 注册窗口;
  2. 下 API 函数 CreateWindowEx 创建窗口;
  3. 采取 API 函数 ShowWindow 显示窗口;
  4. 说到底退出时利用 API 函数 DestroyWindow 销毁窗口。

  过程非常简单,Winform 的窗口也退不了是历程。那这样, HOOK API
不就是有机可乘了也?只要我们 HOOK RegisterClass
和 CreateWindowEx,在
Winform 注册窗口时,把它们采取的类名改也我们需要的类名;创建窗口的时光,也同样。当然,在骨子里处理过程中,UnregisterClassGetClassInfo
也是C++用 HOOK 进行处理的。

最终实现

  不多说,非常简单,一切为代码说话。  

  1 #include "ClassNameManager.h"
  2 #include <Windows.h>
  3 #include <tchar.h>
  4 #include <assert.h>
  5 #include "../MinHook/include/MinHook.h"
  6 
  7 #ifdef _M_X64
  8 #pragma comment(lib, "../lib/MinHook/MinHook.x64.lib")
  9 #else
 10 #pragma comment(lib,"../lib/MinHook/MinHook.x86.lib")
 11 #endif
 12 
 13 namespace Starts2000 {
 14     namespace Window {
 15         namespace Forms {
 16 
 17             #define FORM_CLASS_NAME L"WindowsForms10.Window.8.app"
 18             #define FORM_CUSTOM_CLASS_NAME L"Starts2000.Window"
 19 
 20             typedef ATOM (WINAPI * TrueRegisterClassW)(_In_ CONST WNDCLASSW *);
 21             typedef BOOL (WINAPI * TrueUnregisterClassW)(_In_ LPCWSTR, _In_opt_ HINSTANCE);
 22             typedef BOOL (WINAPI * TrueGetClassInfoW)(
 23                 _In_opt_ HINSTANCE,
 24                 _In_ LPCWSTR,
 25                 _Out_ LPWNDCLASSW);
 26             typedef HWND (WINAPI * TrueCreateWindowExW)(
 27                 _In_ DWORD,
 28                 _In_opt_ LPCWSTR,
 29                 _In_opt_ LPCWSTR,
 30                 _In_ DWORD,
 31                 _In_ int,
 32                 _In_ int,
 33                 _In_ int,
 34                 _In_ int,
 35                 _In_opt_ HWND,
 36                 _In_opt_ HMENU,
 37                 _In_opt_ HINSTANCE,
 38                 _In_opt_ LPVOID);
 39 
 40             TrueRegisterClassW _registerClassW = NULL;
 41             TrueUnregisterClassW _unregisterClassW = NULL;
 42             TrueGetClassInfoW _getClassInfoW = NULL;
 43             TrueCreateWindowExW _createWindowExW = NULL;
 44 
 45             ATOM WINAPI RegisterClassWD(_In_ CONST WNDCLASSW *lpWndClass) {
 46                 if (_tcsstr(lpWndClass->lpszClassName, FORM_CLASS_NAME)) {
 47                     WNDCLASSW wndClass;
 48                     memcpy(&wndClass, lpWndClass, sizeof(WNDCLASSW));
 49                     wndClass.lpszClassName = FORM_CUSTOM_CLASS_NAME;
 50                     auto ret = _registerClassW(&wndClass);
 51                     return ret;
 52                 }
 53 
 54                 return _registerClassW(lpWndClass);
 55             }
 56 
 57             BOOL WINAPI UnregisterClassWD(_In_ LPCWSTR lpClassName, _In_opt_ HINSTANCE hInstance) {
 58                 if (_tcsstr(lpClassName, FORM_CLASS_NAME)) {
 59                     return _unregisterClassW(FORM_CUSTOM_CLASS_NAME, hInstance);
 60                 }
 61 
 62                 return _unregisterClassW(lpClassName, hInstance);
 63             }
 64 
 65             BOOL WINAPI GetClassInfoWD(_In_opt_ HINSTANCE hInstance,
 66                 _In_ LPCWSTR lpClassName,
 67                 _Out_ LPWNDCLASSW lpWndClass) {
 68                 if (_tcsstr(lpClassName, FORM_CLASS_NAME)) {
 69                     return _getClassInfoW(hInstance, FORM_CUSTOM_CLASS_NAME, lpWndClass);
 70                 }
 71 
 72                 return _getClassInfoW(hInstance, lpClassName, lpWndClass);
 73             }
 74 
 75             HWND WINAPI CreateWindowExWD(
 76                 _In_ DWORD dwExStyle,
 77                 _In_opt_ LPCWSTR lpClassName,
 78                 _In_opt_ LPCWSTR lpWindowName,
 79                 _In_ DWORD dwStyle,
 80                 _In_ int X,
 81                 _In_ int Y,
 82                 _In_ int nWidth,
 83                 _In_ int nHeight,
 84                 _In_opt_ HWND hWndParent,
 85                 _In_opt_ HMENU hMenu,
 86                 _In_opt_ HINSTANCE hInstance,
 87                 _In_opt_ LPVOID lpParam) {
 88                 if (_tcsstr(lpClassName, FORM_CLASS_NAME)) {
 89                     auto hwnd = _createWindowExW(dwExStyle, FORM_CUSTOM_CLASS_NAME, lpWindowName, dwStyle,
 90                         X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
 91                     assert(hwnd);
 92                     return hwnd;
 93                 }
 94 
 95                 return _createWindowExW(dwExStyle, lpClassName, lpWindowName, dwStyle,
 96                     X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
 97             }
 98 
 99             ClassNameManager::ClassNameManager() {
100                 auto ret = MH_Initialize();
101                 assert(ret == MH_STATUS::MH_OK);
102 
103                 ret = MH_CreateHookApi(L"User32.dll", 
104                     "RegisterClassW", &RegisterClassWD, reinterpret_cast<LPVOID*>(&_registerClassW));
105                 assert(ret == MH_STATUS::MH_OK);
106 
107                 ret = MH_CreateHookApi(L"User32.dll",
108                     "UnregisterClassW", &UnregisterClassWD, reinterpret_cast<LPVOID*>(&_unregisterClassW));
109                 assert(ret == MH_STATUS::MH_OK);
110 
111                 ret = MH_CreateHookApi(L"User32.dll",
112                     "GetClassInfoW", &GetClassInfoWD, reinterpret_cast<LPVOID*>(&_getClassInfoW));
113                 assert(ret == MH_STATUS::MH_OK);
114 
115                 ret = MH_CreateHookApi(L"User32.dll",
116                     "CreateWindowExW", &CreateWindowExWD, reinterpret_cast<LPVOID*>(&_createWindowExW));
117                 assert(ret == MH_STATUS::MH_OK);
118 
119                 ret = MH_EnableHook(MH_ALL_HOOKS);
120                 assert(ret == MH_STATUS::MH_OK);
121             }
122 
123             ClassNameManager::~ClassNameManager() {
124             }
125 
126             ClassNameManager::!ClassNameManager() {
127                 auto ret = MH_Uninitialize();
128                 assert(ret == MH_STATUS::MH_OK);
129             }
130         }
131     }
132 }

末段效果

C++ 1

末段之末尾

  源码是一旦达标之,下载类源代码。