Solved

# Pointers arithmetic in VB ?

Posted on 2006-04-18
573 Views
Hello experts !

I has pointer of memory using return value from MapViewOfFile and then by RtlMoveMemory read/write data.

Publuc Type TROI
Xxx as long
Yyy as long
End type
Dim ROI as TROI,  P as long
P = MapViewOfFile(HANDLE_MapedFile, FILE_MAP_WRITE, 0, 0, Len(ROI))
CopyMemory ByVal P, ROI, Len(ROI)

Now I have to access memory with some offset from this pointer – let say 2x Len(ROI). A am pretty sure what I have in offset.

Can I make next ?

Dim ROI2 as TROI
CopyMemory ByVal (P+ Len(ROI2)), ROI2, Len(ROI)

Will this will work ALWAYS ? As I know pointers are LONG (DWORD), but has NO sign, while in VB6 Long has sign. Does (P+ Len(ROI)) will produce error if P is negative ?

0
Question by:dvplayltd

LVL 26

Accepted Solution

i will use byte to hold it first

i.e
dim i as long
dim TotalByte1() As Byte

ReDim Preserve TotalByte1(Lenb(ROI)) * whatever_the_size - 1)
CopyMemory TotalByte1(0), byval p, Lenb(ROI)) * whatever_the_size
for i=1 to whatever_the_size
CopyMemory ROI2, TotalByte1(lenb(ROI) * i), Lenb(ROI))
next
0

Author Comment

clever.... logic is rock ! you will get grade A and all points :)

however - I am curious is it has value in pointer (p) when VB calulation (with respect sign - or +) will produce incorrect results ? I mean error due to aritmetic with + or -  while real pointer use long (i.e DWOD in C++).

0

LVL 26

Expert Comment

the only thing i know VB doesn't deal with pointer really well.
0

LVL 26

Expert Comment

I think there is something wrong on the above code

dim i as long
dim TotalByte1() As Byte

ReDim Preserve TotalByte1(Lenb(ROI)) * whatever_the_size - 1)
CopyMemory TotalByte1(0), byval p, Lenb(ROI)) * whatever_the_size
for i=1 to whatever_the_size
CopyMemory ROI2, TotalByte1(lenb(ROI) * (i-1)), Lenb(ROI))  ' at lease you know the idea
next
0

LVL 27

Expert Comment

Public Const OFFSET_4 = 4294967296#

Public Function LongToUnsigned(Value As Long) As Double
If Value < 0 Then
LongToUnsigned = Value + OFFSET_4
Else
LongToUnsigned = Value
End If
End Function

0

LVL 27

Expert Comment

Oops, sorry, misunderstood the question:
>>Will this will work ALWAYS ? As I know pointers are LONG (DWORD), but has NO sign, while in VB6 Long has sign. Does (P+ Len(ROI)) will produce error if P is negative ?<<
Yes, this will work ALWAYS, even with negative P. You can access memory pointers greater then 2147483647 with negativ values.

Just FYI - reverse funtion:

Public Const MAXINT_4 = 2147483647
Public Function UnsignedToLong(Value As Double) As Long
If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
If Value <= MAXINT_4 Then
UnsignedToLong = Value
Else
UnsignedToLong = Value - OFFSET_4
End If
End Function

0

## Featured Post

### Suggested Solutions

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