Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试读取或写入受保护内存时发生平台调用错误_C#_Asp.net_.net_Pinvoke - Fatal编程技术网

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也注意到您缺少长度参数。我已经更新了我的答案。