平台调用错误试图读取或写入受保护的内存
Platform Invoke error attempted to read or write protected memory
当我尝试使用平台调用示例并尝试更改字符串的大小写时出现错误。
这是我目前得到的:
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's documentation 表示 CharLowerBuffA
是该方法的 ANSI 变体,但您指定的是 Unicode。
尝试使用 ANSI - 通过指定 CharSet = CharSet.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 提到的那样。
相同的结果意味着,它仍然给出相同的错误,尝试在调用该方法时使用 string.ToCharArray(),将签名更改为 char 数组。
当我尝试使用平台调用示例并尝试更改字符串的大小写时出现错误。
这是我目前得到的:
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's documentation 表示 CharLowerBuffA
是该方法的 ANSI 变体,但您指定的是 Unicode。
尝试使用 ANSI - 通过指定 CharSet = CharSet.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 提到的那样。
相同的结果意味着,它仍然给出相同的错误,尝试在调用该方法时使用 string.ToCharArray(),将签名更改为 char 数组。