pInvoke CreateFileMapping需要什么?

时间:2014-06-17 11:26:02

标签: vb.net pinvoke

我不是pInvokes的粉丝,但我不认为我对此有任何其他选择,我需要调整vb.net中的内存映射文件。无论如何,我从pinvoke.net获得了这个pInvoke签名:

<DllImport("kernel32.dll", SetLastError:= true, CharSet:= CharSet.Auto)> _
public Function CreateFileMapping( _
 hFile as intptr, _
 lpFileMappingAttributes As IntPtr , _
 flProtect As FileMapProtection, _
 dwMaximumSizeHigh As UInteger, _
 dwMaximumSizeLow As UInteger, _
 lpName As <MarshalAs(UnmanagedType.LPTStr)> string) as IntPtr
End Function

Public Enum FileMapProtection As UInteger
    PageReadonly = &H2
    PageReadWrite = &H4
    PageWriteCopy = &H8
    PageExecuteRead = &H20
    PageExecuteReadWrite = &H40
    SectionCommit = &H8000000
    SectionImage = &H1000000
    SectionNoCache = &H10000000
    SectionReserve = &H4000000
End Enum

我已导入System.Runtime.InteropServices,但我在&lt; MarshalAs thingy。我不知道什么是丢失的或它来自哪里。难道你不是很讨厌这些引用怎么都没有提到任何依赖关系?就像我应该自发地知道哪个库或框架分支它。通常我可以通过查看MSDN中的左手树来查找,但这次我找不到对该命令的任何直接引用。

1 个答案:

答案 0 :(得分:1)

MarshalAs属性必须出现在参数之前而不是中间。

<DllImport("kernel32.dll", SetLastError:= true, CharSet:= CharSet.Auto)> _
public Function CreateFileMapping( _
    hFile as intptr, _
    lpFileMappingAttributes As IntPtr , _
    flProtect As FileMapProtection, _
    dwMaximumSizeHigh As UInteger, _
    dwMaximumSizeLow As UInteger, _
    <MarshalAs(UnmanagedType.LPTStr)> lpName As string) as IntPtr
End Function

pinvoke.net上的声明是错误的。他们经常这样。对于它的价值,该属性是没有意义的,因为UnmanagedType.LPTStr是默认值。您可以省略此属性。