GDAL C#封装对中文字符转换过程中存在问题。

C++封装一个Win32 DLL,采用Unicode字符集。使用标准头文件。

https://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx

  1. class CodePageHelper
  2. {
  3. public:
  4. CodePageHelper(void);
  5. ~CodePageHelper(void);
  6. static wchar_t* ANSIToUnicode( const char* str );
  7. static char* UnicodeToANSI( const wchar_t* str );
  8. static wchar_t* UTF8ToUnicode( const char* str );
  9. static char* UnicodeToUTF8( const wchar_t* str );
  10. };

CodePageHelper.h

  1. #include "StdAfx.h"
  2. #include "CodePageHelper.h"
  3.  
  4. CodePageHelper::CodePageHelper(void)
  5. {
  6. }
  7.  
  8. CodePageHelper::~CodePageHelper(void)
  9. {
  10. }
  11.  
  12. // ANSI to Unicode
  13. wchar_t* CodePageHelper:: ANSIToUnicode( const char* str )
  14. {
  15. int unicodeLen = ::MultiByteToWideChar( CP_ACP,
  16. ,
  17. str,
  18. -,
  19. NULL,
  20. );
  21. wchar_t * pUnicode;
  22. pUnicode = new wchar_t[unicodeLen+];
  23. memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
  24. ::MultiByteToWideChar( CP_ACP,
  25. ,
  26. str,
  27. -,
  28. (LPWSTR)pUnicode,
  29. unicodeLen );
  30. wchar_t* rt;
  31. rt = ( wchar_t* )pUnicode;
  32. //delete pUnicode;
  33.  
  34. return rt;
  35. }
  36. // Unicode to ANSI
  37. char* CodePageHelper:: UnicodeToANSI( const wchar_t* str )
  38. {
  39. char* pElementText;
  40. int iTextLen;
  41. // wide char to multi char
  42. iTextLen = WideCharToMultiByte( CP_ACP,
  43. ,
  44. str,
  45. -,
  46. NULL,
  47. ,
  48. NULL,
  49. NULL );
  50. pElementText = new char[iTextLen + ];
  51. memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
  52. ::WideCharToMultiByte( CP_ACP,
  53. ,
  54. str,
  55. -,
  56. pElementText,
  57. iTextLen,
  58. NULL,
  59. NULL );
  60. char* strText;
  61. strText = pElementText;
  62. //delete[] pElementText;
  63. return strText;
  64. }
  65. // UTF-8 to Unicode
  66. wchar_t* CodePageHelper::UTF8ToUnicode( const char* str )
  67. {
  68. int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
  69. ,
  70. str,
  71. -,
  72. NULL,
  73. );
  74. wchar_t * pUnicode;
  75. pUnicode = new wchar_t[unicodeLen+];
  76. memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
  77. ::MultiByteToWideChar( CP_UTF8,
  78. ,
  79. str,
  80. -,
  81. (LPWSTR)pUnicode,
  82. unicodeLen );
  83. wchar_t* rt;
  84. rt = ( wchar_t* )pUnicode;
  85. //delete pUnicode;
  86.  
  87. return rt;
  88. }
  89. // Unicode to UTF-8
  90.  
  91. char* CodePageHelper::UnicodeToUTF8( const wchar_t* str )
  92. {
  93. char* pElementText;
  94. int iTextLen;
  95. // wide char to multi char
  96. iTextLen = WideCharToMultiByte( CP_UTF8,
  97. ,
  98. str,
  99. -,
  100. NULL,
  101. ,
  102. NULL,
  103. NULL );
  104. pElementText = new char[iTextLen + ];
  105. memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
  106. ::WideCharToMultiByte( CP_UTF8,
  107. ,
  108. str,
  109. -,
  110. pElementText,
  111. iTextLen,
  112. NULL,
  113. NULL );
  114. char* strText;
  115. strText = pElementText;
  116. //delete[] pElementText;
  117. return strText;
  118. }

CodePageHelper

  1. #ifdef DEMIMP_EXPORTS
  2. #define CPL_DLL __declspec(dllexport)
  3. #else
  4. #define CPL_DLL __declspec(dllimport)
  5. #endif
  6.  
  7. #ifndef CPL_DISABLE_STDCALL
  8. # define CPL_STDCALL __stdcall
  9. #endif
  10.  
  11. extern "C"
  12. {
  13. HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath);
  14. };

GDALRaster.h

  1. HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath)
  2. {
  3. //char* file=CodePageHelper::UnicodeToUTF8((const wchar_t*)filepath);
  4.  
  5. char* file1=CodePageHelper::UnicodeToANSI((const wchar_t*)filepath);
  6. //const wchar_t* file2=filepath;
  7. GDALAllRegister();
  8. CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
  9. GDALDataset *pDSrc = (GDALDataset *)GDALOpen(file1, GA_ReadOnly);
  10. if (pDSrc == NULL)
  11. {
  12. return ;
  13. }
  14. char** metadata=pDSrc->GetMetadata("");
  15. return metadata;
  16. }

C# P/Invoke调用:

  1. [DllImport("GDALRaster.dll", EntryPoint = "GetMetaData", CharSet = CharSet.Unicode)]
  2. private static extern IntPtr CSharp_GetMetadata([In, MarshalAs(UnmanagedType.LPWStr)]string filepath);

