|
-
Sep 9th, 2005, 04:17 AM
#1
Thread Starter
Fanatic Member
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.
-
Sep 9th, 2005, 05:43 AM
#2
Re: Can you solve the mystery of IUnknown?
Take a look at this and this and this and all of these.
-
Sep 9th, 2005, 06:05 AM
#3
Frenzied Member
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:
[
uuid(F8EA7708-1EAA-11DA-8CD6-0800200C9A66),
version(1.0),
helpstring("IUnknown Override")
]
library IUnknownOverride
{
interface IUnknownOverride;
typedef struct tagVBGUID {
long Data1;
short Data2;
short Data3;
unsigned char Data4[8];
} VBGUID;
[
odl,
uuid(00000000-0000-0000-C000-000000000046),
hidden
]
interface IUnknownOverride {
long QueryInterface([in] VBGUID* riid, [in, out] long* ppvObj);
long AddRef();
long Release();
};
};
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
-
Sep 9th, 2005, 06:10 AM
#4
Frenzied Member
Re: Can you solve the mystery of IUnknown?
VB Code:
Public Function PtrToObject(ByVal Ptr As Long) As Object
Dim obj As Object
CopyMemory obj, lObjRef, 4
Set PtrToObject = obj
CopyMemory obj, 0&, 4
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
-
Sep 9th, 2005, 09:12 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|