Pointers arithmetic in VB ?

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 ?

dvplayltdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EDDYKTCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dvplayltdAuthor Commented:
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++).
Answer ("I do NOT know" also is answer :) ) and I will accept you answer.



EDDYKTCommented:
the only thing i know VB doesn't deal with pointer really well.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

EDDYKTCommented:
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
ArkCommented:
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

ArkCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.