您的位置:首页 > 大数据 > 人工智能

vs2005 int _tmain(int argc, _TCH…

2013-10-23 23:37 246 查看
用VS2005中的VC++做一个WIN32控制台(console)下的应用程序,向导程序默认给出的是以下内容:

1

#include "stdafx.h"
2


3

int _tmain(int argc, _TCHAR* argv[])

4





{
5


6

return 0;

7


8

}

想当然,很好,很容易。试试列出命令的参数:

1

#include "stdafx.h"

2


3

using namespace std; //必须在stdafx.h增加#include <iostream>

4


5

int _tmain(int argc, _TCHAR* argv[])

6





{
7

cout << argc <<endl;

8

cout << argv[0] <<endl;

9

return 0;

10

}

11


运行结果竟然是这样的:

D:\MyData\CSharp\Projects\test\Debug>test
1
003A5210
“003A5210”是什么值?一开始就弄不懂了。
如果把_tmain函数变为:

1

int main(int argc, char* argv[])

2





{
3

cout << argc <<endl;

4

cout << argv[0] <<endl;

5

return 0;

6

}

7


运行结果正常:

D:\MyData\CSharp\Projects\test\Debug>test

1

d:\MyData\CSharp\Projects\test\Debug\test.exe
头大了。最后分别对两个函数运用断点中的反汇编看看,发现原来**argv竟然是wchar_t**,再翻弄了一下_TCHAR的声明:typedef wchar_t
_TCHAR;
一切明白了。要输出这个_TCHAR只能用cout的另一个版本:wcout。
为什么呢?原因很简单,因为他们都带了一个“w”在前面啊!


能正确输出_TCHAR*
argv[]的版本:

1

#include "stdafx.h"

2


3

using namespace std;

4


5

int _tmain(int argc, _TCHAR* argv[])

6





{
7

wcout << argc <<endl;

8

wcout << argv[0] <<endl;

9

return 0;

10

}

11


给出一个网上对_tmain的一个有用的定义:


对于ANSI版本,"_tWinMain"就是"WinMain";对于UINCODE版本,"_tWinMain"就是"wWinMain"。

(比如这样的定义:)


1

#ifdef _UNICODE

2

#define _tmain wmain

3

#define _tWinMain wWinMain

4

#else

5

#define _tmain main

6

#define _tWinMain WinMain

7

#endif
所以,_tmain()不过是unicode版本的的main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: