Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create an array that resides at a given address in memory

Status
Not open for further replies.

chell

Programmer
Oct 26, 2000
30
US
Hello Everyone;

I've created a program with an arbitrary resource (a resource but not an icon) included in the exe file and the API functions return a long which is the address to the data. I would like to access this data by using a variable that is an array of bytes.

So far my idea is to use either assembly language or C to create a function that would be like an expanded Redim function. It would take an undimensioned array, the size of the array, and an address. Perhaps an API function already exists?

My function might look sortof like this:
function RedimAt (buffer() as byte, SizeOf as long, Adr as long)

'Set buffer() variable to point to Adr
'Set buffer() dimensions to SizeOf


end function


Sincerely;
Michelle
 
Hi the function you should look at is the CopyMemory function of the win32 api.

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

In your case, you have first to dimension the buffer() array to the appropriate size, and then use the copymemory function as following.

Redim Buffer(SizeOf)
CopyMemory Buffer(),Adr,SizeOf

Please make sure, that the adr-pointer to the resource is within your applications memory space. If it's not and belongs to the adress space of a different thread, or if you exceed the limit of the adress space, by using a too large SizeOf number, you will experiance a nice crash of the entire vb enviroment. Its just, that vb isn't made to deal with pointers, or at least hides them all the time form the users.

I hope that helps ... by OpenWater
 
This should do the trick (without moving the data in memory).

--------------------------------------------
Private Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type

Private Type SAFEARRAY1D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 0) As SAFEARRAYBOUND
End Type

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Var() As Any) As Long

Private Sub Command1_Click()
Dim Ssa As SAFEARRAY1D, SsaPtr As Long
Dim SArr() As Byte
ReDim SArr(0) 'create the safearray structure
CopyMemory SsaPtr, ByVal VarPtrArray(SArr), 4 'Get a pointer to the SafeArray structure
CopyMemory Ssa, ByVal SsaPtr, Len(Ssa) 'Copy the safearray stucture into varaible Ssa
Ssa.pvData = 'Here you need to set the pointer to memory area where you keep you data
Ssa.Bounds(0).cElements = 'Here you need to set the lenght of the memory area in bytes
CopyMemory ByVal VarPtrArray(SArr), VarPtr(Ssa), 4 'Copy the new info back to the SafeArray

'Sarr() now contains you data

CopyMemory ByVal VarPtrArray(SArr), 0&, 4 'Clean up
End Sub
----------------------------------------- Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top