当我尝试使用平台调用示例(其中我尝试更改字符串的小写和大写)时,出现错误。
这是到目前为止我得到的:
class Program
{
[DllImport("User32.dll", EntryPoint = "CharLowerBuffA",
ExactSpelling = false,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern string CharLower(string lpsz);
[DllImport("User32.dll",
EntryPoint = "CharUpperBuffA",
ExactSpelling = false,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern string CharUpper(string lpsz);
static void Main(string[] args)
{
string l = "teSarf";
string ChangeToLower = CharLower(l.ToLower());
string ChangeToUpper = CharUpper(l.ToUpper());
Console.WriteLine("{0}", ChangeToLower);
Console.ReadLine();
}
}
我不确定我在哪里出错,但是我认为这与EntryPoint有关。
我必须使用Unicode,CharLowerBuffW也不起作用。
我怎样才能解决这个问题?
Microsoft的文档表明这CharLowerBuffA
是该方法的ANSI变体,但是您正在指定Unicode。
通过指定CharSet = CharSet.Ansi
-尝试使用ANSI-或如果需要Unicode,请使用CharLowerBuffW
和CharUpperBuffW
。
同样,该方法采用两个参数。您没有第二个。所以试试这个:
[DllImport("User32.dll", EntryPoint = "CharLowerBuffW",
ExactSpelling = false,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern string CharLower(string lpsz, int cchLength);
[DllImport("User32.dll",
EntryPoint = "CharUpperBuffW",
ExactSpelling = false,
CharSet = CharSet.Unicode,
SetLastError = true
)]
public static extern string CharUpper(string lpsz, int cchLength);
并这样称呼它:
string ChangeToLower = CharLower(l, l.Length);
如果仍然无法解决问题,请尝试使用字符数组,例如NatarajC。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句