main函数的参数__网络编程之参数学习
是否还记得ppt上的这张图片呢?
这个程序可以带参数,是不是和以前的程序不一样呢?
下面我们就来实现,这可花了我半个小时的时间哦!!!
首先,我们和以前写C语言程序一样,新建一个工程,工程的名字当然是vc啦
然后选择A simple application,然后点击finish,完成工程的创建
开始写代码啦,双击打开vc.cpp文件
开始下面的代码,应该能看懂吧,嘿嘿
1 #include "stdafx.h" 2 #include "stdio.h" 3 4 int main(int argc, char* argv[]) 5 { 6 if (argc==1) 7 { 8 printf("\n\n The content in argv[0] is :%s \n\n",argv[0]); 9 } 10 if (argc==2) 11 { 12 printf("\n\n The command includes 1 parameter: %s \n\n",argv[1]); 13 printf("The content in argv[0] is :%s \n\n",argv[0]); 14 } 15 16 if (argc>2) 17 { 18 printf("\n\n bad command !\n"); 19 } 20 return 0; 21 }
这样就OK了哦,很简单吧!
===================下面是我的东西了,hoho~~~====================
反汇编,找到主函数
1 00401256 |. 52 push edx 2 00401257 |. A1 847C4200 mov eax, dword ptr [__argv] 3 0040125C |. 50 push eax 4 0040125D |. 8B0D 807C4200 mov ecx, dword ptr [__argc] 5 00401263 |. 51 push ecx 6 00401264 |. E8 9CFDFFFF call 00401005
运行到00401257看看情况
进入数据窗口查看003D0D90
看出来是什么没?就是argv[0]啦。
继续运行到0040125D
只有一个参数,是不是,因为我们没有输入
下面是主函数
1 00401005 /$ /E9 06000000 jmp main 2 0040100A | |CC int3 3 0040100B | |CC int3 4 0040100C | |CC int3 5 0040100D | |CC int3 6 0040100E | |CC int3 7 0040100F | |CC int3 8 00401010 >|> \55 push ebp 9 00401011 |. 8BEC mov ebp, esp 10 00401013 |. 83EC 40 sub esp, 40 11 00401016 |. 53 push ebx 12 00401017 |. 56 push esi 13 00401018 |. 57 push edi 14 00401019 |. 8D7D C0 lea edi, dword ptr [ebp-40] 15 0040101C |. B9 10000000 mov ecx, 10 16 00401021 |. B8 CCCCCCCC mov eax, CCCCCCCC 17 00401026 |. F3:AB rep stos dword ptr es:[edi] 18 00401028 |. 837D 08 01 cmp dword ptr [ebp+8], 1 19 0040102C |. 75 13 jnz short 00401041 20 0040102E |. 8B45 0C mov eax, dword ptr [ebp+C] 21 00401031 |. 8B08 mov ecx, dword ptr [eax] 22 00401033 |. 51 push ecx ; /<%s> 23 00401034 |. 68 90204200 push 00422090 ; |format = LF,LF," The content in argv[0] is :%s ",LF,LF,"" 24 00401039 |. E8 82000000 call printf ; \printf 25 0040103E |. 83C4 08 add esp, 8 26 00401041 |> 837D 08 02 cmp dword ptr [ebp+8], 2 27 00401045 |. 75 27 jnz short 0040106E 28 00401047 |. 8B55 0C mov edx, dword ptr [ebp+C] 29 0040104A |. 8B42 04 mov eax, dword ptr [edx+4] 30 0040104D |. 50 push eax ; /<%s> 31 0040104E |. 68 5C204200 push 0042205C ; |format = LF,LF," The command includes 1 parameter: %s ",LF,LF,"" 32 00401053 |. E8 68000000 call printf ; \printf 33 00401058 |. 83C4 08 add esp, 8 34 0040105B |. 8B4D 0C mov ecx, dword ptr [ebp+C] 35 0040105E |. 8B11 mov edx, dword ptr [ecx] 36 00401060 |. 52 push edx ; /<%s> 37 00401061 |. 68 34204200 push 00422034 ; |format = "The content in argv[0] is :%s ",LF,LF,"" 38 00401066 |. E8 55000000 call printf ; \printf 39 0040106B |. 83C4 08 add esp, 8 40 0040106E |> 837D 08 02 cmp dword ptr [ebp+8], 2 41 00401072 |. 7E 0D jle short 00401081 42 00401074 |. 68 1C204200 push 0042201C ; /format = LF,LF," bad command !",LF,"" 43 00401079 |. E8 42000000 call printf ; \printf 44 0040107E |. 83C4 04 add esp, 4 45 00401081 |> 33C0 xor eax, eax 46 00401083 |. 5F pop edi 47 00401084 |. 5E pop esi 48 00401085 |. 5B pop ebx ; 7FFD5000 49 00401086 |. 83C4 40 add esp, 40 50 00401089 |. 3BEC cmp ebp, esp 51 0040108B |. E8 B0000000 call _chkesp 52 00401090 |. 8BE5 mov esp, ebp 53 00401092 |. 5D pop ebp 54 00401093 \. C3 retn
很简单,不解释。