下面这个函数为什么执行后会有问题,请问如何修正,当然要能实现这个功能啊?
Option Explicit Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As SECURITY_ATTRIBUTES, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long Private Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long Private Const SECTION_MAP_READ = &H4 Private Const FILE_MAP_READ = SECTION_MAP_READ Private Const SECTION_MAP_WRITE = &H2 Private Const FILE_MAP_WRITE = SECTION_MAP_WRITE Private Const PAGE_READWRITE = &H4 Dim m_hMemShare As Long Dim m_lpdata As Long Public Function CreateRestCallerSharedMemory() Dim hMemShare As Long Dim mAttr As SECURITY_ATTRIBUTES Dim data As Long Dim lpData As Long data = GetCurrentProcessId hMemShare = CreateFileMapping(&HFFFFFFFF, mAttr, PAGE_READWRITE, 0, LenB(data), "Test") If hMemShare = 0 Then Debug.Print "无法创建映像文件,创建映像数据失败!" Exit Function End If m_hMemShare = hMemShare lpData = MapViewOfFile(hMemShare, FILE_MAP_WRITE, 0, 0, LenB(data)) If lpData = 0 Then Debug.Print "映像文件到当前进程失败!" Exit Function End If m_lpdata = lpData CopyMemory ByVal lpData, data, LenB(data) End Function Private Sub Form_Load() CreateRestCallerSharedMemory //执行这句后就有问题了 CommonDialog1.ShowOpen End Sub
显示不完全最好能截个图就更能表达清楚了
重启了下就好了,因为用的虚拟机开发,长时间没有重启,可能刚好有些地方有内存泄漏吧