• Welcome to Valhalla Legends Archive.
 

Modified copymemory for pointers

Started by brew, April 12, 2007, 06:19 PM

Previous topic - Next topic

brew

So
I decided to attempt a pointer in vb6 (hard, right?)
after a while I came up with this:

Private Declare Sub CopyMemoryRead Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long)
Public asdf As Long
Private Sub Form_Load()
    Dim zxc&, lngPtr&
    asdf = 50
    lngPtr = VarPtr(asdf)
    CopyMemoryRead zxc, lngPtr, 4
    MsgBox zxc
End Sub

And I want to make an array version of this, anyone have any ideas? I think I would just need to change VarPtr to VarPtrArray or something like that....... I saw "VarPtrArray" somewhere on msdn. And for the record, if anyone says the AddressOf operator and VarPtr() do the same thing, you're totally wrong! At least 3 people already said they're the same today and it's starting to piss me off -_____-;; Uh... so could anyone help?

Edit:

Allright, I'm making a bit of progress:

Private Function PtrString(ptrPointerType As PointerType) As String
    Dim tmp$, i&
    tmp = String(ptrPointerType.lngLen, vbNullChar)
    CopyMemoryRead tmp, ptrPointerType.lngPtr, ptrPointerType.lngLen
    For i= 1 To (ptrPointerType.lngLen * 2) - 1 Step 2
        PtrString = PtrString & Mid$(tmp, i, 1)
    Next i
End Function

Many thanks to Ante, he made this function while i was taking a poo. Anyways I'm still faced with the problem of finding the string len, any ideas on how to do this? :}
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Hell-Lord

#1
Why is your length 1?

Len(Expression)

brew

I didn't use Len() anywhere .......
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Hell-Lord

>.< sorry i am an idiot her just woke up and is still tired.
I meant to use it :P

Barabajagal

Paul, you're not making any sense...

Hell-Lord

Nvm ill try figure something out after i wake up sorry for wasting your time guys.

l2k-Shadow

I'm curious on why you would attempt to do this in VB6, but the only way I see you being able to do this is read at the memory address one byte at a time until you hit a null char.
Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

brew

Quote from: l2k-Shadow on April 13, 2007, 10:59 AM
I'm curious on why you would attempt to do this in VB6, but the only way I see you being able to do this is read at the memory address one byte at a time until you hit a null char.

Good... but that's going to be very inefficient when i use it for what I want to (string arrays). And also I don't think that would really work because i would have to increase the length by 1 every loop ..and it would be something like
Dim i%, strTemp$, strFull
i = 1
Do Until strTemp = vbNullChar
CopyMemoryRead strTemp, lngPtr, i
i = i + 1
strFull = strFull & Mid$(strTemp, i,  1)
Loop
i = 1
so on... and I have no idea how to do this with an array.
Or maybe i could do a bit of pointer arithmetic and extract the length of the string (which, if you recall, is stored in a long 4 bytes from the beginning of the string) and use that. (but then i'd have to use another copymemory) but then again that is just one more as compared to the loop method, which calls it many more times
Quote
>.< sorry i am an idiot her just woke up and is still tired.
how much did you have last night? :p
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

l2k-Shadow

#8
you're right, i forgot that vb6 strings were in unicode. This would probably be your best bet, although how are you looking to read the string arrays? Do you expect them to be located in the memory in a row.. because they most likely won't be.


Private Declare Sub CopyMemoryLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Declare Sub CopyMemoryString Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As String, ByVal Source As Long, ByVal Length As Long)

Public Function ReadStr(ByVal Ptr As Long) As String
Dim strlen As Long, s As String
    Call CopyMemoryLong(strlen, Ptr - 4, 4)
    s = String$(strlen, vbNullChar)
    Call CopyMemoryString(s, Ptr, strlen)
    ReadStr = StrConv(s, vbFromUnicode)
End Function

Private Sub Form_Load()
    Dim s As String, sRes As String
    s = "Hello World!"
    sRes = ReadStr(StrPtr(s))
    MsgBox sRes
End Sub


