Tag Archive: conversion


Summarized from:

Convert WCHAR_T to LPCWSTR

A wchar_t is the same as a WCHAR (typedef wchar_t WCHAR; from WinNT.h), and LPCWSTR is just an array of WCHAR, like a String is an array of char.

So you already have a LPCWSTR, so if it is asking for a pointer, try putting an ampisand, &, in front of the variable when you use it.

However, I could be totally wrong… but try it anyway.

The string conversion macros discussed here are valid for both ATL and MFC. For more information on MFC string conversion, see TN059: Using MFC MBCS/Unicode Conversion Macros and MFC Macros and Globals.

ATL 7.0 String Conversion Classes and Macros

ATL 7.0 introduces several new conversion classes and macros, providing significant improvements over the existing macros.

The names of the new string conversion classes and macros take the form:

CString是一个动态TCHAR数组,BSTR是一种专有格式的字符串(需要用系统提供的函数来操纵,LPCTSTR只是一个常量的TCHAR指针。

CString 是一个完全独立的类,动态的TCHAR数组,封装了 + 等操作符和字符串操作方法。
typedef OLECHAR FAR* BSTR;
typedef const char * LPCTSTR;

vc++中各种字符串的表示法

首先char* 是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传统的C,C++的兼容。

LP的含义是长指针(long pointer)。LPSTR是一个指向以‘\0’结尾的ANSI字符数组的指针,与char*可以互换使用,在win32中较多地使用LPSTR。
而LPCSTR中增加的‘C’的含义是“CONSTANT”(常量),表明这种数据类型的实例不能被使用它的API函数改变,除此之外,它与LPSTR是等同的。
1.LP表示长指针,在win16下有长指针(LP)和短指针(P)的区别,而在win32下是没有区别的,都是32位.所以这里的LP和P是等价的.
2.C表示const
3.T是什么东西呢,我们知道TCHAR在采用Unicode方式编译时是wchar_t,在普通时编译成char.

为了满足程序代码国际化的需要,业界推出了Unicode标准,它提供了一种简单和一致的表达字符串的方法,所有字符中的字节都是16位的值,其数量也可以满足差不多世界上所有书面语言字符的编码需求,开发程序时使用Unicode(类型为wchar_t)是一种被鼓励的做法。

LPWSTR与LPCWSTR由此产生,它们的含义类似于LPSTR与LPCSTR,只是字符数据是16位的wchar_t而不是char。

然后为了实现两种编码的通用,提出了TCHAR的定义:
如果定义_UNICODE,声明如下:
typedef wchar_t TCHAR;
如果没有定义_UNICODE,则声明如下:
typedef char TCHAR;

LPTSTR和LPCTSTR中的含义就是每个字符是这样的TCHAR。

CString类中的字符就是被声明为TCHAR类型的,它提供了一个封装好的类供用户方便地使用。

LPCTSTR:

Convert integer to Enum instance

Example: Convert integer to Enum instance

public void EnumInstanceFromInt()
{
   // The .NET Framework contains an Enum called DayOfWeek.
   // Let's generate some Enum instances from int values.

   // Usually you wouldn't cast an instance of an existing Enum to an int
   // in order to create an Enum instance.  :-)   You would have the actual
   // integer value, perhaps a value from a database where the int value of
   // the enum was stored.

   DayOfWeek wednesday =
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Wednesday);
   DayOfWeek sunday =
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Sunday);
   DayOfWeek tgif =
      (DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Friday);

   lblOutput.Text = wednesday.ToString()
      + ".  Int value = " + ((int)wednesday).ToString() + "";
   lblOutput.Text += sunday.ToString()
      + ".  Int value = " + ((int)sunday).ToString() + "";
   lblOutput.Text += tgif.ToString()
      + ".  Int value = " + ((int)tgif).ToString() + "";

The Enum.ToObject method takes two arguments. The first is the type of the
enum you want to create as output. The second field is the int to convert.
Obviously, there must be a corresponding Enum entry for the conversion to succeed.

Powered by WordPress | Theme: Motion by 85ideas.