none
C# 调用CreateFile报错 RRS feed

  • 问题

  • 代码如下:

            定义部分:

    public const int GENERIC_ALL = 0x10000000;
            public const int GENERIC_EXECUTE = 0x20000000;
            public const uint GENERIC_READ = 0x80000000;
            public const int GENERIC_WRITE = 0x40000000;
            public const int FILE_SHARE_READ = 1;
            public const int FILE_SHARE_WRITE = 2;
            public const int OPEN_EXISTING = 3;
            public IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);

            [StructLayout(LayoutKind.Sequential)]
            public struct SECURITY_ATTRIBUTES
            {
                public int nLength;
                public IntPtr lpSecurityDescriptor;
                public int bInheritHandle;
            }

            [DllImport("coredll", SetLastError = true)]
            public static extern IntPtr CreateFile
                (
                String lpFileName,
                UInt32 dwDesiredAccess,
                UInt32 dwShareMode,
                IntPtr lpSecurityAttributes,
                UInt32 dwCreationDisposition,
                UInt32 dwFlagsAndAttributes,
                IntPtr hTemplateFile
                );
            public struct OVERLAPPED
            {
                public UIntPtr Internal;
                public UIntPtr InternalHigh;
                public uint Offset;
                public uint OffsetHigh;
                public IntPtr EventHandle;
            }

            [DllImport("kernel32.dll", EntryPoint = "DeviceIoControl")]
            public static extern int DeviceIoControl(
                int hDevice,
                int dwIoControlCode,
                ref int lpInBuffer,
                int nInBufferSize,
                ref int lpOutBuffer,
                int nOutBufferSize,
                ref int lpBytesReturned,
                ref OVERLAPPED lpOverlapped
            );

    调用部分:

     IntPtr hDevice;         // 设备句柄
                bool bResult;           // DeviceIoControl的返回结果\\\\.\\C:

                hDevice = CreateFile
                    (
                    "\\\\.\\C:",           // 文件名
              GENERIC_READ,                          // 软驱需要读盘
              FILE_SHARE_READ | FILE_SHARE_WRITE,    // 共享方式
                    IntPtr.Zero,                                  // 默认的安全描述符
              OPEN_EXISTING,                         // 创建方式
              0,                                     // 不需设置文件属性
                    IntPtr.Zero
                    );

    运行到CreateFile时报错:无法加载 DLL“coredll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。

    请问是怎么回事?谢谢!


    guiwenyang

    2012年4月28日 7:35

答案

  • For windows:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern SafeFileHandle CreateFile(
        string lpFileName,
        [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
        [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
        IntPtr lpSecurityAttributes,
        [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
        [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
        IntPtr hTemplateFile);

    你的那个是 coredll 版本是 for smart devices 的。


    学习学习....

    2012年4月28日 8:45

全部回复

  • For windows:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern SafeFileHandle CreateFile(
        string lpFileName,
        [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
        [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
        IntPtr lpSecurityAttributes,
        [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
        [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
        IntPtr hTemplateFile);

    你的那个是 coredll 版本是 for smart devices 的。


    学习学习....

    2012年4月28日 8:45
  • 非常感谢!

    guiwenyang

    2012年5月9日 2:31