_tmain main wmain WinMain

来源:互联网 发布:官方淘宝投诉电话号码 编辑:程序博客网 时间:2024/05/02 05:07
在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[ ])
原创粉丝点击