Valhalla Legends Forums Archive | Visual Basic Programming | Re: Modified copymemory for pointers

AuthorMessageTime
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? :}
April 12, 2007, 11:19 PM
Hell-Lord
Why is your length 1?

[code] Len(Expression) [/code]
April 13, 2007, 1:27 AM
BreW
I didn't use Len() anywhere .......
April 13, 2007, 1:41 AM
Hell-Lord
>.< sorry i am an idiot her just woke up and is still tired.
I meant to use it :P
April 13, 2007, 1:51 AM
Barabajagal
Paul, you're not making any sense...
April 13, 2007, 2:15 AM
Hell-Lord
Nvm ill try figure something out after i wake up sorry for wasting your time guys.
April 13, 2007, 2:21 AM
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.
April 13, 2007, 3:59 PM
BreW
[quote author=l2k-Shadow link=topic=16610.msg167894#msg167894 date=1176479959]
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]

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.
[/quote]
how much did you have last night? :p
April 13, 2007, 7:20 PM
l2k-Shadow
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.

[code]
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
[/code]

April 13, 2007, 8:16 PM
TheMinistered
[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?
[/quote]

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? :}
[/quote]

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
[/quote]

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"
April 17, 2007, 2:58 AM
BreW
I was trying to find a good way to make a variable for a variable.
April 17, 2007, 11:32 AM
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."
April 18, 2007, 11:36 PM
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....
[code]
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.
[/code]
April 19, 2007, 1:36 AM
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?
April 19, 2007, 4:44 AM
BreW
[quote author=RεalityRipplε link=topic=16610.msg168122#msg168122 date=1176957870]
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?
[/quote]

You're not understanding me. I don't nessisarily want all the files loaded at the same time.
April 19, 2007, 7:15 PM
Barabajagal
Make a function that loads the files and use it. That's the best way you're gonna get.
April 19, 2007, 7:31 PM
Newby
[quote author=TheMinistered link=topic=16610.msg168056#msg168056 date=1176778709]
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...
[/quote]

You rock.
April 20, 2007, 11:05 PM
JoeTheOdd
[tt]Private Declare Sub RtlMoveMemory Lib "kernel32" ( _
    ByRef dest As Any, _
    ByVal src As Any, _
    ByVal length As Long)

Public Sub Main()
    Dim str As String
    Dim byteArray(0 To 19) As Byte 'they're all initially null
    Dim i As Integer
   
    str = "0123456789" ' 30 31 32 33 34 35 36 37 38 39
    Call RtlMoveMemory(byteArray(0), str, 10)
   
    For i = 0 To 19 Step 1
        MsgBox Hex(byteArray(i))
        ' 30 31 32 33 34 35 36 37 38 39
        ' 00 00 00 00 00 00 00 00 00 00
    Next i
   
End Sub[/tt]
April 22, 2007, 3:43 AM
BreW
[quote author=Joe[x86] link=topic=16610.msg168218#msg168218 date=1177213410]
[tt]Private Declare Sub RtlMoveMemory Lib "kernel32" ( _
    ByRef dest As Any, _
    ByVal src As Any, _
    ByVal length As Long)

Public Sub Main()
    Dim str As String
    Dim byteArray(0 To 19) As Byte 'they're all initially null
    Dim i As Integer
   
    str = "0123456789" ' 30 31 32 33 34 35 36 37 38 39
    Call RtlMoveMemory(byteArray(0), str, 10)
   
    For i = 0 To 19 Step 1
        MsgBox Hex(byteArray(i))
        ' 30 31 32 33 34 35 36 37 38 39
        ' 00 00 00 00 00 00 00 00 00 00
    Next i
   
End Sub[/tt]
[/quote]

No.
VB6 strings are NOT just byte arrays. They are BSTRs which follow this format:
[DWORD](4) String Len [BYTES](String Len * 2)String Here, in unicode [WORD](2) Double null terminator.
EDIT** Way to not read the entire topic
EDIT 2** Decided to correct joe on a few things...
You really want to message box each individual character....
And For... Next loop is by default Step 1.
But I was intrigued by your use of copymemory. "Call RtlMoveMemory(byteArray(0), str, 10)"
When you use the first element of a byte array, VB6 is really smart enough to move the other byte elements into the other arrays? Also you had to define the length of your string on your own, not very handy. Nice though... I guess. Also I was looking to do something with pointers, not just move a string into a byte array. You could easily enough do that using Split(String, vbNullString). And it allocates the array scope automatically. (which is pretty good) And also why did you do "Dim ByteArray(0 To 19) as Byte"? 0 is assumed to always be the base unless otherwise specified (Option Base 1) which is plain retarded, imo. Thanks for your effort, Joe.
April 22, 2007, 1:17 PM
JoeTheOdd
They are not BSTRs. I tested that code and it's output is exactly as intended.

Also, this was only meant as a demonstration. Correct calling to the base of the byte array would be [tt]byteArray(LBound(byteArray))[/tt].

For defining the length of the string, I didn't really have to do that either. I could have done [tt]Len(str)[/tt].

Using [tt]Step 1[/tt] is a good coding habit -- it prepares you for stepping up to C++, and makes it easier for your code to be understood by non-VB users. I'll reiterate the demonstration only clause for the repeated [tt]MsgBox[/tt] calls.

Lastly, VB6 is not smart at all, let alone smart enough to position the bytes correctly. That is done by the Win32 API, but doesn't take any smartness on it's behalf. The lowbound of the byte array points to the first byte in memory. A byte array is stored in memory as a pointer to a bunch of bytes, that pointer pointing to the first one. RtlMoveMemory works somewhat like this:

[tt]; void RtlMoveMemory(void dest, void src, int length)
; {

    xor esi, esi
_start:
    cmp esi, length
    je _bottom
    inc esi

    lea [dest+esi], [src+esi]

    jmp _start:

_bottom:
: }[/tt]

EDIT -
If you want a pointer to the string, pass an Integer ByVal in dest. Make sure you specifiy ByVal, though, because if it's passed ByRef it will be returned with the value 0x33323130 (little endian) and the 6 bytes following it will be murdified. Well.. the bytes after it will get owned anyhow, which is why the API is dangerous if you don't know what you're doing.
April 22, 2007, 6:04 PM
BreW
[quote author=Joe[x86] link=topic=16610.msg168254#msg168254 date=1177265059]
They are not BSTRs. I tested that code and it's output is exactly as intended.
[/quote]
VB strings are stored in the BSTR format, always. Possibly VB was smart enough to make the conversion first?
[quote]
If you want a pointer to the string, pass an Integer ByVal in dest. Make sure you specifiy ByVal, though, because if it's passed ByRef it will be returned with the value 0x33323130 (little endian) and the 6 bytes following it will be murdified. Well.. the bytes after it will get owned anyhow, which is why the API is dangerous if you don't know what you're doing.
[/quote]
Like I said, read the entire topic. You would have seen that I already have gotten that far.
April 22, 2007, 11:57 PM
TheMinistered
[quote author=Joe[x86] link=topic=16610.msg168254#msg168254 date=1177265059]
They are not BSTRs. I tested that code and it's output is exactly as intended.

Also, this was only meant as a demonstration. Correct calling to the base of the byte array would be [tt]byteArray(LBound(byteArray))[/tt].

For defining the length of the string, I didn't really have to do that either. I could have done [tt]Len(str)[/tt].

Using [tt]Step 1[/tt] is a good coding habit -- it prepares you for stepping up to C++, and makes it easier for your code to be understood by non-VB users. I'll reiterate the demonstration only clause for the repeated [tt]MsgBox[/tt] calls.

Lastly, VB6 is not smart at all, let alone smart enough to position the bytes correctly. That is done by the Win32 API, but doesn't take any smartness on it's behalf. The lowbound of the byte array points to the first byte in memory. A byte array is stored in memory as a pointer to a bunch of bytes, that pointer pointing to the first one. RtlMoveMemory works somewhat like this:

[tt]; void RtlMoveMemory(void dest, void src, int length)
; {

    xor esi, esi
_start:
    cmp esi, length
    je _bottom
    inc esi

    lea [dest+esi], [src+esi]

    jmp _start:

_bottom:
: }[/tt]

EDIT -
If you want a pointer to the string, pass an Integer ByVal in dest. Make sure you specifiy ByVal, though, because if it's passed ByRef it will be returned with the value 0x33323130 (little endian) and the 6 bytes following it will be murdified. Well.. the bytes after it will get owned anyhow, which is why the API is dangerous if you don't know what you're doing.
[/quote]

Joe, I didn't even read your whole reply but the first sentence.  You retarded pig dog of a communist mother frackin pile of hot shit, VB6 strings ARE FRACKING BSTRS!!! LOOK AT MSDN YOU FRACKIN WHORE

A BSTR has a Long prepended to it containing the strings length, then a unicode string followed by a null terminator you silly peice of shiat

Further more, you don't "call" a byte array, you should have said correct reading, of the memory, of the base of the byte array... yadda yadda anywho i'm done busting on your ass... but I assume you meant "calling rtlmovememory on a byte array" you should be more clear ;)
April 23, 2007, 2:42 AM
JoeTheOdd
I didn't mean calling as in [tt]Call[/tt], I meant naming it. :P

And yeah, I forgot that BSTR has the Int32 before it, not in it. That's a retarded format, by the way.

BreW, you've gotta get it through your head somehow that VB isn't smart enough to do a conversion between Sting and Any for you. Learn about stacks. This is what happened:

[tt]; sub Main

str = dword ptr [esp-14]    ; 10 bytes
bytArry = dword ptr [esp-34]  ; 20 bytes

mov [str+0], 30h
mov [str+1], 31h
mov [str+2], 32h
mov [str+3], 33h
mov [str+4], 34h
mov [str+5], 35h
mov [str+6], 36h
mov [str+7], 37h
mov [str+8], 38h
mov [str+9], 39h
; VB: str = "0123456789"

push 10
push str
push bytArry
call RtlMoveMemoryA

xor esi, esi
_loopStart:
    cmp esi, 19
    jge _loopEnd:

    ;VB MsgBox(byteArray(esi))

  jmp _loopStart
_loopEnd:

call GetCurrentProcess
push eax
call TerminateProcess

; end sub[/tt]
April 23, 2007, 3:20 AM
Newby
[quote author=Joe[x86] link=topic=16610.msg168293#msg168293 date=1177298458]
BreW, you've gotta get it through your head somehow that VB isn't smart enough to do a conversion between Sting and Any for you. Learn about stacks. This is what happened:

[assembly goes here]
[/quote]

Spewing off random assembly (that probably isn't even completely accurate of what actually occurs) to someone who doesn't understand assembly is not going to help him, smartass.

Where do the concept of a stack and the conversation from "Sting" and "Any" intermix? Maybe explain to him the "stack" and how it would matter to him, a high-level VB programmer.

@ your assembly in the above quoted post: I thought all Windows API calls were pushed in the Pascal convention, and not C-style convention?

@ the assembly in this post:

[quote author=Joe[x86] link=topic=16610.msg168254#msg168254 date=1177265059]
[tt]; void RtlMoveMemory(void dest, void src, int length)
; {

    xor esi, esi
_start:
    cmp esi, length
    je _bottom
    inc esi

    lea [dest+esi], [src+esi]

    jmp _start:

_bottom:
: }[/tt]
[/quote]

Wouldn't it be necessary to increment esi after the lea operation, so as to get src+0 moved over as well? :|

(My assembly is incredibly rusty, so eh.)
April 23, 2007, 3:39 AM
JoeTheOdd
Yeah, good point. That was using a 1-based array.

Spewing off random assembly is a subtle sign that he needs to learn assembly. :P

The fact that he's using VB doesn't matter. He's calling the C API, and pushing a series of pointers on to the stack. What VB thinks it points to doesn't matter, C++ receives it as a pointer, not a BSTR, CSTR, PSTR, PWSTR, DWORD, LPDWORD, or whatever else you might want to call it.

As far as pascal, nope. Here's the first call I saw when I loaded up a random IDA database (lockdown04::GetVerHash, if anyone cares):
[tt].text:00ED12AD                push    esi            ; lpData
.text:00ED12AE                push    edi            ; dwLen
.text:00ED12AF                push    [ebp+dwHandle]  ; dwHandle
.text:00ED12B5                lea    eax, [ebp+sFilename]
.text:00ED12BB                push    eax            ; lptstrFilename
.text:00ED12BC                call    GetFileVersionInfoW ; C++: (bool)eax = GetFileVersionInfoW(sFilename, dwHandle, edi, esi)[/tt]
April 23, 2007, 9:11 AM
BreW
[quote author=Joe[x86] link=topic=16610.msg168305#msg168305 date=1177319467]
Yeah, good point. That was using a 1-based array.

Spewing off random assembly is a subtle sign that he needs to learn assembly. :P
[/quote]
If you say I don't know any C++ at all, then why are you saying I need to learn assembly? That'd be murder. (I'm not too good yet-- The best I could do is probably is manipulate registers so on. Also getting an assembler to assemble my programs so i can learn more wouldn't be a bad idea either. I've tried MASM32, but I don't like it's C syntax. It's either asm or not asm.)

[quote]
The fact that he's using VB doesn't matter. He's calling the C API, and pushing a series of pointers on to the stack. What VB thinks it points to doesn't matter, C++ receives it as a pointer, not a BSTR, CSTR, PSTR, PWSTR, DWORD, LPDWORD, or whatever else you might want to call it.
[/quote]
Sure it does, but ANY string that vb stores in memory is in UNICODE. this means it stores a word of data for each character. Therefore the length in bytes of "hello world" wouldn't be 11, but instead 22. 28 for the entire length. You're thinking of a byte array.... dammit, sorry to say but GO back to C or what ever you code in. You usually speak down about vb and whatever, but you clearly don't know specifics about the VB language-- This board is for vb specific discussions, therefore it wouldn't be a good idea to go on about some language you obviously don't use much if at all.
April 23, 2007, 7:42 PM
JoeTheOdd
I'm not speaking down about Visual Basic. Replace each instance of VB with C# in there and I'd say it again. Your compiler is not intelligent, it relies on you to be intelligent. For example try this:

[tt]Private Declare Sub RtlMoveMemory Lib "kernel32" (Void as Any) ' This obviously won't work

Public Sub Main()
    Call RtlMoveMemory("ngrplz")
End Sub[/tt]

It'll get past your compiler no problem, but when you try running it your world comes crashing to reality.
April 23, 2007, 9:51 PM
l2k-Shadow
[quote author=Joe[x86] link=topic=16610.msg168316#msg168316 date=1177365079]
I'm not speaking down about Visual Basic. Replace each instance of VB with C# in there and I'd say it again. Your compiler is not intelligent, it relies on you to be intelligent. For example try this:

[tt]Private Declare Sub RtlMoveMemory Lib "kernel32" (Void as Any) ' This obviously won't work

Public Sub Main()
    Call RtlMoveMemory("ngrplz")
End Sub[/tt]

It'll get past your compiler no problem, but when you try running it your world comes crashing to reality.
[/quote]

how the fuck can the compiler know that you're not passing the correct amount of variables to a function located outside the program defined by the user?

The same would happen in C++ if the function wasn't defined in windows.h
April 23, 2007, 10:46 PM
BreW
[quote author=l2k-Shadow link=topic=16610.msg168318#msg168318 date=1177368408]
how the fuck can the compiler know that you're not passing the correct amount of variables to a function located outside the program defined by the user?
The same would happen in C++ if the function wasn't defined in windows.h
[/quote]
hahaha so true
and joe why do you keep using [tt] instead of [code] thats so annoying[/code]
April 24, 2007, 12:40 AM
JoeTheOdd
[quote author=l2k-Shadow link=topic=16610.msg168318#msg168318 date=1177368408]
how the fuck can the compiler know that you're not passing the correct amount of variables to a function located outside the program defined by the user?

The same would happen in C++ if the function wasn't defined in windows.h
[/quote]

I hope you know that you just reiterated exactly what I said. :)
April 24, 2007, 5:12 PM
BreW
[quote author=Joe[x86] link=topic=16610.msg168333#msg168333 date=1177434740]
[quote author=l2k-Shadow link=topic=16610.msg168318#msg168318 date=1177368408]
how the fuck can the compiler know that you're not passing the correct amount of variables to a function located outside the program defined by the user?

The same would happen in C++ if the function wasn't defined in windows.h
[/quote]

I hope you know that you just reiterated exactly what I said. :)
[/quote]

Acually, he asked "how the hell could one would possibly expect a compiler to catch something like that?" the same would happen in ANY language. Compilers nowadays are plenty intelligent-- A program is only as intelligent as it's creator made it, not limited to the ide/compiler for a language. For example, say if you were trying to pass the value of 10.0 in an integer variable by pointers. C++, for example, is intelligent enough to demote the value of 10.0 to an integer before it even attempts to pull that off. Sheesh, you don't give modern compilers enough credit. You don't even have to define ANY variables in visual basic, for fucks sake. By the way, what you said about arguments was a retarded example, but I get what you ment.
April 24, 2007, 7:17 PM
JoeTheOdd
Visual Basic simply declares it in the back of it's mind as a Variant.
April 24, 2007, 9:06 PM
BreW
It's still pretty damn smart. Think of C++, where it flips out if you don't delcare something
April 25, 2007, 1:41 AM
rabbit
Because C++ is a good language.
April 25, 2007, 2:51 AM
Myndfyr
[quote author=brew link=topic=16610.msg168349#msg168349 date=1177465319]
It's still pretty damn smart. Think of C++, where it flips out if you don't delcare something
[/quote]
You mean, to help enforce code maintainability and efficiency?  Oh noes!
April 25, 2007, 4:38 AM
St0rm.iD
So, like, why hasn't anyone bitched him out yet for trying to copy x amount of data into a y sized buffer with no bounds checks? Last time I checked, we've been trying to get rid of these sort of bugs for oh...I don't know...thirty years? One of the reasons people use VB is to stay AWAY from pointers: they're dangerous.

Hint: Put "ByRef" in front of one of your string arguments and assign to it and see what happens.
April 25, 2007, 2:08 PM
BreW
[quote author=Banana fanna fo fanna link=topic=16610.msg168356#msg168356 date=1177510084]
So, like, why hasn't anyone bitched him out yet for trying to copy x amount of data into a y sized buffer with no bounds checks? Last time I checked, we've been trying to get rid of these sort of bugs for oh...I don't know...thirty years? One of the reasons people use VB is to stay AWAY from pointers: they're dangerous.

Hint: Put "ByRef" in front of one of your string arguments and assign to it and see what happens.
[/quote]
Did you see the original copymemory api with modified arguments? It's source is ByVal for a reason.

Edit***
On another note, I tried out the function that I came up with to extract the value of a string variable from a pointer. It works fine when in vb6, but when compiled, it crashes. Any ideas? And it totally doesn't work when you put in any value lower then the pointer's value for the source, I was going to try to make a quick program to read the first MB of memory. Crashed faster then a 95 yr old woman on the highway :/ Any ideas on how to do this properly?
April 25, 2007, 3:51 PM
St0rm.iD
[quote author=brew link=topic=16610.msg168116#msg168116 date=1176946595]
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....
[code]
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.
[/code]
[/quote]

Could be accomplished by using ByRef. I know what ByVal and ByRef means, brew.
April 26, 2007, 12:19 AM
BreW
ohhhhhhhhhh shit, hahahahaha omg bannana that made me feel like a total idiot. this entire thing was useless. I thought you ment adding a byref in one of the arguments in Copymemory, sorry I wasn't reading your post correctly. i can't believe i didn't think of that.....
April 26, 2007, 12:33 AM

Search