Windows Phone Runtime Component 中的类型转换

Windows Phone Runtime Component 是Windows Phone 平台用来写C++类库的项目类型。c++

主要目的是让C#和C++进行互操做,引用历史的C++代码,保护知识产权,提供性能等。windows

这里要注意可能会涉及到多种类型系统,分别是:ide

  • 标准C++类型系统。可能不多会用到,但也不免。如:char, bool, int 等。
  • 微软Win32类型系统。都是一些宏定义,看着就烦。如:TCHAR, CHAR, LPSTR 等。
  • 微软Windows Runtime类型系统。为了在C++/CX和C#/.NET之间交互的通讯类型。如:HSTRING等。
  • C++/CX类型系统:微软用于开发商店应用和手机应用的C++语言系统。例如:UInt8等。
  • C#/.NET类型系统。虽然CLR和C#的类型描述不大同样,但基本是100%映射的。
 
C# .net CLR std c++ c++/cx windows runtime win32 Length
bool Boolean bool bool Boolean   1
byte Byte char char Char CHAR 8
char Char __wchar_t char16 Char16 wchar_t 16
short Int16 short int16 Int16   16
ushort Int32 unsigned short uint16 UInt16 WORD  16
int Int32 int int Int32   32
uint UInt32 unsigned int uint32 UInt32 DWORD 32
long Int64 long long int64 Int64   64
ulong UInt64 unsigned long long uint64 UInt64 DWORD64 64
float Single float float32 Single FLOAT  
double UInt64 double float64 Double    
string String std:wstring, L"" String^ HSTRING    
             

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

最多见的是C++代码中的字符串都是单字节的,如 char等,而要与C#中的双字节字符串交互,就必须在单字节和双字节之间作转换。固然还有其余类型的转换。性能

wchar_t 到charui

    DWORD nWLength = WideCharToMultiByte(CP_OEMCP, NULL, wchar_s, -1, NULL, 0, NULL, FALSE);
    char_s = new char[nWLength];
    DWORD nCLength = WideCharToMultiByte(CP_OEMCP, NULL, wchar_s, -1, char_s, nWLength, NULL, FALSE);

char 到wchar_tspa

    DWORD nWLength = MultiByteToWideChar(CP_ACP, 0, (LPCCH)char_s, -1, NULL, 0);
    wchar_t *pwValue = new wchar_t[nWLength];
    DWORD nCLength = MultiByteToWideChar(CP_ACP, 0, (LPCCH)char_s, -1, pwValue, nWLength);

String^ 到 wchar_t*.net

String::Data()
相关文章
相关标签/搜索