不确定我应该在哪里这样做,但是我发布这个问题是为了节省一些人的时间。我已经找到了答案。如果这不是最好的地方,请告诉我在哪里可以做到这一点。
这是在UnmapViewOfFile行之后创建错误487:
Option Explicit
Private Const PAGE_READWRITE As Long = &H4
Private Const FILE_MAP_WRITE As Long = &H2
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_ALWAYS = 4
Private Const FILE_ATTRIBUTE_NORMAL = &H80
'http://www.cadsharp.com/docs/Win32API_PtrSafe.txt
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function CreateFileMapping Lib "kernel32.dll" Alias "CreateFileMappingA" ( _
ByVal hFile As Long, _
ByVal lpFileMappigAttributes As Long, _
ByVal flProtect As Long, _
ByVal dwMaximumSizeHigh As Long, _
ByVal dwMaximumSizeLow As Long, _
ByVal lpName As String) As Long
Declare Function MapViewOfFile Lib "kernel32.dll" ( _
ByVal hFileMappingObject As Long, _
ByVal dwDesiredAccess As Long, _
ByVal dwFileOffsetHigh As Long, _
ByVal dwFileOffsetLow As Long, _
ByVal dwNumberOfBytesToMap As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Declare Function UnmapViewOfFile Lib "kernel32.dll" ( _
ByRef lpBaseAddress As Any) As Long
Public hMMF As Long
Public pMemFile As Long
Sub IntoMemoryFileOutOfMemoryFile()
Err.Clear
Dim sFile As String
Dim hFile As Long
Dim bBytes As Variant
sFile = "C:\test1.txt"
Debug.Print Err.LastDllError
'https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
hFile = CreateFile(sFile, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
Debug.Print Err.LastDllError
'https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx
hMMF = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, 1000000, "MyMemoryMappedFilex")
Debug.Print Err.LastDllError
'https://msdn.microsoft.com/en-us/library/windows/desktop/aa366761%28v=vs.85%29.aspx
pMemFile = MapViewOfFile(hMMF, FILE_MAP_WRITE, 0, 0, 1000000)
Debug.Print Err.LastDllError
UnmapViewOfFile pMemFile '< - produced error 487
Debug.Print Err.LastDllError
CloseHandle hMMF
Debug.Print Err.LastDllError
CloseHandle hFile
Debug.Print Err.LastDllError
End Sub
答案 0 :(得分:0)
更改此行:
Declare Function UnmapViewOfFile Lib "kernel32.dll" ( _
ByRef lpBaseAddress As Any) As Long
为...
Declare Function UnmapViewOfFile Lib "kernel32.dll" ( _
ByVal lpBaseAddress As Any) As Long
刚刚将ByRef更改为ByVal。