📄 main.cpp
字号:
// Main.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
LPCTSTR szClassName = TEXT( "WINCLASS" );
LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{//////////////////////////////////////////////////////////////////////////////
// 函数名称: WndProc
// 内容简介: Window procedure
// 参 数:
// HWND hWnd: Handle to the current window
// UINT uMsg: The message
// WPARAM wParam:
// LPARAM lParam: Reference MSDN
// 返 回: LRESULT
// 作 者: L.F.
// 开始日期: 2005.06.30
///////////////////////////////////////////////////////////////////////////////
PAINTSTRUCT ps;
HDC hdc = NULL;
TCHAR strSysPath[ MAX_PATH ] = { '0' };
TCHAR buffer[ 512 ] = { '0' };
static PROCESS_INFORMATION ProcessInfo;
static STARTUPINFO StartupInfo;
static HANDLE hMine = NULL;
// Check if version is win2k or winXP
OSVERSIONINFO versionInfo;
int nWidthAddress = 0x0;
int nHeightAddress = 0x0;
int nMinesAddress = 0x0;
int nCellBaseAddress = 0x0;
static BYTE* byPtrIsMine = NULL;
static RECT* rectPtr = NULL;
static BYTE byWidth = 0;
static BYTE byHeight = 0;
static BYTE byMines = 0;
int index = 0;
switch ( uMsg )
{
case WM_CREATE:
GetVersionEx( & versionInfo );
if ( versionInfo.dwMajorVersion >= 5 )
{
// In WIN2K enviroment
if ( versionInfo.dwMinorVersion == 0 )
{
nWidthAddress = 0x10056F8;
nHeightAddress = 0x1005A68;
nMinesAddress = 0x1005A6C;
nCellBaseAddress = 0x1005700;
}
else
{
// In WINXP enviroment
nWidthAddress = 0x1005334;
nHeightAddress = 0x1005338;
nMinesAddress = 0x1005330;
nCellBaseAddress = 0x1005340;
}
}
else
{
MessageBox( \
NULL, \
TEXT( "THIS PROGRAMME NEED YOUR OPERATION SYSTEM IS WIN2K OR WINXP!" ), \
TEXT( "INFORMATION" ),
MB_OK | MB_ICONINFORMATION );
return 0;
}
// Execute the winmine game.
GetWindowsDirectory( strSysPath, MAX_PATH );
lstrcat( strSysPath, TEXT( "\\System32\\WinMine.exe" ) );
OutputDebugString( strSysPath );
OutputDebugString( "\n" );
ZeroMemory( & ProcessInfo, sizeof( PROCESS_INFORMATION ) );
ZeroMemory( & StartupInfo, sizeof( STARTUPINFO ) );
StartupInfo.cb = sizeof( StartupInfo );
if ( CreateProcess( \
strSysPath,
NULL, NULL, NULL, FALSE, 0, NULL, NULL, \
& StartupInfo, \
& ProcessInfo ) )
{
// Wait the CreateProcess function done
WaitForInputIdle( ProcessInfo.hProcess, INFINITE );
sprintf( buffer, "ThreadId: %d\n", ProcessInfo.dwThreadId );
OutputDebugString( buffer );
hMine = OpenProcess( PROCESS_VM_READ, TRUE, ProcessInfo.dwProcessId );
_ASSERT( hMine );
DWORD dwNumOfBytesRead = 0;
ReadProcessMemory( \
hMine, \
( LPVOID ) nWidthAddress, \
& byWidth, \
1, \
& dwNumOfBytesRead );
ReadProcessMemory( \
hMine, \
( LPVOID ) nHeightAddress, \
& byHeight, \
1, \
& dwNumOfBytesRead );
sprintf( buffer, "Mine Game's Width: %d\tMine Game's Height: %d\n", byWidth, byHeight );
OutputDebugString( buffer );
ReadProcessMemory( \
hMine, \
( LPVOID ) nMinesAddress, \
& byMines, \
1, \
& dwNumOfBytesRead );
sprintf( buffer, "Mine Game's Mines: %d\n", byMines );
OutputDebugString( buffer );
byPtrIsMine = new BYTE[ byWidth * byHeight ];
rectPtr = new RECT[ byWidth * byHeight ];
_ASSERT( byPtrIsMine );
_ASSERT( rectPtr );
if ( ! byPtrIsMine || ! rectPtr )
{
MessageBox( \
NULL, \
TEXT( "NO MORE MEMORY ALLOCATE !" ), \
TEXT( "INFORMATION" ), \
MB_OK | MB_ICONINFORMATION );
PostQuitMessage( 0 );
}
ZeroMemory( byPtrIsMine, byWidth * byHeight );
for ( int y = 0; y < byHeight; y++ )
{
for ( int x = 0; x < byWidth; x++ )
{
int nMineCellAddress = nCellBaseAddress + ( 32 * ( y + 1 ) ) + ( x + 1 );
ReadProcessMemory( \
hMine, \
( LPVOID ) nMineCellAddress, \
& byPtrIsMine[ index ], \
1, \
& dwNumOfBytesRead );
sprintf( buffer, "Value Is: 0x%x\n", byPtrIsMine[ index ] );
OutputDebugString( buffer );
rectPtr[ index ].left = ( x * 16 );
rectPtr[ index ].top = ( y * 16 );
rectPtr[ index ].right = ( x * 16 + 16 );
rectPtr[ index ].bottom = ( y * 16 + 16 );
index++;
}
}
index = 0;
}
else
{
MessageBox(
NULL, \
TEXT( "CANN'T CREATE WINMINE PROCESS !" ), \
TEXT( "INFORMATION" ), \
MB_OK | MB_ICONINFORMATION );
PostQuitMessage( 0 );
}
{
// Add the WS_EX_LAYERED attribute
SetWindowLong( hwnd, GWL_EXSTYLE, GetWindowLong( hwnd, GWL_EXSTYLE ) ^ 0x80000 );
HINSTANCE hInst = LoadLibrary( "User32.dll ");
_ASSERT( hInst );
typedef BOOL ( WINAPI* SetWindowLayeredFunc )( HWND, COLORREF, BYTE, DWORD );
SetWindowLayeredFunc Func = NULL;
Func = ( SetWindowLayeredFunc ) GetProcAddress( hInst, "SetLayeredWindowAttributes" );
_ASSERT( Func );
Func( hwnd, 0, 128, 2 );
FreeLibrary( hInst );
RECT rect;
GetWindowRect( hwnd, & rect );
SetWindowPos( \
hwnd, \
HWND_TOPMOST, \
rect.left, rect.top, \
16 * byWidth + 9, 16 * byHeight + 28, \
SWP_NOMOVE );
}
return 0;
case WM_PAINT:
hdc = BeginPaint( hwnd, & ps );
_ASSERT( hdc );
{
for ( int i = 0; i < ( byWidth * byHeight ); i++ )
{
// This is a mine
if ( byPtrIsMine[ i ] == 0x8F )
{
HBRUSH hBrushRed = CreateSolidBrush( RGB( 255, 0, 0 ) );
_ASSERT( hBrushRed );
HGDIOBJ hOldBrush = SelectObject( hdc, hBrushRed );
_ASSERT( hOldBrush );
Rectangle( hdc, \
rectPtr[ i ].left, rectPtr[ i ].top, \
rectPtr[ i ].right, rectPtr[ i ].bottom );
SelectObject( hdc, hOldBrush );
}
else
{
Rectangle( hdc, \
rectPtr[ i ].left, rectPtr[ i ].top, \
rectPtr[ i ].right, rectPtr[ i ].bottom );
}
}
}
EndPaint( hwnd, & ps );
return 0;
case WM_DESTROY:
// Close the winmine game
PostQuitMessage( 0 );
if ( byPtrIsMine )
{
delete byPtrIsMine;
byPtrIsMine = NULL;
}
_ASSERT( byPtrIsMine == NULL );
if ( rectPtr )
{
delete rectPtr;
rectPtr = NULL;
}
_ASSERT( rectPtr == NULL );
CloseHandle( hMine );
TerminateProcess( ProcessInfo.hProcess, 0 );
CloseHandle( ProcessInfo.hThread );
CloseHandle( ProcessInfo.hProcess );
return 0;
default:
break;
}
return DefWindowProc( hwnd, uMsg, wParam, lParam );
}
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow )
{//////////////////////////////////////////////////////////////////////////////
// 函数名称: WinMain
// 内容简介: Window entry point
// 参 数:
// HINSTANCE hInstance: Handle to the application
// LPSTR szCmdLine: Command line
// int nCmdShow:
// 返 回: int
// 作 者: L.F.
// 开始日期: 2005.06.30
///////////////////////////////////////////////////////////////////////////////
HWND hwnd = NULL; // Generic window handle
MSG msg; // Generic message
WNDCLASSEX winclass;
winclass.cbSize = sizeof( WNDCLASSEX );
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WndProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = ( HBRUSH ) GetStockObject( BLACK_BRUSH );
winclass.lpszMenuName = NULL;
winclass.lpszClassName = szClassName;
winclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
// Register the window class
if ( ! RegisterClassEx( & winclass ) )
return 0;
int cxScreen = GetSystemMetrics( SM_CXSCREEN );
int cyScreen = GetSystemMetrics( SM_CYSCREEN );
// Create the window
hwnd = CreateWindowEx(
NULL, // extended style
szClassName, // class
TEXT( "GAME" ), // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // window style
cxScreen / 4, cyScreen / 4, // initial x,y
cxScreen / 2, cyScreen / 2, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hInstance, // instance of this application
NULL ); // extra creation parms
_ASSERT( hwnd );
if ( ! hwnd )
return 0;
// enter main event loop
while ( TRUE )
{
if ( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )
{
if ( msg.message == WM_QUIT )
break;
// translate any accelerator keys
TranslateMessage( & msg );
// send the message to the window proc
DispatchMessage( & msg );
}
} // end while
UnregisterClass( szClassName, hInstance );
return ( msg.wParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -