WriteFile有时会返回错误代码6

时间:2015-10-28 06:53:42

标签: c# windows

以下是代码段:

CreateFile代码:

m_RunFileHandle = CreateFile(strRunFullPath,
                             EFileAccess.GenericWrite,
                             EFileShare.None,
                             IntPtr.Zero, 
                             ECreationDisposition.CreateAlways,
                             EFileAttributes.Write_Through | EFileAttributes.NoBuffering,
                             IntPtr.Zero);

WriteFile代码:

WriteFile(m_RunFileHandle, NewFramePixelData, nDataSize, out nBytesWritten, IntPtr.Zero);

查询:很少有WritrFile方法返回错误代码6。

2 个答案:

答案 0 :(得分:-1)

如果在FILE_FLAG_OVERLAPPED模式下使用该文件,则需要指向OVERLAPPED结构的指针,否则确保在不关闭句柄的情况下多次调用CreateFile。

在拨打电话" WriteFile"之前,检查是否多次调用CreateFile(不关闭先前创建的句柄)。在这种情况下,系统可以返回错误6。

答案 1 :(得分:-2)

溶剂是:

isEqualTo

修改为:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool WriteFile(
    IntPtr hFile,
    byte[] lpBuffer,
    UInt32 nNumberOfBytesToWrite,
    ref UInt32 lpNumberOfBytesWritten,
    ref DriverEntity.OVERLAPPED lpOverlapped);

使用:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool WriteFile(
    IntPtr hFile,
    byte[] lpBuffer,
    UInt32 nNumberOfBytesToWrite,
    ref UInt32 lpNumberOfBytesWritten,
    IntPtr lpOverlapped);