Results 1 to 5 of 5

Thread: Can you solve the mystery of IUnknown?

  1. #1

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Can you solve the mystery of IUnknown?

    Hi,
    I found a nice piece of code that is working fine.
    But It declares the function as type IUnknown.

    I figured out that is some kind of structure. but I don't understand its working and function propperly (Guess I'm Thick)

    this is the code I saw it in:

    ' Implement support for enumeration (For Each)
    Function NewEnum() As IUnknown
    ' delegate to the private collection
    Set NewEnum = m_InvoiceLines.[_NewEnum]
    End Function

    it's used to enumerate a collection and I do understand the rest of it

    So any info on IUnknown would be appriciated

    Thanks in advance,.....
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Can you solve the mystery of IUnknown?

    Take a look at this and this and this and all of these.

  3. #3
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Can you solve the mystery of IUnknown?

    IUnknown is the interface that all COM objects must implement.

    The interface has three functions QueryInterface, AddRef, and Release.

    Addref is called when you get a pointer to the object (using, mainly, set)
    Release is called when you release a pointer to the object (out of scope, or set obj = nothing
    QueryInterface returns pointers to any other interfaces the object might support. Most VB authored stuff, for instance, implement IDispatch (but not many people know, or even intend to support the interface)

    IUnknown is marked as hidden, and the functions on the interface are marked as restricted (so you can't normally call them)

    However, you can actually call them.

    What you have to do is to create a new interface with the same IID as IUnknown like this:

    VB Code:
    1. [
    2.   uuid(F8EA7708-1EAA-11DA-8CD6-0800200C9A66),
    3.   version(1.0),
    4.   helpstring("IUnknown Override")
    5. ]
    6. library IUnknownOverride
    7. {
    8.  
    9.     interface IUnknownOverride;
    10.  
    11.     typedef struct tagVBGUID {
    12.         long Data1;
    13.         short Data2;
    14.         short Data3;
    15.         unsigned char Data4[8];
    16.     } VBGUID;
    17.  
    18.     [
    19.       odl,
    20.       uuid(00000000-0000-0000-C000-000000000046),
    21.       hidden
    22.     ]
    23.     interface IUnknownOverride {
    24.         long QueryInterface([in] VBGUID* riid, [in, out] long* ppvObj);
    25.         long AddRef();
    26.         long Release();
    27.     };
    28. };

    Compile this with MkTypLib.exe (but whatever you do DONT REGISTER IT unless you want to spend the next few hours rebuilding your machine)

    Use the references box in VB6 to add a reference to the typelib and you should be able to DIM the interface, and call it's functions.

    This may seem a little 'too under the hood' but it's incredibly useful for storing arrays of object references (you add the pointer to an array of longs, but call AddRef (so the object doesn't move or get released) and then you can change the ppObject of your native VB object to convert the pointer back into a VB object)

    Much much much more efficient than collections that you need to access sequentially by their numerical ordinal. And of course, much more memory efficient too (you are only storing a long per object in the array)
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Can you solve the mystery of IUnknown?

    VB Code:
    1. Public Function PtrToObject(ByVal Ptr As Long) As Object
    2.  
    3.    Dim obj As Object
    4.  
    5.    CopyMemory obj, lObjRef, 4
    6.    Set PtrToObject = obj
    7.    CopyMemory obj, 0&, 4
    8.  
    9. End Function

    This converts the long into an object. You should create typed versions of this function (avoids IDispatch, and late-binding)
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  5. #5

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Can you solve the mystery of IUnknown?

    Thanks for the info Hack somehow i didn't think of just typing iunknown into google

    and

    ywdrffa special thanks to you for the extended explenation .
    gonna look into my favorite api site to check your last post
    BTW it's Hacks favorite API site too
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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