Thread: Abstract class (IDispatch) in a C structure

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    Abstract class (IDispatch) in a C structure

    I have a C structure (not my code), which compiles without errors on a C compiler
    Code:
    typedef struct{
    	IDispatch		dispatchObj;
    	DWORD			refCount;
    } _IDispatchEx;
    But when compiling in C++, I get this error:
    Code:
    'IDispatch' : cannot instantiate abstract class
    because IDispatch is an abstract class (COM). I don't understand how it can be declared in C, since C doesn't support classes at all, but the problem is that I need to use this structure in my C++ program.

    How should I change it to work in my C++ project?

    p.s. If I declared dispatchObj as a pointer (IDispatch *dispatchObj; ), the structure wouldn't work as intended (or would it?).

    Thanks for any help.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    All COM objects are intended to be accessed through interface pointers. Declaring an instance of an interface makes no sense, since an interface is not an object but merely a gateway to the functionality of an object.

    I can understand how such a piece of code might get created (by someone confused about COM) but it's not correct.

    It looks like somebody (you?) is trying to create a new interface called IDispatchEx which aggregates an IDispatch interface on some object. If you can give more details about what this is for I might be able to suggest something.

    EDIT: Looking at the "C" structure, this looks like a basic implementation of IDispatch, not an extension. You might look into the various ATL helper classes which let you easily define classes which implement a single interface. Be aware that the way COM is done in C is not at all how it's done in C++, which might be the cause of your confusion.
    Last edited by brewbuck; 12-29-2008 at 04:59 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Thanks for the reply, the code is from an article on codeproject, it's from a header file for a DLL. I can compile the DLL (it's in C), but I also need that header file for my C++ project.

    The full structure with comments from its author is here:
    Code:
    // Our _IDispatchEx struct. This is just an IDispatch with some
    // extra fields appended to it for our use in storing extra
    // info we need for the purpose of reacting to events that happen
    // to some element on a web page.
    typedef struct {
    	IDispatch		dispatchObj;	// The mandatory IDispatch.
    	DWORD			refCount;		// Our reference count.
    	IHTMLWindow2 *	htmlWindow2;	// Where we store the IHTMLWindow2 so that our IDispatch's Invoke() can get it.
    	HWND			hwnd;			// The window hosting the browser page. Our IDispatch's Invoke() sends messages when an event of interest occurs.
    	short			id;				// Any numeric value of your choosing that you wish to associate with this IDispatch.
    	unsigned short	extraSize;		// Byte size of any extra fields prepended to this struct.
    	IUnknown		*object;		// Some object associated with the web page element this IDispatch is for.
    	void			*userdata;		// An extra pointer.
    } _IDispatchEx;
    I've also attached the full header file from that article, the structure is on line 34.
    Sorry for my weak understanding of COM, I just need this structure to work in my C++ project.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by Overlord View Post
    Thanks for the reply, the code is from an article on codeproject, it's from a header file for a DLL. I can compile the DLL (it's in C), but I also need that header file for my C++ project.

    The full structure with comments from its author is here:
    Code:
    // Our _IDispatchEx struct. This is just an IDispatch with some
    // extra fields appended to it for our use in storing extra
    // info we need for the purpose of reacting to events that happen
    // to some element on a web page.
    typedef struct {
    	IDispatch		dispatchObj;	// The mandatory IDispatch.
    	DWORD			refCount;		// Our reference count.
    	IHTMLWindow2 *	htmlWindow2;	// Where we store the IHTMLWindow2 so that our IDispatch's Invoke() can get it.
    	HWND			hwnd;			// The window hosting the browser page. Our IDispatch's Invoke() sends messages when an event of interest occurs.
    	short			id;				// Any numeric value of your choosing that you wish to associate with this IDispatch.
    	unsigned short	extraSize;		// Byte size of any extra fields prepended to this struct.
    	IUnknown		*object;		// Some object associated with the web page element this IDispatch is for.
    	void			*userdata;		// An extra pointer.
    } _IDispatchEx;
    I've also attached the full header file from that article, the structure is on line 34.
    Sorry for my weak understanding of COM, I just need this structure to work in my C++ project.
    Judging from the Codeproject code, a good starting point for conversion to C++ would be:

    Code:
    typedef struct {
    	IDispatch * dispatchObj;	// The mandatory IDispatch.
    	DWORD			refCount;		// Our reference count.
    	IHTMLWindow2 *	htmlWindow2;	// Where we store the IHTMLWindow2 so that our IDispatch's Invoke() can get it.
    	HWND			hwnd;			// The window hosting the browser page. Our IDispatch's Invoke() sends messages when an event of interest occurs.
    	short			id;				// Any numeric value of your choosing that you wish to associate with this IDispatch.
    	unsigned short	extraSize;		// Byte size of any extra fields prepended to this struct.
    	IUnknown		*object;		// Some object associated with the web page element this IDispatch is for.
    	void			*userdata;		// An extra pointer.
    } _IDispatchEx;
    It should be private data in your class declaration. So, give it a try to see if it works.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Thank you, it worked flawlessly. I had declared dispatchObj as a pointer previously, but I thought that would not work, since dispatchObj is not the same as *dispatchObj. But it still works, thanks again and happy New Year.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Basic help with the class structure
    By hobscrk777 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 04:31 PM
  3. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 01:35 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. vector static abstract data structure in C?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 05:02 PM