当前位置:网站首页>Recognize the startup function and find the user entry
Recognize the startup function and find the user entry
2022-06-28 13:21:00 【Spadefish】
Linux 0.13 edition 1.5W That's ok
< > and "" Affect search order
Linux 0.13 edition 1.5W That's ok
Code specification
Huawei C Language programming specifications http://t.csdn.cn/h5OOZ
Check your consciousness
build.bat Script
del *.obj
del *.exe
cl /c /W3 /WX /P hello.c /P: It is often used when there are problems with multiple include files or complex macros Use P Generate .i Document review the document
link hello.obj
pause # Pause to see the results
About C Language input and output redirection
1、 One is to set in the program : Use freopen Redirect input and output .
C The standard input and output of the language is stdin and stdout, The types of these two variables are FILE* type , in other words , Standard I / O operation , Its essence is file operation .
When redirection is required , You can call
:::info
#include <stdio.h>
freopen(“d:\data_in.txt”,“r”,stdin); Direct input to file d Files on disk data_in.txt file
(linux The path under is a little different :freopen(“/data_in.txt”,“r”,stdin); Represents in the root directory data_in.txt file .)
freopen(“d:\data_out.txt”,“w”,stdout); Direct the output to d On the plate data_out.txt Catalog .
:::
When this function is called , A reference header file is required stdio.h, In the use of freopen() Subsequent standard outputs or inputs are redirected , And the previous will not change .
2、 The other is runtime redirection :
Enter at the command line : myprog.exe > X:\data_out.txt ( stay myprog.exe Under the folder )
You can redirect the output to X On the plate data_out.txt In file , All output from the program will be redirected to this file , Unless... Is used in the program freopen() function , If so , In the program freopen() The output before the function is redirected to data_out.txt file , and freopen() Later will be directed to freopen() In the specified file .
:::info
Redirect input in a similar way : Enter at the command line myprog.exe < data_in.txt
:::
When the program appears in turn scanf()、getchar() When entering a function , Will automatically from the file in turn data_in.txt Read the corresponding length of text in ( In bytes ). Note that the end of the file is marked as EOF
For example, to read all the text in a file, you can write the following code :
while( getchar() != EOF)
{
putchar();
}
Can redirect to file 、ftp( File server )、 LAN 、 The printer
Redirect output
>
hello > c:/test.txt
Redirect input
<
stdout stdin stderr( Output to standard equipment 、 Monitor 、 It is used to keep a log )
#include <stdio.h>// If it is a standard library or an official library <>; The custom library written by myself uses " "
//<> and "" Affect search order
//Linux 0.13 edition 1.5W That's ok
// Code specification
// Check your consciousness
int main(int argc,char*argv[])//main The return value of the function is the return value of the process
{
// Format output to standard output device (stdout)
int n = printf("hello\r\n"); // Functions with variable parameters // Return value : Number of bytes successfully output , Negative numbers are failures
printf("%d\r\n",n);
// fprintf(stderr,"hello world!\n");
return 0; //exit(0)->ExitProcess(0)
}
The real entry to the program
main or WinMain The function needs to have a caller , Before they are called , The compiler has actually done a lot , therefore main or WinMain yes “ Syntax defined user entry ”, instead of “ Application entry ”.
Enter into mouth generation code Its real and No yes main or WinMain Letter Count , through often yes mainCRTStartup 、wmainCRTStartup 、 WinMainCRTStartup or wWinMainCRTStartup , have body Depending on the Ed translate choose term and set . This is the actual launcher of the application . It calls the user's main routine [w]main() or [w]WinMain perform C Runtime library initialization . Its in mainCRTStartup and wmainCRTStartup It is multi byte encoding in the console environment and Unicode Ed code Of Qi dynamic Letter Count , and WinMainCRTStartup and wWinMainCRTStartup It is Windows Multi byte coding and Unicode Coded startup function . In the development process ,C++ Also allows programmers to specify their own entry .
mainCRTStartup Detailed explanation of
Get the full Win32 version Get the complete Win32 edition
initialize heap Initializing the heap
GetCommandLineA function : Get the command line parameter letter The first address of the message .
_crtGetEnvironmentStringsA function : Get environment change The first address of the quantity information .
_setargv function : This function is based on GetCommandLineA Get the first address of the command line parameter information and perform parameter analysis , Save the number of separated parameters in the global variable _argc in , Store the first address of each command line parameter analyzed in the array in , And save the first address of the character pointer array in the global Variable _argv in . Thus, the number of command line parameters is obtained , And command line parameter information .
_setenvp function : This function is based on _crtGetEnvironmentStringsA Function to Get the first address of the environment variable information , And then analyze it , The first address of each environment variable string will be obtained and stored in the character pointer array , Then the first address of the array is stored in the global variable env in .
When calling main Function , It will _argc、_argv、env this Three global variables as parameters , Passed to by stack parameter main Function .
initialize multi-thread Initialize multithreading
initialize lowio Initialize low level io
do C data initialize do C Data initialization
call run time initializer Call the runtime initializer
// Precompile macro
#else/*_WINMAIN_*/
#ifdef WPRFLAG
// Wide character console startup function
void wmainCRTStartup(
#else/*WPRFLAG*/
// Multi byte console startup function
void mainCRTStartup(
#endif/*WPRFLAG*/
#endif/*_WINMAIN_*/
void
)
{
// Get version information
_osver=GetVersion();
_winminor=(_osver>>8)&0x00FF;
_winmajor=_osver&0x00FF;
_winver=(_winmajor<<8)+_winminor;
_osver=(_osver>>16)&0x00FFFF;
// Heap space initialization process , In this function , Specifies the starting address of the heap space in the program
//_MT Is a multithreaded tag
#ifdef_MT
if(!_heap_init(1))
#else/*_MT*/
if(!_heap_init(0))
#endif/*_MT*/
fast_error_exit(_RT_HEAPINIT);
// Initialize the multithreaded environment
#ifdef_MT
if(!_mtinit())
fast_error_exit(_RT_THREAD);
#endif/*_MT*/
__try{
// Wide character processing code is omitted
// Multibyte version get command line
_acmdln=(char*)GetCommandLineA();
// Multi byte version obtains environment variable information
_aenvptr=(char*)
__crtGetEnvironmentStringsA();
// Multi byte version to get command line information
_setargv();
// Multi byte version to obtain environment variable information
_setenvp();
#endif/*WPRFLAG*/
// Initialize global data and floating-point registers
_cinit();
// Window program processing code slightly
// Wide character processing code is omitted
// Get environment variable information
_initenv=_environ;
// call main function , Pass command line parameter information
mainret=main(_argc,_argv,_environ);
#endif/*WPRFLAG*/
#endif/*_WINMAIN_*/
// Check main Function returns a value to execute a destructor or atexit Registered function pointer , And end the program
exit(mainret);
}
// Exit end code is omitted
}
main The function has the following Is characterized by : It has 3 Parameters , Is the number of command line parameters 、 Command line parameter information and environment variable information , And it is the only startup function that has 3 A function of parameters .
stay VC++6.0 in ,main A function that must be called before it is called Function as follows :
GetVersion()
_heap_init()
GetCommandLineA()
_crtGetEnvironmentStringsA()
_setargv()
_setenvp()
_cinit()
When these function calls are completed, they will call main function . root According to the main Features of function calls , Will 3 Parameters _argc、_argv、env Push on the stack as a function parameter .
extern "C" int mainCRTStartup()
{
return __scrt_common_main();
}
static __forceinline int __cdecl __scrt_common_main()
{
// Initialization buffer overflow global variable
__security_init_cookie();
return __scrt_common_main_seh();
}
static __declspec(noinline) int __cdecl
__scrt_common_main_seh()
{
// For initialization C Global data in Syntax
if (_initterm_e(__xi_a, __xi_z) != 0)
return 255;
// For initialization C++ Global data in Syntax
_initterm(__xc_a, __xc_z);
// Initialize thread local storage variables
_tls_callback_type const* const tls_init_callback =
__scrt_get_dyn_tls_init_callback();
if (*tls_init_callback != nullptr &&
__scrt_is_nonwritable_in_current_image(tls_init_callback))
{
(*tls_init_callback)(nullptr, DLL_THREAD_ATTACH, nullptr);
}
// Register thread local storage destructors
_tls_callback_type const * const tls_dtor_callback =
__scrt_get_dyn_tls_dtor_callback();
if (*tls_dtor_callback != nullptr &&
__scrt_is_nonwritable_in_current_image(tls_dtor_callback))
{
_register_thread_local_exe_atexit_callback(*tls_dtor_callba
ck);
}
// Initialization complete call main() function
int const main_result = invoke_main();
//main() Function returns to execute a destructor or atexit Registered function pointer , And end the program
if (!__scrt_is_managed_app())
exit(main_result);
}
static int __cdecl invoke_main()
{
// call main function , Pass command line parameter information
return main(__argc, __argv,
_get_initial_narrow_environment());
}
When to check printf The return value of ?( Check your consciousness )
1、 As long as the data is out of memory ,( disk 、 LAN 、) Consider redirection , Do a step-by-step check
Use winhex View the address initialization value and the address value modified data
Test code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* standard Write a line , Specification line For example, for the total number of students int nStudentCount = 0; float fStudentCount = 0.0f; or flStudentCount; double dblStudentCount =0.0; char cStudentCount = '\0'; short int snStudentCount = 0; int *pnStudentCount = NULL; void *pv; */
//0x0018ff44
// An uninitialized local variable returns : The value left by the code of this address
int n = 0;
// Format input from the standard input device to the specified memory address
// Small tail way : Low data bits are stored at low addresses , The high data bit exists at the high address
// Big tail way : The high data bits are stored at the low address , The low data bits exist at the high address
// The architecture at the beginning of computer design determines
//0xc0000005 Memory access exception
printf("%08x\r\n",&n);
system("pause");
scanf("%d",&n);
printf("%d\r\n",n);
system("pause");
return 0;
}
First run the program , Be careful not to quit the program , To add system(“pause”); After operation, as shown in the figure

