Solved

SHARED ARRAYS IN VB

Posted on 2000-05-07
6
278 Views
Last Modified: 2008-02-01
I'm converting from VAX BASIC to VB. Is there a way similar to VMS's Named COMMON or C's UNION statement where I can have several arrays start at the same address in memory? The application is a realtime structure which uses a linked list of values; some are bytes, some are integers, some long integers.

Byte BArray (2047,31)    ' 32 bytes
Word IArray (2047,15)    ! 32 bytes
Long LArray (2047,7)     ! 32 bytes

I would like to see these arrays overlay each other by starting at the same physical memory address.

Thanks in Advance,
Bob Fidelman
0
Comment
Question by:bobf22
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2785370
As far as i know, you can't have the arrays to be analysed the way you want, but you can copy the memory (using the LSet operator) from one array to the other, thus simulation the behaviour.

Hope this helps
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2785758
Yes, LSet works okay for what you want to do.  For other things (like converting Single floats to Longs) is easy with the MoveMemory API.

-- in module --

Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)

-- in form / module --

Public Function AsciiFloat(S As String) As Single
    Dim L As Long
    Dim F As Single
   
    L = AsciiLong(S)
    MoveMemory F, L, 4
    AsciiFloat = F
End Function


0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 2787289
Helas VB does not have a UNION like structure
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:leendertz
ID: 2787834
Don't know how to do it but maybe u can use the win32 memory mapped file functions in VB using global namespace(like mapped common block in Vax/VMS)to share data between applications.
Read the book "VB Progr. Guide to the Win32 API" -by Dan Appleman.
There're examples elaborated in that book.

I hope this help u
0
 

Accepted Solution

by:
Spngbob_SqPnts earned 100 total points
ID: 2826130
This may help you, but having three arrays to point at one memory address does not seem to work well.  You could have strings, integer, and longs and other data types all in one array. For 2dimensionals you may have to take the one dimension already stored with data and write into the 2 dimension through a loop.

'-------------

Public anArray(0 To 32) As Byte

'Notice the number 4 after PutMem... indicates number of bytes to read/store
'there is a 2, 1, and 8
Private Declare Sub PutMem4 Lib "msvbvm50.dll" (ByVal p As Long, ByVal value As Long)
Private Declare Sub GetMem4 Lib "msvbvm50.dll" (ByVal p As Long, var As Long)

Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)

Sub test()
  Dim a As Long, b As Long
  Dim c As String * 6
  Dim cc As String
  Dim d(0 To 7) As Byte
  Dim p As Long
  a = 100: b = 0
 
  'string value to store into array
  c = "APE"
  cc = StrConv(c, vbFromUnicode)
  anArray(0) = 100
 
  'store string value into array beginning at element 8
  CopyMemory anArray(8), ByVal StrPtr((cc)), LenB(cc)
 
  'copy from anArray starting at element 8 and store into d() array starting at element 0
  CopyMemory d(0), ByVal VarPtr(anArray(8)), 6
  'read from array the string to store into string variable
  c = Mid(StrConv(d(), vbUnicode), 1, 6)
   
  'assign array pointer into variable p
  p = VarPtr(anArray(0))
 
  'GetMem4 will read the long value at p-pointer to store into variable b
  GetMem4 p, b
  MsgBox "NewValue_b = " & CStr(b)
  b = 200001
  MsgBox "OldValue_a = " & CStr(anArray(0))
 
  'p will receive value for ptr from variable b
  PutMem4 p, b
 
  GetMem4 p, a
 
    For i% = 0 To 14
    Debug.Print "anarray:" & i% & " =" & anArray(x%, i%)
   Next i%
 
  MsgBox "NewValue_a = " & CStr(a)
End Sub

'----------------
'hope this helps....
0
 

Author Comment

by:bobf22
ID: 2826145
This is great... it gives me a head start on the array and pointers. excellent answer.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening Remote & Local Data Connection 2 57
Looking for VB6 code to read SQL table export it to ascii 8 38
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 46
MsgBox 4 59
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question