C# 尝试读取或写入受保护内存时发生平台调用错误
当我尝试使用平台调用示例时,我得到了一个错误,在这个示例中,我试图更改字符串的大小写 以下是我到目前为止得到的信息:C# 尝试读取或写入受保护内存时发生平台调用错误,c#,asp.net,.net,pinvoke,C#,Asp.net,.net,Pinvoke,当我尝试使用平台调用示例时,我得到了一个错误,在这个示例中,我试图更改字符串的大小写 以下是我到目前为止得到的信息: class Program { [DllImport("User32.dll", EntryPoint = "CharLowerBuffA", ExactSpelling = false, CharSet = CharSet.Unicode, SetLastError = true )] public static ext
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();
}
}
我不确定我的错误在哪里,但我认为这与入口点有关
我必须使用Unicode,而charlowerbufffw也不起作用
如何修复此问题?表示CharLowerBuffA
是该方法的ANSI变体,但您指定的是Unicode
请尝试使用ANSI-通过指定CharSet=CharSet.ANSI
-或者如果需要Unicode,请使用charlowerbufffw
和charupperbufw
同样,该方法采用两个参数。你没有第二个。所以试试这个:
[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提到的那样。相同的结果意味着,它仍然给出相同的错误,尝试使用string.ToCharArray()调用该方法时,将签名更改为char array CharUpperBuffW得到了相同的结果,我需要unicodeI也注意到您缺少长度参数。我已经更新了我的答案。