?
Solved

SHARED ARRAYS IN VB

Posted on 2000-05-07
6
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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