2014-06-17 37 views
-4

我不是pInvokes的粉絲,但我不認爲我有這個任何其他選擇,我需要調整vb.net中的內存映射文件的大小。不管怎麼說,我從pinvoke.net此的PInvoke簽名:pInvoke CreateFileMapping需要什麼?

<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但我在<的MarshalAs得到一個錯誤啄。我不知道有什麼缺失或從哪裏來。難道你不恨這些引用怎麼沒有提到過任何依賴關係?就像我應該自發地知道哪個庫或框架分支它。通常我可以通過查看MSDN中的左側樹來找到,但是這次我無法找到對該命令的任何直接引用。

+1

「我得到一個錯誤」==沒有幫助。 – ken2k

+3

當人們說「我得到一個錯誤」,但拒絕提供他們可以得到的確切的錯誤,他們可以看到,但沒有人能夠看到的時候,難道你不是不喜歡它嗎?當人們期望你閱讀他們的想法時,你不要只是討厭它。只是在說'。 –

+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是默認值。您可以省略此屬性。