StringBuilder和Char[]在平台调用中的差异如下:
- StringBuilder不能接收以2个'\0'结尾的多个字符串集合,只能保留第一个字符串,剩余的字符串被舍弃。而Char[]可以获取完整的多个字符串集合。总之,如果接收的字符串中间包含'\0',则StringBuilder只能获取第一个'\0'前的字符,后面的字符被舍弃。
- 将结果转换成String类型时,StringBuilder的转换方式为使用自身的成员函数:public overridestring ToString()。Char[]的转换方法必须使用String的构造函数:public String(Char[] value, int startIndex, int length),而不是简单的:public String(Char[] value),以消除垃圾字符。
示例一:
BOOL GetUserPreferredUILanguages(
__in DWORD dwFlags,
__out PULONG pulNumLanguages,
__out_opt PZZWSTR pwszLanguagesBuffer,
__inout PULONG pcchLanguagesBuffer
);
由于pwszLanguagesBuffer中接收的是以2个'\0'结尾的多个字符串集合,因此应该使用Char[]类型。
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern Boolean GetUserPreferredUILanguages(
UInt32 dwFlags,
out Int32 pulNumLanguages,
[Out] Char[] pwszLanguagesBuffer,
ref Int32 pcchLanguagesBuffer
);
/// <summary>
/// 获取用户偏好的界面语言
/// </summary>
/// <param name="dwFlags">
/// MUI_LANGUAGE_ID = 4:语言ID
/// MUI_LANGUAGE_NAME = 8:语言名称
/// </param>
/// <returns>偏好的语言集合</returns>
public static String[] GetUserPreferredUILanguages(UInt32 dwFlags = MUI_LANGUAGE_NAME)
{
// 计算需要的空间大小
Int32 NumLanguages;
Int32 cchLanguagesBuffer = 0;
if (GetUserPreferredUILanguages(dwFlags, out NumLanguages, null, ref cchLanguagesBuffer))
{
if (NumLanguages > 0)
{ // 获取实际的数据
Char[] LanguagesBuffer = new Char[cchLanguagesBuffer];
GetUserPreferredUILanguages(dwFlags, out NumLanguages, LanguagesBuffer, ref cchLanguagesBuffer);
// 提取所有的语言
return new String(LanguagesBuffer).TrimEnd('\0').Split('\0');
}
}
return null;
}
示例二:
int GetUserDefaultLocaleName(
__out LPWSTR lpLocaleName,
__in int cchLocaleName
);
使用StringBuilder:
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern Int32 GetUserDefaultLocaleName([Out] StringBuilder lpLocaleName, Int32 cchLocaleName);
public const Int32 LOCALE_NAME_MAX_LENGTH = 85;
/// <summary>
/// 获取用户缺省本地语言名称
/// </summary>
/// <returns>本地语言名称</returns>
public static String GetUserDefaultLocaleName()
{
StringBuilder lpLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
if (GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH) > 0)
{
return lpLocaleName.ToString();
}
return null;
}
使用Char[],转换成String类型时需要消除垃圾字符:
[DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
private static extern Int32 GetUserDefaultLocaleName([Out] Char[] lpLocaleName, Int32 cchLocaleName);
public const Int32 LOCALE_NAME_MAX_LENGTH = 85;
/// <summary>
/// 获取用户缺省本地语言名称
/// </summary>
/// <returns>本地语言名称</returns>
public static String GetUserDefaultLocaleName()
{
Char[] lpLocaleName = new Char[LOCALE_NAME_MAX_LENGTH];
Int32 Length = GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH);
if (Length > 0)
{
return new String(lpLocaleName, 0, Length - 1);
}
return null;
}
本文编辑:插秧机 www.cgjfg.com 。