
GDAL C#封装对中文字符转换过程中存在问题。
C++封装一个Win32 DLL,采用Unicode字符集。使用标准头文件。
https://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx
class CodePageHelper
{
public:
CodePageHelper(void);
~CodePageHelper(void);
static wchar_t* ANSIToUnicode( const char* str );
static char* UnicodeToANSI( const wchar_t* str );
static wchar_t* UTF8ToUnicode( const char* str );
static char* UnicodeToUTF8( const wchar_t* str );
};
CodePageHelper.h
#include "StdAfx.h"
#include "CodePageHelper.h" CodePageHelper::CodePageHelper(void)
{
} CodePageHelper::~CodePageHelper(void)
{
} // ANSI to Unicode
wchar_t* CodePageHelper:: ANSIToUnicode( const char* str )
{
int unicodeLen = ::MultiByteToWideChar( CP_ACP,
,
str,
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,
,
str,
-,
(LPWSTR)pUnicode,
unicodeLen );
wchar_t* rt;
rt = ( wchar_t* )pUnicode;
//delete pUnicode; return rt;
}
// Unicode to ANSI
char* CodePageHelper:: UnicodeToANSI( const wchar_t* str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP,
,
str,
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_ACP,
,
str,
-,
pElementText,
iTextLen,
NULL,
NULL );
char* strText;
strText = pElementText;
//delete[] pElementText;
return strText;
}
// UTF-8 to Unicode
wchar_t* CodePageHelper::UTF8ToUnicode( const char* str )
{
int unicodeLen = ::MultiByteToWideChar( CP_UTF8,
,
str,
-,
NULL,
);
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+];
memset(pUnicode,,(unicodeLen+)*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8,
,
str,
-,
(LPWSTR)pUnicode,
unicodeLen );
wchar_t* rt;
rt = ( wchar_t* )pUnicode;
//delete pUnicode; return rt;
}
// Unicode to UTF-8 char* CodePageHelper::UnicodeToUTF8( const wchar_t* str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8,
,
str,
-,
NULL,
,
NULL,
NULL );
pElementText = new char[iTextLen + ];
memset( ( void* )pElementText, , sizeof( char ) * ( iTextLen + ) );
::WideCharToMultiByte( CP_UTF8,
,
str,
-,
pElementText,
iTextLen,
NULL,
NULL );
char* strText;
strText = pElementText;
//delete[] pElementText;
return strText;
}
CodePageHelper
#ifdef DEMIMP_EXPORTS
#define CPL_DLL __declspec(dllexport)
#else
#define CPL_DLL __declspec(dllimport)
#endif #ifndef CPL_DISABLE_STDCALL
# define CPL_STDCALL __stdcall
#endif extern "C"
{
HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath);
};
GDALRaster.h
HANDLE CPL_DLL WINAPI GetMetaData(LPWSTR filepath)
{
//char* file=CodePageHelper::UnicodeToUTF8((const wchar_t*)filepath); char* file1=CodePageHelper::UnicodeToANSI((const wchar_t*)filepath);
//const wchar_t* file2=filepath;
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
GDALDataset *pDSrc = (GDALDataset *)GDALOpen(file1, GA_ReadOnly);
if (pDSrc == NULL)
{
return ;
}
char** metadata=pDSrc->GetMetadata("");
return metadata;
}
C# P/Invoke调用:
[DllImport("GDALRaster.dll", EntryPoint = "GetMetaData", CharSet = CharSet.Unicode)]
private static extern IntPtr CSharp_GetMetadata([In, MarshalAs(UnmanagedType.LPWStr)]string filepath);
解析字符串:
public static string[] GetMetaData(string filePath)
{
IntPtr cPtr = CSharp_GetMetadata(filePath);
if (cPtr == IntPtr.Zero) throw new Exception("打开失败"); IntPtr objPtr;
int count = ;
if (cPtr != IntPtr.Zero)
{
while (Marshal.ReadIntPtr(cPtr, count * IntPtr.Size) != IntPtr.Zero)
++count;
}
string[] ret = new string[count];
if (count > )
{
for (int cx = ; cx < count; cx++)
{
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
ret[cx] = (objPtr == IntPtr.Zero) ? null : System.Runtime.InteropServices.Marshal.PtrToStringAnsi(objPtr);
}
}
return ret;
//double[] temp = new double[xsize * ysize];
//Marshal.Copy(pData, temp, 0, xsize * ysize);
//FreeData(pData);
//return temp; }