_tmain main wmain WinMain

在visual c++ 2008 中,当选择编辑一个32位Win32控制台应用程序时。
初始状态下系统自带函数:
  int _tmain(int argc, _TCHAR* argv[])
  {
      return 0;
  }
上述Win32控制台应用程序的入口程序是用来存放机器的一个环境变量的,如:机器名,系统信息等。
其中:
·int argc //参数个数
·char *argv[] //字符串数组,字符串数组的每个单元是 char* 类型的,指向一个C风格字符串。
·_TCHAR类型是宽字符型字符串,它是32位或者更高的操作系统中所使用的类型。
出处:
  #include <iostream>
  #include <string.h>
  using namespace std;
  void main(int argc,char *argv[],char *envp[])
  {
      int iNumberLines=0; // Default is no line numbers.
      // If more than .EXE filename supplied, and if the
      // /n command-line option is specified, the listing
      // of environment variables is line-numbered.
      if(argc==2&&strcmp(argv[1],"/n")==0)
      {
          iNumberLines=1;
      } // Walk through list of strings until a NULL is encountered.
      for(int i=0;envp[i]!=NULL;++i )
      {
          if(!iNumberLines)
          cout<<i<<":"<<envp[i]<<"\n";
      }
  }
The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user’s environment variables.

_tmain:
1. main是所有C或C++的程序执行的起点,_tmain是main为了支持unicode所使用的main的别名。
2. _tmain需要一个返回值,而main默认为void。
3. _tmain的定义在<tchar.h>可以找到,如#define _tmain main,所以要加 #include <tchar.h> 才能用。
4. _tmain()是个宏,如果定义了UNICODE,则他是wmain(),否则他是main()。
5. _tmain这个符号多见于VC++创建的控制台工程中,这个是为了保证移植unicode而加入的。
6. 一般_t、_T、T()这些东西都是宏都和unicode有关系。
7. 对于使用非unicode字符集的工程来说,实际上_tmain和main没有差别。
8. 因此_tmain编译后后仍为main,所以都可以执行。
9. main()是WINDOWS的控制台程序(32BIT)或DOS程序(16BIT)。
10.WinMain()是WINDOWS的GUI程序。
11.另外,wmain也是main的另一个别名,是为了支持二个字节的Unicode语言环境。
  -----------------------
  int main( int argc, char *argv[ ], char *envp[ ])
  wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
  int _tmain(int argc, _TCHAR* argv[ ])

 

实例分析:

使用VS2005创建了一个Win32控制台程序,生成的主函数傻眼了:

1 #include "stdafx.h"  
2 int _tmain(int argc, _TCHAR* argv[])  
3 {  
4     return 0;  
5 } 

查看了一下stdafx.h这个文件,如下:

1 #pragma once  
2   
3 #ifndef _WIN32_WINNT        // 允许使用特定于 Windows XP 或更高版本的功能。  
4 #define _WIN32_WINNT 0x0501 // 将此值更改为相应的值,以适用于 Windows 的其他版本。  
5 #endif                        
6   
7 #include <stdio.h>  
8 #include <tchar.h>  

跟到tchar.h文件中,发现该头文件中定义了下面的语句:

 1 #ifdef  UNICODE  
 2 /* ++++++++++++++++++++ UNICODE ++++++++++++++++++++ */  
 3   
 4 #define _TEOF       WEOF  
 5   
 6 #define _tmain      wmain  
 7 ....  
 8 #else   /* ndef UNICODE */  
 9   
10 /* ++++++++++++++++++++ SBCS (MBCS in Not supported) ++++++++++++++++++++ */  
11   
12 #define _TEOF       EOF  
13   
14 /* Program */  
15 #define _tmain      main  
16 #define _tWinMain   WinMain  

可以看到,如果定义了UNICODE这个宏,那么编译时_tmain会被wmain替换;如果没有定义UNICODE宏,则被main替换。

wmain是支持unicode字符的一个主函数。

 注:_tmain这个符号多见于VC++创建的控制台工程中,这个是为了保证移植unicode而加入的 。一般_t、_T、T()这些都是宏,都和unicode有关系。

 

 

转载于:https://www.cnblogs.com/sunstrong/p/4053663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值