访问冲突异常通过第二次访问访问C DLL函数

时间:2015-12-17 11:46:25

标签: c# access-violation

我有以下代码:

Generic.cs

class Generic
{
    [DllImport("Generic.dll", EntryPoint = "Consult", CallingConvention = CallingConvention.Winapi)]
    public static extern String Consulta(int NumeroSecao);       
}

main.cs

private void Button_Click(object sender, RoutedEventArgs e)
{
   Random rnd = new Random();
   int random = rnd.Next(9999);
   Stopwatch sw = new Stopwatch();

   sw.Start();
   String teste = Generic.Consult(random);
   sw.Stop();

   TextoLog = "Tempo de Execução:"+sw.Elapsed.Milliseconds+"ms | ConsultarSAT";
   tempoEntreComandos();
}

DLL的代码是:

GENERIC.C

__declspec(dllexport) char* __stdcall Consult(int numeroSessao)
{
    memset(pcReturn,0,sizeof(char) * BUFFER_SIZE);
    HANDLE fileHandle;
    Communicate(&fileHandle, pcReturn,10);
    return pcReturn;
}

当我第二次调用函数Consult时,收到的消息是Access Violation Exception,就会出现问题。这里发生了什么?

2 个答案:

答案 0 :(得分:1)

解决方案是函数声明中返回的类型,而不是使用String必须使用IntPtr

答案 1 :(得分:1)

必须封送本地字符串才能在c#中使用。您的本机代码实际上并不返回字符串对象,而是返回char指针。你必须告诉编组人员返回类型是什么,如下:

[DllImport("Generic.dll", EntryPoint = "Consult", CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern String Consulta(int NumeroSecao);

将LPStr用于ASCII字符串,将LPWStr用于unicode。

实际上,当编组器在转换后立即释放本机字符串时,手动编组字符串可能是更好的主意。声明将是

[DllImport("Generic.dll", EntryPoint = "Consult", CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr Consulta(int NumeroSecao);

你手动将IntPtr编组为字符串,如此

String str = Marshal.PtrToStringAnsi(strptr); // for ansi
String str = Marshal.PtrToStringUni(strptr); // for unicode