Visual C++中的TCHAR

      为了使代码兼容ASCII码和Unicode编码,微软公司还提供了通用字符类型TCHAR。

     通用字符类型的含义是,如果在项目属性中选择“Unicode字符集”,则TCHAR代表WCHAR,或者在项目中选择“多字符集”,则TCHAR代表char。

     喜欢刨根问底的读者,可以在一个使用了TCHAR的项目中,在TCHAR上单击鼠标右键,选择“转到定义”,然后仔细查看随后打开的winnt.h中的内容。这里需要C语言中的条件预处理命令的知识。特别要注意的是,对于winnt.h这样由系统提供的头文件,我们可以打开它,但是千万不要修改它。

     在使用通用类型的前提下,程序员只需要注意以下3点:

1)凡是使用关键字char的地方都用TCHAR取代;

2)凡是用char * 的地方都用LPTSTR取代;

3)凡是定义在双引号中的字符串常量,都用TEXT宏或者_T宏重写。

     大多数人在学习C语言时,就习惯使用了C-Run Time库的许多字符串处理函数。由于微软公司建议使用通用字符类型,所以表1所示的对照表,如下:

   

    建议初学者通过MSDN了解各种常用TCHAR版本的函数。例如,我们希望使用如下语句在控制台应用程序中输出“Hello world!”:  

printf("%s\n","Hello world!");

   通过MSDN查找printf的使用帮助,可以知道对应的TCHAR版本是_tprintf,则对应的代码是:

_tprintf("_T(%s)",_T("Hello world!"));

   特别要注意的是,由于C运行库(C-Run Time,CRT)对Unicode的支持不健全,使用_tprintf输出Unicode会有问题,需要在程序的入口处设置如下本地属性:

#include <locale.h>
#include <tchar.h>
#include <stdio.h>
int _tmain(int argc,_TCHAR* argv[]){
	//设置本地属性
	setlocale(LC_ALL,"CHS");
	_tprintf("_T(%s)",_T("Hello world!"));

	printf("\n");
	return 0;
}


效果如下:




  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
