Solved

SHARED ARRAYS IN VB

Posted on 2000-05-07
6
281 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

733 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