Solved

SHARED ARRAYS IN VB

Posted on 2000-05-07
6
275 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

12 Experts available now in Live!

Get 1:1 Help Now