网络课程设计用 Visual C++实现局域网 IP 多播 // task3UIDlg.cpp : implementation file // #include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> #include <String.h> #pragma comment(lib,"ws2_32") #include "StdAfx.h" #include "task3UI.h" #include "task3UIDlg.h" #include "../task3UI/CInitSock.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define MCASTADDR "233.0.0.1" //本例使用的多播组地址。 #define MCASTPORT 9999 //本地端口号。 #define BUFSIZE 1024 //发送数据缓冲大小。 WSADATA wsd; struct sockaddr_in local,remote,from; SOCKET sock,sockM; TCHAR sendbuf[BUFSIZE]; TCHAR recvbuf[BUFSIZE]; char ip[255]; int len; CString strrece,strsend; int CTask3UIDlg::KMeansSegment() { // 主要处理函数在这里写 int ret; while(1){ if(( ret = recvfrom(sock,recvbuf,BUFSIZE,0,(struct sockaddr*)&from;,&len;)) == SOCKET_ERROR) { printf("recvfrom failed with:%d\n",WSAGetLastError()); closesocket(sockM); closesocket(sock); WSACleanup(); return -1; } if( strcmp(recvbuf,"QUIT") == 0 ) { break; }else{ recvbuf[ret] = '\0'; if(strcmp(ip,inet_ntoa(from.sin_addr))!=0){ printf("%s : %s\n",inet_ntoa(from.sin_addr),recvbuf); GetDlgItemText(IDC_RECE,strrece); SetDlgItemText(IDC_RECE,strrece+inet_ntoa(from.sin_addr)+":"+recvbuf+"\r\n"); } } } return 0L; } UINT myproc(LPVOID lParam) { CTask3UIDlg *pWnd = (CTask3UIDlg *)lParam; //将窗口指针赋给无类型指针 pWnd->KMeansSegment(); //要执行的函数 return 1; } bool GetLocalIP(char* ip) { char szText[256]; //获取本机主机名称 int iRet; iRet = gethostname(szText,256); int a = WSAGetLastError(); if (iRet!=0) { printf("gethostname() Failed!"); return FALSE; } //通过主机名获取到地址信息 HOSTENT *host = gethostbyname(szText); if (NULL==host) { printf("gethostbyname() Failed!"); return false; } in_addr PcAddr; for (int i=0;;i++) { char *p = host->h_addr_list[i]; if (NULL==p) { break; } memcpy(&(PcAddr.S_un.S_addr),p,host->h_length); ip = ::inet_ntoa(PcAddr); } return true; } int chushihua() { len = sizeof( struct sockaddr_in); //初始化 WinSock2.2 if( WSAStartup( MAKEWORD(2,2),&wsd;) != 0 ) { printf("WSAStartup() failed\n"); return -1; } if((sock=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0, WSA_FLAG_MULTIPOINT_C_LEAF|WSA_FLAG_MULTIPOINT_D_LEAF| WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET) { printf("socket failed with:%d\n",WSAGetLastError()); WSACleanup(); return -1; } //将 sock 绑定到本机某端口上。 local.sin_family = AF_INET; local.sin_port = htons(MCASTPORT); local.sin_addr.s_addr = INADDR_ANY; if( bind(sock,(struct sockaddr*)&local;,sizeof(local)) == SOCKET_ERROR ) { printf( "bind failed with:%d \n",WSAGetLastError()); closesocket(sock); WSACleanup(); return -1; } //加入多播组 remote.sin_family = AF_INET; remote.sin_port = htons(MCASTPORT); remote.sin_addr.s_addr = inet_addr( MCASTADDR ); if(( sockM = WSAJoinLeaf(sock,(SOCKADDR*)&remote;, sizeof(remote),NULL,NULL,NULL,NULL, JL_BOTH)) == INVALID_SOCKET) { printf("WSAJoinLeaf() failed:%d\n",WSAGetLastError()); closesocket(sock); WSACleanup(); return -1; } } int mainmain() { //发送多播数据,当用户在控制台输入"QUIT"时退出。 //while(1) //{ //scanf("%s",sendbuf); if( sendto(sockM,(char*)sendbuf,strlen(sendbuf),0,(struct sockaddr*) &remote;,sizeof(remote))==SOCKET_ERROR) { printf("sendto failed with: %d\n",WSAGetLastError()); closesocket(sockM); closesocket(sock); WSACleanup(); return -1; } //if(strcmp(sendbuf,"QUIT")==0) break; //Sleep(500); //} /*closesocket(sockM); closesocket(sock); WSACleanup();*/ return 0; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP(); }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTask3UIDlg dialog CTask3UIDlg::CTask3UIDlg(CWnd* pParent /*=NULL*/) : CDialog(CTask3UIDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTask3UIDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTask3UIDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTask3UIDlg) DDX_Control(pDX, IDC_RECE, m_Edit); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTask3UIDlg, CDialog) //{{AFX_MSG_MAP(CTask3UIDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_tishi, Ontishi) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTask3UIDlg message handlers BOOL CTask3UIDlg::OnInitDialog() { CDialog::OnInitDialog(); chushihua(); GetLocalIP(ip); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon AfxBeginThread(myproc, (LPVOID)this); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CTask3UIDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CTask3UIDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect;); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CTask3UIDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CTask3UIDlg::OnOK() { //CString strrece,strsend; // TODO: Add your control notification handler code here GetDlgItemText(IDC_SEND,strsend); strcpy(sendbuf, strsend); mainmain(); } void CTask3UIDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel(); } void CTask3UIDlg::Ontishi() { // TODO: Add your control notification handler code here } void CTask3UIDlg::Onsend() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here }
Visual Leak Detector是一款免费的、健全的、开源的Visual C++内存泄露检测系统。相比Visual C++自带的内存检测机制,Visual Leak Detector可以显示导致内存泄露的完整内存分配调用堆栈。 下载Visual Leak Detector,当前版本2.2.3,在Visual C++ IDE的"工具"→"选项"→"项目和解决方案"→"VC++ 目录","包含文件"增加VLD的"\include"路径,"库文件"增加VLD的"\lib\Win32"路径,另外动态库"\bin\Win32"路径在安装时已经添加到环境变量里面了,若是未添加,则需要手动拷贝"\bin\Win32"下的文件到工程Debug目录。下 展开查看详细 收起信息 返回顶部 1.新建一个Win32控制台项目; 2.添加代码如下所示: #include "stdafx.h" #include "vld.h" int _tmain(int argc, _TCHAR* argv[]) { char *pBuf = new char[200]; return 0; } 3.在Debug模式下的“输出”窗口,将有如下信息: 报告列出了内存泄露是在第几块,所在的地址,泄露的字节,调用的堆栈,内存内容。双击调用堆栈可以跳转到所在行。 4.在Release模式下,不会链接Visual Leak Detector。 5.Visual Leak Detector有一些配置项,可以设置内存泄露报告的保存地(文件、调试器),拷贝"\Visual Leak Detector"路径下的vld.ini文件到工程的Debug目录下(在IDE运行的话,则需要拷贝到工程目录下),修改以下项: ReportFile = .\memory_leak_report.txt ReportTo = both 直接双击Debug目录下exe,文件内容跟“输出”窗口的内容一样。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值