Use winhex open RAM Select the process to run the program 、 All memory

Enter the address you just output , Go to find jump

It is shown as follows

Continue running the program output

return winhex View know , The value of the address has been modified


Changed to E7030000
reason :
Small tail way : Low data bits are stored at low addresses , The high data bit exists at the high address
Big tail way : The high data bits are stored at the low address , The low data bits exist at the high address
The architecture at the beginning of computer design determines
This post refers to 《C++ The technology of disassembly and reverse analysis is revealed 》
边栏推荐
- 一文抄 10 篇!韩国发表的顶级会议论文被曝抄袭,第一作者是“原罪”?
- 电驴怎么显示服务器列表,(转)如何更新电驴服务器列表(eMule Server List)
- FS7022方案系列FS4059A双节两节锂电池串联充电IC和保护IC
- Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”
- Implementation of fruit and vegetable mall management system based on SSM
- 完全背包 初学篇「建议收藏」
- Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
- Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
- The counter attack story of Fu Jie, a young secondary school student: I spent 20 years from the second undergraduate to the ICLR outstanding Thesis Award
- Google Earth engine (GEE) - Global organic soil area of FAO (1992-2018)
猜你喜欢
随机推荐
专业英语历年题
同花顺上怎么进行开户啊, 安全吗
在线JSON转PlainText工具
股票网上开户及开户流程怎样?手机开户是安全么?
4年用户数破亿,孙哥带领波场再创新高
Oceanwide micro fh511 single chip microcomputer IC scheme small household appliances LED lighting MCU screen printing fh511
一种跳板机的实现思路
Watermaker of the Flink core
895. 最长上升子序列
Solution to directory access of thinkphp6 multi-level controller
c语言中的类结构体-点号
Hang Seng Electronics: lightdb, a financial distributed database, has passed a number of evaluations by China Academy of communications technology
pytorch模型调参、训练相关内容
Fh511+tp4333 form an outdoor mobile power lighting camping lamp scheme.
Complete backpack beginner chapter "suggestions collection"
fastposter v2.8.4 发布 电商海报生成器
Mobile web training day-2
PHP根据年月获取月初月末时间
Align content attribute in flex layout
The English translation of heartless sword Zhu Xi's two impressions of reading








