Bounce down on DirectX
Jinshan Company's game modifier - "Jinshan Ranger", with its unique "DirectX smart pop-up" function in numerous game modifiers unique in many game modifiers, won the love of many players.
"DirectX smart pop-up" is very useful in grasping, game modifiers, game commissioning. So what is its principle? Can we implement it in your own software? After some efforts, I finally unveiled the mystery of "DirectX smart pop-up".
Preliminary knowledge
Before the start, it is necessary to introduce a little basic knowledge about Hook and Windows.
As we all know, Windows is a message-driven 32-bit operating system. In Windows, all processes have a separate 2GB virtual address space, and the processes are not visible. Most of the API and messages of Windows cannot span processes.
"Hook" is mainly used in Windows, and the image is used to "hook" messages. The Hook is actually a block that handles messages, whenever a specific message is issued, the hook function captures the message before you do not reach the destination window, that is, the hook function first gets the control of the message. And if you implement the Hook in a DLL file, the Hook function will automatically be mapped to the process virtual address space where the system is located. For example, you can use hook to capture all keyboard input messages in your system to record your computer user's input (for detailed usage of Windows Process Management and HOOK, see MSDN and related information).
2. Principle
Microsoft's DirectX has brought gorgeous sound and light effects under Windows. However, because DirectX uses direct access to hardware, improve the speed of multimedia and gaming programs, thus causing people to make a normal Windows dialog box in DirectX (exactly DirectDraw).
But DirectX is supporting GDI, that is, the game can display the dialog box under DirectX with a conventional method (in Microsoft DirectX 8 SDK examples). Now our question is: As mentioned above, calling the API to establish and display the dialog box must be inside the process, and our program is outside the game process, so that cretedialog () and Dialogbox () will only cause " Illegal operations "or no effect.
However, there is different HOOK situation above. Since the Hook can map to other processes inside, as long as the function and dialog resources of the display dialog are included in the Hook DLL, do you want to call DialogBox ()? Completely correct! We use setWindowsHooKex () to set a keyboard message hook for your system, and this DLL will automatically map this DLL into the process of the game. Whenever there is a keyboard message, we only need to judge that the hotkey we set, if you call the Dialogbox () display dialog box.
It can be seen that hook solves the system-level hot bonds and inserting DLLs, it is really two! It also illustrates the importance of learning Windows basic knowledge. If you have a research spirit, there is a good book guidelines like "Windows Core Programming", it will become a top master.
3. Example source program
Limited to the space, only the critical code can only be extracted, and there are comments in the program.
// TestPopup.cpp: Defines The entry point for the dll application.
//
#include "stdafx.h" #include "testpopup.h"
#define shard_seg_name "shard_data"
// Share data segment
#pragma data_seg (Shard_SEG_NAME)
#pragma bss_seg (Shard_SEG_NAME)
Static bool g_fisinstalled = false;
Static HHOOK G_HKEYHOK;
Static Hinstance G_HINSTANCE;
Static hwnd g_hwnd;
#pragma data_seg ()
Bool apientry dllmain (Handle Hmodule,
DWORD UL_REASON_FOR_CALL,
LPVOID LPRESERVED
)
{
G_HINSTANCE = (hinstance) hmodule;
Switch (ul_reason_for_call)
{
Case DLL_Process_attach:
Case DLL_THREAD_ATTACH:
Case DLL_THREAD_DETACH:
Case DLL_PROCESS_DETACH:
Break;
}
Return True;
}
// Keyboard message handler
LResult TestPopup_API Callback KeyboardProc (int Ncode, WPARAM WPARAM, LPARAM LPARAM)
{
// IF ((DWORD) LPARAM & 0x40000000) && (hc_action == ncode));
Word wkey = (word) WPARAM;
// button
IF ((HiWord (LPARAM) & kf_up) == 0 && HC_Action == NCODE)
{
IF (WKEY == VK_ADD) // is a hotkey
{
// Get the front desk window (game window)
HWND HWND = :: getForeGroup ";
// Create and display Mode dialog
:: DialogBox (G_HINSTANCE, MakeintResource (IDD_MAIN_DIALOG), HWND, DLGPROC (DialogProc);
}
}
Lresult Retval = CallNextHookex (G_hKeyhook, Ncode, WPARAM, LPARAM);
Return RetVal;
}
Bool TestPopup_API InstallhotKey ()
{
IF (g_fisinstalled) returnaf false;
G_hKeyhook = :: setwindowshookex (wh_keyboard, (hookProc) KeyboardProc, g_hinstance, 0); // Install HOOK
g_fisinstalled = true;
Return True;
}
Bool TestPopup_API Uninsthotkey ()
{
BOOL BRET;
Bret = :: UnHookWindowsHookex (G_hKeyhook);
g_fisinstalled = false;
g_hKeyhook = null;
Return Bret;
}
Bool Callback DialogProc (HWND HWNDDLG, UINT UMSG, WPARAM WPARAM, LPARAM LPARAM)
{
Switch (UMSG)
{
Case WM_INITDIALOG:
Return True;
Case WM_COMMAND:
Switch (loword (wparam))
{
Case IDCANCEL:
Case IDOK:
EndDialog (HWNDDLG, TRUE);
Return True;
}
Break;
Case WM_MOVE:
Break;
Case WM_DESTROY:
g_hwnd = null;
Break;
}
Return False;
}
//Testpopupdemodlg.c
Void ctestpopupdemodlg :: OneNablehotKey ()
{
// Todo: Add Your Control Notification Handler Code Here
Updatedata ();
IF (M_FhotKeyenable)
{
:: InstallhotKey ();
}
Else
{
:: UninsthotKey ();
}
Updatedata (FALSE);
}
This program is debugged by Microsoft Visual C , Win97.
Author E-mail: malloc@sohu.com