天下

记录修行的印记

如何:将 System::String 转换为 wchar_t* 或 char*

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vccore/html/385da01b-5649-4543-8076-e3e251243ff0.htm
// convert_string_to_wchar.cpp
// compile with: /clr
#include < stdio.h >
#include < stdlib.h >
#include < vcclr.h >

using namespace System;

int main() {
   String ^str = "Hello";

   // Pin memory so GC can't move it while native function is called
   pin_ptr<const wchar_t> wch = PtrToStringChars(str);
   printf_s("%S\n", wch);

   // Conversion to char* :
   
// Can just convert wchar_t* to char* using one of the 
   
// conversion functions such as: 
   
// WideCharToMultiByte()
   
// wcstombs_s()
   
//  etc
   size_t convertedChars = 0;
   size_t  sizeInBytes = ((str->Length + 1) * 2);
   errno_t err = 0;
   char    *ch = (char *)malloc(sizeInBytes);

   err = wcstombs_s(&convertedChars, 
                    ch, sizeInBytes,
                    wch, sizeInBytes);
   if (err != 0)
      printf_s("wcstombs_s  failed!\n");

    printf_s("%s\n", ch);
}

posted on 2015-12-01 11:08 天下 阅读(825) 评论(0)  编辑 收藏 引用 所属分类: C#


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论