Results 1 to 3 of 3

Thread: Question about Visual Basic 6 and memory buffers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    1

    Question about Visual Basic 6 and memory buffers

    Hello,

    I have a DLL written in C++: one of its functions returns a pointer to a
    memory buffer in form of a long value (32 bits); when calling this function
    from within Visual Basic, which is the best way to let Visual Basic access
    this existing buffer?

    I know that Visual Basic lets define a memory buffers like this:
    Dim buffer() As Byte
    how can I initialize this buffer using the long value returned by the DLL?

    Thanks in advance and Kind Regards

    Severino

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Question about Visual Basic 6 and memory buffers

    It's a bit hard to try and access it as such so your best bet is to use RtlMoveMemory to move the buffer contents into your VB buffer. Either that or pass a pointer to your VB buffer in the first place.

    VB Code:
    1. Declare Sub RtlMoveMemory Lib "kernel32" ( _
    2.     ByRef lpvDest As Any, _
    3.     ByRef lpvSrc As Any, _
    4.     ByVal cbLen As Long _
    5. )

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Question about Visual Basic 6 and memory buffers

    You might be interested about the trick used in this code: InStrCount. Basically the code makes a fake SAFEARRAYHEADER, assings this to an Integer array using CopyMemory (RtlMoveMemory) and then we could virtually read memory from anywhere.

    Noticeable is that BufStrHeader(1) = 2 tells the array to be used is an integer array (two bytes per item). Change to 1 and it works with a byte array.


    And then the warning: doing this is pretty dangerous, so save often and don't blame us if you destroy your computer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width