来自USB AccessViolationException的C#.NET ReadFile未处理

时间:2015-02-27 21:39:04

标签: c# .net usb dllimport readfile

我在调用ReadFile API时收到AccessViolationException。我已经查看了StackOverflow上处理类似问题的所有条目,并且没有成功解读出错的地方。

以下是相关的相关代码:

调用

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile([MarshalAs(UnmanagedType.LPTStr)] string filename, UInt32 desiredaccess, UInt32 sharemode, IntPtr securityattributes, UInt32 creationdisposition, UInt32 flagsandattributes, IntPtr templatefile);

[DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadFile(IntPtr hFile, out byte[] lpbuffer, UInt32 nNumberofBytesToRead, out UInt32 lpNumberofBytesRead, IntPtr lpOverlapped);

[DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Ansi, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, UInt32 nNumberOfBytesToWrite, out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);

创建文件功能调用

private IntPtr OpenPort(string port)
    {
        IntPtr printerhandle = IntPtr.Zero;

        printerhandle = CreateFile(port, 
                            (UInt32)(FileAccess.GENERIC_READ | FileAccess.GENERIC_WRITE), 
                            (UInt32)(FileShare.FILE_SHARE_READ | FileShare.FILE_SHARE_WRITE), 
                            IntPtr.Zero, 
                            (UInt32)FileMode.OPEN_EXISTING, 
                            (UInt32)(FileAttribute.FILE_ATTRIBUTE_NORMAL), 
                            IntPtr.Zero);

        return (printerhandle);
    }

READFILE FUNCTION CALL

private bool WriteToPort(IntPtr printer, ref byte[] data)
    {
        bool success = true;
        byte[] data2 = new byte[64];
        byte[] dataread = new byte[64];
        int index = 0;
        int length = 64;
        uint written = 0;
        uint read = 0;

        while ((index + length) <= data.Length)
        {
            Array.Copy(data, index, data2, 0, length);
            success &= WriteFile(printer, data2, (uint)length, out written, IntPtr.Zero);
            index += 64;
        }

        if ((index < data.Length) &&
            ((index + length) > data.Length))
        {
            length = data.Length - index;
            Array.Copy(data, index, data2, 0, length);
            success &= WriteFile(printer, data2, (uint)length, out written, IntPtr.Zero);
        }

        success &= ReadFile(printer, out dataread, 64, out read, IntPtr.Zero);

        return success;
    }

执行success &= ReadFile(printer, out dataread, 64, out read, IntPtr.Zero);行时会发生异常。

以下是VS2013报告的相关堆栈跟踪:

StackTrace:
   at System.StubHelpers.MngdNativeArrayMarshaler.ClearNative(IntPtr pMarshalState, IntPtr pNativeHome, Int32 cElements)
   at APP_NET.Main.ReadFile(IntPtr hFile, Byte[]& lpbuffer, UInt32 nNumberofBytesToRead, UInt32& lpNumberofBytesRead, IntPtr lpOverlapped)
   at APP_NET.Main.WriteToPort(IntPtr printer, Byte[]& data) in Main.cs:line 770

对可能发生的事情的任何想法?

1 个答案:

答案 0 :(得分:2)

因为ReadFile函数声明错误。从out参数中删除lpbuffer

导入ReadFile:

[DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Ansi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReadFile(IntPtr hFile, byte[] lpbuffer, UInt32 nNumberofBytesToRead, out UInt32 lpNumberofBytesRead, IntPtr lpOverlapped);

阅读文件:

success &= ReadFile(printer, dataread, 64, out read, IntPtr.Zero);
相关问题