- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个分布式计算系统,该系统使用内存映射文件来通过 VBA 协调多台联网 PC 之间的工作。换句话说,我想让一组联网的计算机以协调的方式同时在一个可以轻松划分为不同部分的项目上工作。一台 PC 需要 13 个多小时才能完成该项目,这对我的客户来说不切实际。
我想将信息存储在内存映射文件中,这将有助于电脑以协调的方式处理项目(即没有重复工作,避免竞争问题等)。我尝试过使用其他类型的文件来完成此操作,但它会导致文件竞争问题或花费太长时间。因此,按照本论坛的建议,我正在尝试内存映射文件。
我对内存映射文件和分布式计算是全新的。必须在VBA中完成。据我所知,我必须指定该文件保存在我们网络上所有 PC 都可以访问的目录(此处为 Z 驱动器)上。我从不同的地方拼凑了一些代码:
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
Private 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
Private 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
Private 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
#If VBA7 Then
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (destination As Any, source As Any, _
ByVal length As Long)
#Else
Public Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (destination As Any, source As Any, _
ByVal length As Long)
#End If
Private Declare Function UnmapViewOfFile Lib "kernel32.dll" ( _
ByRef lpBaseAddress As Any) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Private hMMF As Long
Private pMemFile As Long
Sub IntoMemoryFileOutOfMemoryFile()
Dim sFile As String
Dim hFile As Long
sFile = "Z:\path\test1.txt"
hFile = CreateFile(sFile, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
hMMF = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, 1000000, "MyMemoryMappedFile")
pMemFile = MapViewOfFile(hMMF, FILE_MAP_WRITE, 0, 0, 0)
Dim buffer As String
buffer = "testing1"
CopyMemory pMemFile, ByVal buffer, 128
hMMF = CreateFileMapping(-1, 0, PAGE_READWRITE, 0, 1000000, "MyMemoryMappedFile")
pMemFile = MapViewOfFile(hMMF, FILE_MAP_WRITE, 0, 0, 0)
Dim buffer2 As String
buffer2 = String$(128, vbNullChar)
CopyMemory ByVal buffer2, pMemFile, 128
MsgBox buffer2 & " < - it worked?"
UnmapViewOfFile pMemFile
CloseHandle hMMF
End Sub
作为一个小例子,上面的代码尝试将字符串“testing1”放入文件 test1.txt 中,然后检索该字符串并将其存储在变量 buffer2 中,最后通过 msgbox 显示该字符串。 super 简单。但是,我不知道自己在做什么。
我们所有的电脑均为 64 位、Windows 7、Office/Excel 2013。
问题/疑问:
以下是我用来到达现在位置的一些链接:
https://msdn.microsoft.com/en-us/library/dd997372%28v=vs.110%29.aspx
http://vb.mvps.org/hardcore/html/sharedmemorythroughmemory-mappedfiles.htm
http://www.tushar-mehta.com/publish_train/xl_vba_cases/1016%20Office%202010%20VBA.shtml
有趣但不重要的信息:该“项目”是针对对冲基金客户的。我是一名从事基础定量研究的金融人士。我们每天分析 1250 多个数据字段中的 2000 多只股票,以发出宏观经济信号/预测来买卖股票、 future 和期权。
更新:如果我分别像这样更改两个 CopyMemory 行(按值传递 pMemFile):
CopyMemory ByVal pMemFile, buffer, 128
还有...
CopyMemory buffer2, ByVal pMemFile, 128
我在文件 test1.txt 中收到一堆疯狂的字符,并且 Excel 崩溃。
最佳答案
对于您的第一个问题(还没有对其进行太多探索),这与您尝试将缓冲区
传递到RtlMoveMemory的方式有关。它需要一个指针,但您向它传递了 BSTR 的副本。 。另请记住,VBA 中的字符串是 Unicode,因此您将得到交织的空字符。我通常使用字节数组或变体(它们将被编码为 CSTR)。
对于您的第二个问题,文件被锁定,因为您从未释放 hFile
的句柄。事实上,一旦将其传递给 CreateFileMappingA
,您就可以对 hFile
调用 CloseHandle
。
对于第三个问题,当您进行第二次调用时,您将覆盖句柄 hMMF
和指针 pMemFile
。理论上,它们应该返回与您在同一进程中相同的句柄和指针,但这并不能真正测试您是否获得了 map View 。
至于内存访问,我可能会建议将整个内容包装在一个类中,并将指针映射到比调用RtlMoveMemory
更有用的东西。我将您在问题中链接的代码改编为一个类,这应该使它更安全、更可靠、更方便使用(尽管它仍然需要通过错误检查来充实):
'Class MemoryMap
Option Explicit
Private Type SafeBound
cElements As Long
lLbound As Long
End Type
Private Type SafeArray
cDim As Integer
fFeature As Integer
cbElements As Long
cLocks As Long
pvData As Long
rgsabound As SafeBound
End Type
Private Const VT_BY_REF = &H4000&
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const OPEN_ALWAYS = &H4
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const PAGE_READWRITE = &H4
Private Const FILE_MAP_WRITE = &H2
Private Const FADF_FIXEDSIZE = &H10
Private cached As SafeArray
Private buffer() As Byte
Private hFileMap As Long
Private hMM As Long
Private mapped_file As String
Private bound As Long
Public Property Get FileName() As String
FileName = mapped_file
End Property
Public Property Get length() As Long
length = bound
End Property
Public Sub WriteData(inVal As String, offset As Long)
Dim temp() As Byte
temp = StrConv(inVal, vbFromUnicode)
Dim index As Integer
For index = 0 To UBound(temp)
buffer(index + offset) = temp(index)
Next index
End Sub
Public Function ReadData(offset, length) As String
Dim temp() As Byte
ReDim temp(length)
Dim index As Integer
For index = 0 To length - 1
temp(index) = buffer(index + offset)
Next index
ReadData = StrConv(temp, vbUnicode)
End Function
Public Function OpenMapView(file_path As String, size As Long, mapName As String) As Boolean
bound = size
mapped_file = file_path
Dim hFile As Long
hFile = CreateFile(file_path, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
hFileMap = CreateFileMapping(hFile, 0, PAGE_READWRITE, 0, size, mapName)
CloseHandle hFile
hMM = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0)
ReDim buffer(2)
'Cache the original SafeArray structure to allow re-mapping for garbage collection.
If Not ReadSafeArrayInfo(buffer, cached) Then
'Something's wrong, close our handles.
CloseOpenHandles
Exit Function
End If
Dim temp As SafeArray
If ReadSafeArrayInfo(buffer, temp) Then
temp.cbElements = 1
temp.rgsabound.cElements = size
temp.fFeature = temp.fFeature And FADF_FIXEDSIZE
temp.pvData = hMM
OpenMapView = SwapArrayInfo(buffer, temp)
End If
End Function
Private Sub Class_Terminate()
'Point the member array back to its own data for garbage collection.
If UBound(buffer) = 2 Then
SwapArrayInfo buffer, cached
End If
SwapArrayInfo buffer, cached
CloseOpenHandles
End Sub
Private Sub CloseOpenHandles()
If hMM > 0 Then UnmapViewOfFile hMM
If hFileMap > 0 Then CloseHandle hFileMap
End Sub
Private Function GetBaseAddress(vb_array As Variant) As Long
Dim vtype As Integer
'First 2 bytes are the VARENUM.
CopyMemory vtype, vb_array, 2
Dim lp As Long
'Get the data pointer.
CopyMemory lp, ByVal VarPtr(vb_array) + 8, 4
'Make sure the VARENUM is a pointer.
If (vtype And VT_BY_REF) <> 0 Then
'Dereference it for the actual data address.
CopyMemory lp, ByVal lp, 4
GetBaseAddress = lp
End If
End Function
Private Function ReadSafeArrayInfo(vb_array As Variant, com_array As SafeArray) As Boolean
If Not IsArray(vb_array) Then Exit Function
Dim lp As Long
lp = GetBaseAddress(vb_array)
If lp > 0 Then
With com_array
'Copy it over the passed structure
CopyMemory .cDim, ByVal lp, 16
'Currently doesn't support multi-dimensional arrays.
If .cDim = 1 Then
CopyMemory .rgsabound, ByVal lp + 16, LenB(.rgsabound)
ReadSafeArrayInfo = True
End If
End With
End If
End Function
Private Function SwapArrayInfo(vb_array As Variant, com_array As SafeArray) As Boolean
If Not IsArray(vb_array) Then Exit Function
Dim lp As Long
lp = GetBaseAddress(vb_array)
With com_array
'Overwrite the passed array with the SafeArray structure.
CopyMemory ByVal lp, .cDim, 16
If .cDim = 1 Then
CopyMemory ByVal lp + 16, .rgsabound, LenB(.rgsabound)
SwapArrayInfo = True
End If
End With
End Function
用法是这样的:
Private Sub MMTest()
Dim mm As MemoryMap
Set mm = New MemoryMap
If mm.OpenMapView("C:\Dev\test.txt", 1000, "TestMM") Then
mm.WriteData "testing1", 0
Debug.Print mm.ReadData(0, 8)
End If
Set mm = Nothing
End Sub
您还需要在某个地方进行以下声明:
Public 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
Public Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal length As Long)
Public Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Public Declare Function UnmapViewOfFile Lib "kernel32.dll" ( _
ByVal lpBaseAddress As Any) As Long
要记住的另一件事 - 由于您使用的是网络驱动器,因此您需要确保缓存机制不会干扰对文件的访问。具体来说,您需要确保所有客户端都关闭网络文件缓存。您可能还想确定性地刷新内存映射,而不是依赖操作系统(请参阅 FlushViewOfFile )。
关于vba - 如何使用VBA中的CopyMemory在内存映射文件中存储数据和从内存映射文件中取出数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30218821/
请看一下我的代码。 int main () { Program* allcommand = new Program; allcommand->addCommand("add", new
因此,当我遇到调试断言时,我正在编写代码。现在我很想知道为什么这段代码不起作用: for(Model::MeshMap::iterator it = obj1->GetMeshes().begin()
这是我上一个问题的延续 Group, Sum byType then get diff using Java streams . 按照建议,我应该作为单独的线程发布,而不是更新原始线程。 因此,通过我
我正在实现一些非常适合 map 的代码。但是,我要迭代的列表中有大量对象,所以我的问题是哪种方法是解决此问题的最佳方法: var stuff = $.map(listOfMyObjects, some
我正在尝试创建一个包含不同类的成员函数指针的映射。成员函数都具有相同的签名。为了做到这一点,我所有的类都继承了一个 Object 类,它只有默认构造函数、虚拟析构函数和一个虚拟 ToString()
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: how do you make a heterogeneous boost::map? 有可能在 C++ 中
我有一个 Mysql 查询,请检查以下内容: SELECT `tbl_classSubjects`.`classID` , `tbl_classSubjects`.`sectionID` , `tbl
抱歉,这可能是一个基本问题。 JNA直接映射和接口(interface)映射有什么区别? 我的解释是否正确: 直接映射 : 直接使用库对象(如 Java 中的静态 main) 接口(interface
在 Twitter's Scala school collections section ,它们显示了一个带有偏函数作为值的 Map: // timesTwo() was defined earlie
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
据我了解,从 scala stdlib 声明一个映射并没有将其专门用于原始类型。我要的不是付出装箱/拆箱的代价,而是同时拥有scala map 的接口(interface)。一个明显的选择是使用 tr
如何为这样的 JSON 响应创建对象映射,它只是一个整数数组: [ 565195, 565309, 565261, 565515, 565292, 565281, 566346, 5
是否可以为 DTO 对象创建映射然后查询它们 而不是域?如果不解释为什么? 如果我需要几个 dtos 怎么办? DTos 是只读的 ID 由 NH 自动生成 将来这些 dtos 将设置映射到链接的 d
我有一个返回的函数(常规代码) [words: "one two", row: 23, col: 45] 在 Scala 中,我将上面更改为 Scala Map,但随后我被迫将其声明为 Map[Str
我有一组与 Vanilla 磅蛋糕烘焙相关的数据(200 行),具有 27 个特征,如下所示。标签caketaste是衡量烤蛋糕的好坏程度,由 bad(0) 定义, neutral(1) , good
我有试图映射到新代码的遗留代码。 OLD_PERSON pid sid name age NEW_PERSON pid sid fid age RESOLVE_PERSON pid fid statu
我有一个表,其中一个字段可以指向其他 3 个表之一中的外键,具体取决于鉴别器值是什么(Project、TimeKeep 或 CostCenter。通常这是用子类实现的,我想知道我有什么 注意子类名称与
我有一个类型 [ST s (Int, [Int])] 的绑定(bind)我正在尝试申请runST使用映射到每个元素,如下所示: name :: [ST s (Int, [Int])] --Of Cou
在我正在进行的项目中,我有以下实体:分析师、客户 和承包商。每个都继承自基类 User。 public abstract class User { public virtual int Id
我想知道是否可以在 Vim 中创建一个映射(对于普通模式),允许用户在映射执行之前输入。 我想为我最常用的 grep 命令创建一个快捷方式的映射。我希望命令允许输入我正在搜索的内容,然后在输入时执行。
我是一名优秀的程序员,十分优秀!