Quote from: replaced on November 04, 2006, 11:54 AM
I dunno wat it means, someone tell me whats ix86 and pmac?
Can someone send me a working bot source (with bnls support) to my email?  Then help me copy and paste it to my bot? ;D
Já jsem byl určenej abych tady žil,
Dával si ovar, křen a k tomu pivo pil.
Tam by ses povídaj jak prase v žitě měl,
Já nechci před nikym sednout si na prdel.

Já nejsem z USA, já nejsem z USA, já vážně nejsem z USA... a snad se proto na mě nezloběj.

TheMinistered

#9
Quote
I want to make an array version of this, anyone have any ideas? I think I would just need to change VarPtr to VarPtrArray or something like that....... I saw "VarPtrArray" somewhere on msdn. And for the record, if anyone says the AddressOf operator and VarPtr() do the same thing, you're totally wrong! At least 3 people already said they're the same today and it's starting to piss me off -_____-;; Uh... so could anyone help?

To find the pointer to an array, all you need to do is find the address of the first item in the array.  Or if you want to find a pointer to the middle or an item other than the first you simply get the address like so

VarPtr(Array(firstindex)) ' returns pointer to first item in array
VarPtr(Array(lastIndex)) ' returns pointer to last item in array
VarPtr(Array(middleindex)) 'returns pointer to the item in the middle of the array

where Array is the variable name for the array, and ...index is the index which address you want.  To get a pointer to the next item in the array without having to call varptr again,you would simply do arraypointer + sizeofarraytype (sizeofarraytype for instance, if it was an array of integers would be 2, longs would be 4, bytes would be 1, etc etc)

Quote
Many thanks to Ante, he made this function while i was taking a poo. Anyways I'm still faced with the problem of finding the string len, any ideas on how to do this? :}

A vb6 string is a BSTR, there is a variable that holds the length of the string above the string, i believe a long (4 bytes wide).

So (VarPtr(string)-4) should hold the length.

Quote
Private Function PtrString(ptrPointerType As PointerType) As String
    Dim tmp$, i&
    tmp = String(ptrPointerType.lngLen, vbNullChar)
    CopyMemoryRead tmp, ptrPointerType.lngPtr, ptrPointerType.lngLen
    For i= 1 To (ptrPointerType.lngLen * 2) - 1 Step 2
        PtrString = PtrString & Mid$(tmp, i, 1)
    Next i
End Function

This function is completely retarded imho, ante is a lame ass programmer he must smell like dog shiat.  He is a horrible programmer.  I wont even touch on how many bad programming practices are in this function, you should delete this one and consult msdn on writing your own.

On a further note, there aren't pointers in vb6 as a language feature.  Is "psuedo" pointers and I don't really recommend using them unless you have to.  The only cases in which you have to get an address for a variable is if your using an API (probably a few other rare cases as well).  But most tasks can be accomplised in vb with minimal use of "pointers"

brew

I was trying to find a good way to make a variable for a variable.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Imperceptus

last time i tried to do that vb gave me an error that literally came across like "you cannot do this in visual basic."
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

brew

I can already pretty much do this with non-array variables, using pointers. Which by the way was the reason I started getting interested with VB pointers. Say if I was making a program which used many databases, and they were all loaded into memory. I would have to make an entirely different subroutine for each database I wish to load, and this redundant code can consiterably lower performance. For instance....

Public Crap As String

Private Sub Form_Load()
LoadGeneric "crap.txt", VarPtr(Crap)
... so on, maybe, in different areas of the program

Public Sub LoadGeneric(strFilename As String, lngPtr As Long)
   Dim lngVarLen&
   Open App.Path & "\" & strFilename For Binary Access Read As #1
   blah blah blah.....
   CopyMemoryRead lngVarLen, (lngPtr - 4), 4
   CopyMemoryRead lngPtr, LoadedString, lngVarLen
End Sub

Heh.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Barabajagal

uhm... Make an array of filenames and data. Do a For loop to call the sub, passing each array value? Or am I not understanding you?

brew

Quote from: RεalityRipplε on April 18, 2007, 11:44 PM
uhm... Make an array of filenames and data. Do a For loop to call the sub, passing each array value? Or am I not understanding you?

You're not understanding me. I don't nessisarily want all the files loaded at the same time.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P