Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

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 ?

0
dvplayltd
Asked:
dvplayltd
  • 3
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now