解析字符串:

  1. public static string[] GetMetaData(string filePath)
  2. {
  3. IntPtr cPtr = CSharp_GetMetadata(filePath);
  4. if (cPtr == IntPtr.Zero) throw new Exception("打开失败");
  5.  
  6. IntPtr objPtr;
  7. int count = ;
  8. if (cPtr != IntPtr.Zero)
  9. {
  10. while (Marshal.ReadIntPtr(cPtr, count * IntPtr.Size) != IntPtr.Zero)
  11. ++count;
  12. }
  13. string[] ret = new string[count];
  14. if (count > )
  15. {
  16. for (int cx = ; cx < count; cx++)
  17. {
  18. objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
  19. ret[cx] = (objPtr == IntPtr.Zero) ? null : System.Runtime.InteropServices.Marshal.PtrToStringAnsi(objPtr);
  20. }
  21. }
  22. return ret;
  23. //double[] temp = new double[xsize * ysize];
  24. //Marshal.Copy(pData, temp, 0, xsize * ysize);
  25. //FreeData(pData);
  26. //return temp;
  27.  
  28. }

字符集WideCharToMultiByte的更多相关文章

  1. 关于多字节、宽字节、WideCharToMultiByte和MultiByteToWideChar函数的详解

    所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE. **************************** ...

  2. Unicode字符集下CString与char *转换 (解决中文乱码等)(转)

    UniCode 下 CString 转 char* 的方法的文章有很多,但是大部分都是在互相转载,看了那么多资料,仍然没有解决乱码的问题,后来从一个论坛的一条回复里面找到了正确的方法,特此拿出来与大家 ...

  3. MULTIBYTETOWIDECHAR的与WIDECHARTOMULTIBYTE的参数详解及相互转换

    第一个就是宽字符到多字节字符转换函数,函数原型如下: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideChar ...

  4. Windows字符集的统一与转换

    以前也零零散散看过一些字符编码的问题,今天看来这边博客,感觉很多东西都总结在里面,非常值得学习! 一.字符集的历史渊源 在Windows编程时经常会遇到编码转换的问题,一直以来让刚接触的人摸不着头脑. ...

  5. 函数WideCharToMultiByte() 详解

    函数原型: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPWSTR lpWideCharStr, int cchWideChar, ...

  6. 字符集转换: Unicode - Ansi

    字符集转换: Unicode - Ansi string UnicodeToAnsi ( const wstring& wstrSrc ) { /*!< 分配目标空间, 一个16位Uni ...

  7. MultiByteToWideChar和WideCharToMultiByte用法详解

    今天写ini文件的时候发现的问题: TCHAR temp[]; //strcpy_s(temp, request.newVersion); MultiByteToWideChar(CP_ACP, , ...

  8. 转:Unicode字符集和多字节字符集关系

    原文地址: http://my.oschina.net/alphajay/blog/5691 unicode.ucs-2.ucs-4.utf-16.utf-32.utf-8 http://stallm ...

  9. Unicode字符集下CString与char *相互转换

    经常遇到CString转换char*时只返回第一个字符.原因是因为在Unicode字符集下CString会以Unicode的形式来保存数据,强制类型转换只会返回第一个字符.所以直接转换在基于MBCS的 ...

随机推荐

  1. java命令行参数

    命令行参数就是main方法里面的参数String[] args他就是一个数组,args只是数据类型的一个名称,就是一个数组的变量,名称无所谓,类型没变就行了.这个就是程序的入口点.如图7.4所示: 图 ...

  2. NBUT 1028 该减肥了(简单递推)

    [1028] 该减肥了 时间限制: 1000 ms 内存限制: 65535 K 问题描述 由于长期缺乏运动,Teacher Xuan发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥.Teach ...

  3. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [3] 首页 APP 接口开发方案 ② 读取缓存方式

    以静态缓存为例. 修改 file.php line:11 去掉 path 参数(方便),加上缓存时间参数: public function cacheData($k,$v = '',$cacheTim ...

  4. 【翻译】Kinect v2程序设计(C++) BodyIndex篇

    通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法.   这 ...

  5. ICON文件保存

    这两天想做一下windows系统下图标的修改,让程序有更新的时候能够更新图标的外观,达到提醒的作用,360,QQ经常采用这种方式进行更新的提示,也有采用弹框的方式来提示,用新版QVOD的同事可能见到过 ...

  6. 《黑客大曝光》实践部分——sql注入(7/8)

    SQL注入实践 由于<黑客大曝光>中涉及到形形色色的攻击方式,从软件到硬件,甚至还有物理锁的开锁教程,当中的很多教程很有趣,但是我没有相关的环境,实践起来不好操作,比如说,查点扫描我还可以 ...

  7. Java高级之线程同步

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于实现多线程的意义,"从业四年看并发"一文已经讲述,而本篇主要讲一下常用的设计 ...

  8. Linux下开发Windows平台运行的程序 - MinGW

    开源不乏神人,于是有了MinGW(Minimalist GNU for Windows),又称mingw32,是将GCC编译器和GNU Binutils一直到Win32平台下,包含一系列头文件.库和可 ...

  9. 15 款最好的 C/C++ 编译器和集成开发环境

    我们有很多编程语言来进行 web 开发,比如 Java,.Net,PHP,Ruby,Perl,Python 等等.今天我们主要讨论的是两大古老而又流行的语言: C 和 C++ ,它们有着许多卓越的特性 ...

  10. Silverlight页面通过继承扩展实现

    在Silverlight中有些项目对UserControl重新做了封装基类,如PageBase,要求项目中每个页面都要从PageBase派生,但是过程比较坎坷,本文针对这个功能点的实现以及实现过程中遇 ...