刚刚创建它的进程找不到内存映射文件

时间:2014-06-30 14:19:35

标签: c# winapi pinvoke

我的目标是64位Windows 8上的.Net Framework 3.5(4.0+可能不是这个项目的选项)。我原本以为问题是ProcessB无法找到ProcessA制作的内存映射文件,但即使句柄很好并且可用于写入和读取文件,ProcessA也无法找到它刚刚创建的文件。在代码示例中,我在调用CreateFileMapping后立即调用OpenFileMapping,但即使将数据写入文件后它也会失败。我在CreateFileMapping中使用了文件映射属性,使其变长并传入0,使其成为SECURITY_ATTRIBUTES结构并传入IntPtr.Zero以获取lpSecurityDescriptor,所有这些都没有任何乐趣。我也尝试过FileMapAccess ReadWrite和AllAccess。我也尝试使memoryfilename不是常量。我没有看到为什么无法找到映射文件,即使是创建它的进程也是如此。以下是所涉及代码的基本内容:

private IntPtr hHandle;
private IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public const Int64 FILESIZE = 1024 * 1024;
private const string memoryfilename = "myfilename";

//CreateFileMapping
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFileMapping(
IntPtr hFile,
IntPtr lpFileMappingAttributes,
FileMapProtection flProtect,
uint dwMaximumSizeHigh,
uint dwMaximumSizeLow,
[MarshalAs(UnmanagedType.LPStr)] string lpName);

//OpenFileMapping
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenFileMapping(
FileMapAccess dwDesiredAccess,
Int32 bInheritHandle, 
[MarshalAs(UnmanagedType.LPStr)] string lpName);

[Flags]
public enum FileMapAccess : uint
{
  FileMapCopy = 0x0001,
  FileMapWrite = 0x0002,
  FileMapRead = 0x0004,
  FileMapReadWrite = 0x0006,
  FileMapAllAccess = 0x001f,
  FileMapExecute = 0x0020,
}

[Flags]
enum FileMapProtection : uint
{
  PageReadonly = 0x02,
  PageReadWrite = 0x04,
  PageWriteCopy = 0x08,
  PageExecuteRead = 0x20,
  PageExecuteReadWrite = 0x40,
  SectionCommit = 0x8000000,
  SectionImage = 0x1000000,
  SectionNoCache = 0x10000000,
  SectionReserve = 0x4000000,
}

//hHandle becomes non-zero
hHandle= CreateFileMapping(
INVALID_HANDLE_VALUE, IntPtr.Zero, FileMapProtection.PageReadWrite,
(uint)0, (uint)FILESIZE, memoryfilename);

//hHandle2 stays zero
IntPtr hHandle2 = OpenFileMapping(FileMapAccess.FileMapWrite, 0, memoryfilename);

//myerror is 2 ERROR_FILE_NOT_FOUND
uint myerror = GetLastError();

//this works and I can read/write the file through UnmanagedMemoryStream
pBuffer = MapViewOfFile(hHandle, FileMapAccess.FileMapWrite, 0, 0, (uint)FILESIZE);

1 个答案:

答案 0 :(得分:1)

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFileMapping(
    IntPtr hFile,
    IntPtr lpFileMappingAttributes,
    FileMapProtection flProtect,
    uint dwMaximumSizeHigh,
    uint dwMaximumSizeLow,
    [MarshalAs(UnmanagedType.LPStr)] 
    string lpName
);

DllImport属性使用CharSet.Auto,这意味着将使用该函数的宽字符版本。然后传递ANSI编码的字符串,因为您使用了UnmanagedType.LPStr。正如汉斯所说,这意味着当系统解释您提供的ANSI编码文本时,您的名称将被破坏,就像它是UTF-16编码一样。

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenFileMapping(
    FileMapAccess dwDesiredAccess,
    Int32 bInheritHandle, 
    [MarshalAs(UnmanagedType.LPStr)] 
    string lpName
);

这次省略CharSet.Auto,这次默认为``CharSet.Ansi is used. And so the ANSI version of the function is used, which matches UnmanagedType.LPStr`。因此传递了预期的名称。

所有这些都解释了系统报告ERROR_FILE_NOT_FOUND

的原因

修复p / invoke声明,一切都会好的。

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr CreateFileMapping(
    IntPtr hFile,
    IntPtr lpFileMappingAttributes,
    FileMapProtection flProtect,
    uint dwMaximumSizeHigh,
    uint dwMaximumSizeLow,
    string lpName
);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr OpenFileMapping(
    FileMapAccess dwDesiredAccess,
    bool bInheritHandle, 
    string lpName
);