📄 winmain.cpp
字号:
// winmain.cpp
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mbctype.h>
#include <errno.h>
#include "msc.h"
#include "winmain.h"
// memo -D
// NOCRT Do not use C-Runtime Library
// NOARG Declared main without argc, argv
// NT_ONLY Cannot run on 95/98/Me platforms.
// select UNICODE entry point compulsorily
#if defined _MSC_VER && ! defined _UNICODE
#define _UNICODE
#endif
#ifdef _UNICODE
static int SetArgumentA ( void ) ;
#else
static int SetArgumentW ( void ) ;
#endif
static int DisplayError ( const char *szMessage ) ;
static void __cdecl AlertOnClose ( void ) ;
#if defined _CONSOLE || defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
static const char szCriticalError [] = "Critical error - not enough memory.\n" ;
#endif
#if defined _CONSOLE || defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
#if defined NT_ONLY
static const char szPlatformError [] = "Critical error - cannot be run on 95/98/Me.\n" ;
#endif
#endif
#if defined _CONSOLE
static const char szAlertOnClose [] = "Press any key to close..." ;
#endif
#if defined _WINDOWS && ! defined _USRDLL
#define MessageBoxA MessageBoxA_
typedef int ( WINAPI *MESSAGEBOXA ) ( HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType ) ;
static HINSTANCE hUser32 ;
static MESSAGEBOXA MessageBoxA ;
#endif
////////////////////////////////////////////
// COMMON //
////////////////////////////////////////////
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _MSC_VER
int __cdecl _setenvp ( void ) { return 0 ; }
int __cdecl _wsetenvp ( void ) { return 0 ; }
char *__cdecl __crtGetEnvironmentStringsA ( void ) { return NULL ; }
wchar_t *__cdecl __crtGetEnvironmentStringsW ( void ) { return NULL ; }
#endif // _MSC_VER
#if defined _WINDOWS && defined _USRDLL || defined NOARG
#ifdef _MSC_VER
int __cdecl _setargv ( void ) { return 0 ; }
int __cdecl _wsetargv ( void ) { return 0 ; }
char *__cdecl __crtGetCommandLineA ( void ) { return NULL ; }
wchar_t *__cdecl __crtGetCommandLineW ( void ) { return NULL ; }
#endif // _MSC_VER
#endif // defined _WINDOWS && defined _USRDLL
#if defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
#ifdef _MSC_VER
char *__cdecl _wincmdln ( void ) { return NULL ; }
wchar_t *__cdecl _wwincmdln ( void ) { return NULL ; }
#endif // _MSC_VER
#endif // defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
#if defined NOCRT
#ifdef _MSC_VER
#pragma comment ( linker, "/nodefaultlib" )
#ifdef _USRDLL
#pragma comment ( linker, "/entry:DllMain" )
#else
#pragma comment ( linker, "/entry:main" )
#undef main
int main ( void ) { return winmain_ () ; }
#endif
#endif // _MSC_VER
#endif // defined NOCRT
#ifdef __cplusplus
}
#endif
////////////////////////////////////////////
// WINDOWS //
////////////////////////////////////////////
#if defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
#ifdef _UNICODE
int WINAPI wWinMain ( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow )
{
#if defined NT_ONLY
if ( ! IsNT () ) return DisplayError ( szPlatformError ) ;
#endif
#if defined NOARG
return winmain_ () ;
#else
if ( SetArgumentA () ) return DisplayError ( szCriticalError ) ;
return winmain_ ( __argc, __argv ) ;
#endif
}
#else
int WINAPI WinMain ( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
#if defined NT_ONLY
if ( ! IsNT () ) return DisplayError ( szPlatformError ) ;
#endif
#if defined NOARG
return winmain_ () ;
#else
if ( SetArgumentW () ) return DisplayError ( szCriticalError ) ;
return winmain_ ( __argc, __argv ) ;
#endif
}
#endif
static int DisplayError ( const char *szMessage ) {
if ( IsNT () ) hUser32 = LoadLibraryW ( L"USER32.DLL" ) ;
else hUser32 = LoadLibraryA ( "USER32.DLL" ) ;
if ( GETPROCADDRESS ( hUser32, MESSAGEBOXA, MessageBoxA ) ) return 255 ;
MessageBoxA ( NULL, szMessage, "", MB_OK | MB_ICONHAND | MB_SETFOREGROUND ) ;
return 255 ;
}
#endif // defined _WINDOWS && ! defined _USRDLL && ! defined NOCRT
////////////////////////////////////////////
// CONSOLE //
////////////////////////////////////////////
#if defined _CONSOLE && ! defined NOCRT
#ifdef _UNICODE
#undef main
int wmain ( int argc, wchar_t *argv [] ) {
atexit ( AlertOnClose ) ;
#if defined NT_ONLY
if ( ! IsNT () ) return DisplayError ( szPlatformError ) ;
#endif
#if defined NOARG
return winmain_ () ;
#else
if ( SetArgumentA () ) return DisplayError ( szCriticalError ) ;
return winmain_ ( __argc, __argv ) ;
#endif
}
#else
#undef main
int main ( int argc, char *argv [] ) {
atexit ( AlertOnClose ) ;
#if defined NT_ONLY
if ( ! IsNT () ) return DisplayError ( szPlatformError ) ;
#endif
#if defined NOARG
return winmain_ () ;
#else
if ( SetArgumentW () ) return DisplayError ( szCriticalError ) ;
return winmain_ ( __argc, __argv ) ;
#endif
}
#endif
static int DisplayError ( const char *szMessage ) {
unsigned long dwCharWritten ;
WriteFile ( GetStdHandle ( STD_ERROR_HANDLE ), szMessage, (unsigned long) strlen ( szMessage ), & dwCharWritten, NULL ) ;
return 255 ;
}
#endif // defined _CONSOLE && ! defined NOCRT
////////////////////////////////////////////
// ALERT //
////////////////////////////////////////////
#if defined _CONSOLE && ! defined NOCRT
static int IsEnabled = 0 ;
static int IsDisabled = 0 ;
static void WaitKey ( void ) ;
void EnableAlertOnClose ( void ) {
IsEnabled = 1 ;
return ;
}
void DisableAlertOnClose ( void ) {
IsDisabled = 1 ;
return ;
}
// 僄僋僗僾儘乕儔偐傜捈愙屇傃弌偝傟偨傜丄暵偠傞慜偵僉乕擖椡傪懀偡
static void __cdecl AlertOnClose ( void ) {
if ( ( IsEnabled || ! IsDisabled && __argc <= 1 && ! IsConsole () ) && fisatty ( stdout ) && fisatty ( stderr ) ) {
fflush ( NULL ) ;
unsigned long dwCharWritten ;
WriteFile ( GetStdHandle ( STD_ERROR_HANDLE ), szAlertOnClose, (unsigned long) strlen ( szAlertOnClose ), & dwCharWritten, NULL ) ;
WaitKey () ;
}
return ;
}
// 僐儞僜乕儖偐傜僉乕擖椡偑偁偭偨傜惂屼傪曉偡
// 昗弨擖椡偑儕僟僀儗僋僩偝傟偰偄偨傜丄偦偺傑傑惂屼傪曉偡
static void WaitKey ( void ) {
HANDLE hConsole = fget_osfhandle ( stdin ) ;
if ( hConsole == INVALID_HANDLE_VALUE ) return ;
unsigned long dwDummy ;
if ( ! GetConsoleMode ( hConsole, & dwDummy ) ) return ;
while ( 1 ) {
INPUT_RECORD InputRecord ;
unsigned long nReadLength ;
if ( ! ReadConsoleInput ( hConsole, & InputRecord, 1, & nReadLength ) || ! nReadLength ) break ;
if ( InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown ) break ;
}
return ;
}
// 僐儞僜乕儖偐傜婲摦偝傟偨側傜 0 埲奜傪曉偡
int IsConsole ( void ) {
STARTUPINFO StartupInfo = { 0 } ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -