Solved

SHARED ARRAYS IN VB

Posted on 2000-05-07
6
276 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now