Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

memory mapping attempt in VBA

718 views
Skip to first unread message

res...@my-dejanews.com

unread,
Aug 12, 1998, 3:00:00 AM8/12/98
to
Hi everyone,

I'm trying to figure out why the following test code won't let me create a
memory mapped file from VBA (in Excel). Stepping through it, the handle
remains 0, and logically nothing is displayed on the excel worksheet either.
I've included everything I've typed into the module. I'm running this in
Windows 95 under Excel 95 and Excel 97, neither version works. There are
several constant values like PAGE_READWRITE and FILE_MAP_WRITE that I was
able to look through include files and figure out. Are they correct? I
declared a SECURITY ATTRIBUTES type since I wasn't sure if passing 0 was the
same as passing a SECURITY_ATTRIBUTES variable uninitialized. It won't let
me pass a NULL.


Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

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

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

Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any,
lpvSource As Any, _
ByVal cbCopy As Long)

Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Const PAGE_READWRITE = &o4
Const FILE_MAP_WRITE = &h2

Dim currStr As String, pointCount As Long

Dim memhan As Variant, dapoint As Long, memStr As String, strLen As Long

Dim dummy As SECURITY_ATTRIBUTES

Sub memtest()

pointCount = 0

memhan = CreateFileMapping(&hffff, dummy, PAGE_READWRITE, 0, 65535,
"SharedMap")

dapoint = MapViewOfFile(memhan, FILE_MAP_WRITE, 0, 0, 0)

currStr = "test"
strLen = Len(currStr)
CopyMemory ByVal (dapoint), strLen, 1
CopyMemory ByVal (dapoint + 1), currStr, strLen
currStr = ""
CopyMemory currStr, ByVal (dapoint + 1), strLen

Worksheets("Sheet1").Cells(1, 1).Value = currStr
UnmapViewOfFile dapoint
CloseHandle memhan

End Sub

_____________________________________________
Thanks for any help anyone can provide.


Richard

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Richard Mason

unread,
Aug 14, 1998, 3:00:00 AM8/14/98
to
In article <6qrp29$h7c$1...@nnrp1.dejanews.com>, res...@my-dejanews.com
writes

>Hi everyone,
>
> I'm trying to figure out why the following test code won't let me create a
>memory mapped file from VBA (in Excel). Stepping through it, the handle
>[snip]

Perhaps if you explained more about what you're _trying_ to do...

It looks like your trying to copy data to/from the system paging file,
if so I guess your on your own.

--
Richard Mason

res...@my-dejanews.com

unread,
Aug 18, 1998, 3:00:00 AM8/18/98
to
Actually, I'm just trying to see if I can get VBA in Excel to handle shared
memory with other apps, like a C DLL for example. I've isolated the problem
to the fact that my call to API function CreateFileMapping() returns a null
handle. I've noticed other people have postings describing the same problem.
Does anyone have a solution?

Richard

In article <hh3ebTAC...@dial.pipex.com>,

-----== Posted via Deja News, The Leader in Internet Discussion ==-----

Richard Mason

unread,
Aug 20, 1998, 3:00:00 AM8/20/98
to
In article <6rapgg$fjh$1...@nnrp1.dejanews.com>, res...@my-dejanews.com
writes

>Actually, I'm just trying to see if I can get VBA in Excel to handle shared
>memory with other apps, like a C DLL for example. I've isolated the problem
>to the fact that my call to API function CreateFileMapping() returns a null
>handle. I've noticed other people have postings describing the same problem.
>Does anyone have a solution?

Had another look at this (and if your still interested :-)) I think that
your code has a number of problems. The following just copies a string
to mapped memory and retrieves it within the same process. I haven't
quite worked out how to address mapped memory using other than the start
address m_mapaddr.

'At module level
Dim m_maphand As Long
Dim m_mapaddr As Long

'At Procedure level
Dim currStr As String
Dim tempByteArray1() As Byte
Dim tempByteArray2() As Byte
Dim NumBytes As Long

'Create file mapping object
m_maphand = CreateFileMapping(&HFFFFFFFF, 0, PAGE_READWRITE, 0, 1000,
vbNullString)
If m_maphand = 0 Then
MsgBox "CreateFileMapping Failed"
Call CleanUp
Exit Sub
End If

'Map to memory
m_mapaddr = MapViewOfFile(m_maphand, FILE_MAP_WRITE, 0, 0, 0)
If m_mapaddr = 0 Then
MsgBox "MapViewOfFile Failed"
Call CleanUp
Exit Sub
End If

currStr = "Test"
NumBytes = 4

tempByteArray1 = StrConv(currStr, vbFromUnicode)
Call CopyMemory(m_mapaddr, tempByteArray1(0), NumBytes)

currStr = ""
ReDim tempByteArray2(NumBytes - 1)
Call CopyMemory(tempByteArray2(0), m_mapaddr, NumBytes)

currStr = StrConv(tempByteArray2, vbUnicode)

Call CleanUp

Private Sub CleanUp()

If m_mapaddr <> 0 Then
Call UnmapViewOfFile(m_mapaddr)
m_mapaddr = 0
End If

If m_maphand <> 0 Then
Call CloseHandle(m_maphand)
m_maphand = 0
End If

End Sub

--
Richard Mason

0 new messages