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?
 
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
0
 
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.



0
 
EDDYKTCommented:
the only thing i know VB doesn't deal with pointer really well.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

0
 
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.