Pointer to a memory buffer...-VBForums
Results 1 to 12 of 12

Thread: Pointer to a memory buffer...

  1. #1

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333

    Pointer to a memory buffer...

    I want to pass an argument into an activeX object...

    It's been specified that the argument needs to be a pointer to a memory buffer... Can I make something like that in VB?

    have tried to make a byte array, but this doesn't seem to fit the bill...

    Also tried creating an ADODB.Stream...

    Do they want the address for the beginning of a piece of memory?

    Cheers!

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092
    What is the data type that the argument is expecting? A Long?

    Try StrPtr for pointers to strings, ObjPtr for pointers to objects, and VarPtr for pointers to anything else.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    I think it wanted a pointer to a "memory buffer" (for storing an image)... I'll have to go back and make sure tomorrow. Will post to let you know if it worked

    Cheers!

  4. #4

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    The argument is actually of type short...

    long blah(short FAR* Buffer)

    (c++ short equiv. to vb Integer)

    Have a problem...

    The pointers that I am able to retrieve are all of type long, but the C++ interface requires it to be short.

    CInt(pointer) does not appear to be allowed as this tries to convert the long into an Integer...

    Any suggestions?

    Thanks
    Last edited by chuddy; Dec 13th, 2003 at 09:26 AM.

  5. #5

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    I am now trying to do something similiar to the first post, but with an array... varPtr, objPtr and strPtr don't appear to work for this. Is there a workaround?

    i.e.
    dim byteArr() as Byte
    redim byteArr(10)

    call ObjPtr(byteArr())
    call VarPtr(byteArr())
    call StrPtr(byteArr())

    All the above fail due to "Type Mismatch"

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by chuddy
    The argument is actually of type short...

    long blah(short FAR* Buffer)

    (c++ short equiv. to vb Integer)

    Have a problem...

    The pointers that I am able to retrieve are all of type long, but the C++ interface requires it to be short.

    CInt(pointer) does not appear to be allowed as this tries to convert the long into an Integer...

    Any suggestions?

    Thanks
    You do in fact want a Long for your pointer. short refers to the data being pointed to, not the size of the pointer. Use VarPtr to get the address of the buffer and be sure to pass it ByVal to the C/C++ function - it is expecting the address of the buffer, not the address of the Long holding the buffer address.

    Also, blah is going to return a Long so you will need an lvalue for that. VarPtr, ObjPtr & StrPtr also require an lvalue, which is most likely the reason for the errors that you mention in your last post.

    Ex:
    VB Code:
    1. Dim ptr       As Long
    2. Dim result    As Long
    3. Dim buffer(n) As Byte  'where n = size of image
    4.  
    5. ptr = VarPtr(buffer(0))
    6. result = blah(ByVal ptr)

  7. #7

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    ccoder,

    That's a very useful bit of information...

    I still cannot get it to work, but have a feeling that there may be a problem with the method. Within VB it's expecting me to pass an Integer through as the argument representing the pointer...

    I can see this because of the "intelli" (ctrl + space) thing... feeding a Long in upsets the compiler as it's expecting an integer and attempting to cast the long (cInt(long value)) causes an overflow error.... am going to see wht the guys who wrote the stuff reckon.

    Anyway, the little explanation below is very handy as I have to do several similar calls... Cheers!



    [added later: isn't byVal varPtr(myArray(0)) equal to myArray(0)? (I can see it is not as my app crashes when I try to pass in myArray(0), but not the other method... not sure why though. Anybody know?)]
    Last edited by chuddy; Dec 15th, 2003 at 05:43 AM.

  8. #8

  9. #9
    Frenzied Member
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,026
    In the DLL declaration declare the parameter ByRef As Integer

    e.g.
    VB Code:
    1. Public Declare Function blah lib "foo" (Byref Buff As Integer) As Long

    Then allocate the buffer and pass the first element...This works because VB arrays are contiguous blocks of memory.

    e.g.
    VB Code:
    1. Dim lRet As Long
    2. Dim buff(0 to 200) As Integer
    3.  
    4. lRet = blah(buff(0))

    Always allocate the buffer as big as you need before calling the API call...

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by chuddy
    [added later: isn't byVal varPtr(myArray(0)) equal to myArray(0)? (I can see it is not as my app crashes when I try to pass in myArray(0), but not the other method... not sure why though. Anybody know?)]
    It should be since the default is ByRef.

    I see that Merrion has already replied with the same suggestion that I was going to make. I wasn't sure what kind of type checking VB would do since we were passing a "pointer", but now that I think about it, it makes sense. I guess my C background got in the way this time. C doen't care how the data is defined as long as you cast it correctly.

  11. #11

    Thread Starter
    Hyperactive Member chuddy's Avatar
    Join Date
    Oct 2002
    Posts
    333
    ccoder, Merrion, Wokawidget and crptcblade- thanks all the info. The call is now working and it's all good for the moment

    ... at this rate I may get to keep my hair till a ripe old age...

    Cheers

  12. #12
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by chuddy
    ... at this rate I may get to keep my hair till a ripe old age...
    I'm getting there...don't count on it!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.