About Com:I need help with the following code(simplified)?
-
The following is the simplified code: #include interface ix:IUnknown { virtual void __stdcall fx()=0; }; class Try:public ix { public: HRESULT __stdcall QueryInterface(IID& iid,void** ppv){return S_OK;} virtual ULONG __stdcall AddRef(){return 0;} virtual ULONG __stdcall Release(){return 0;} virtual void __stdcall fx(){} Try(){} }; void f() { Try ptry; } --------------------Configuration: try - Win32 Debug------- d:\files\vc\com\try\try.cpp(23) : error C2259: 'Try' : cannot instantiate abstract class due to following members: d:\files\vc\com\try\try.cpp(7) : see declaration of 'Try' d:\files\vc\com\try\try.cpp(23) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined d:\microsoft visual studio\vc98\include\unknwn.h(109) : see declaration of 'QueryInterface' d:\files\vc\com\try\try.cpp(23) : error C2259: 'Try' : cannot instantiate abstract class due to following members: d:\files\vc\com\try\try.cpp(7) : see declaration of 'Try' d:\files\vc\com\try\try.cpp(23) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined d:\microsoft visual studio\vc98\include\unknwn.h(109) : see declaration of 'QueryInterface' ********************************************************** I really don't know what's wrong with the code. :confused: :confused: :confused:
-
The following is the simplified code: #include interface ix:IUnknown { virtual void __stdcall fx()=0; }; class Try:public ix { public: HRESULT __stdcall QueryInterface(IID& iid,void** ppv){return S_OK;} virtual ULONG __stdcall AddRef(){return 0;} virtual ULONG __stdcall Release(){return 0;} virtual void __stdcall fx(){} Try(){} }; void f() { Try ptry; } --------------------Configuration: try - Win32 Debug------- d:\files\vc\com\try\try.cpp(23) : error C2259: 'Try' : cannot instantiate abstract class due to following members: d:\files\vc\com\try\try.cpp(7) : see declaration of 'Try' d:\files\vc\com\try\try.cpp(23) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined d:\microsoft visual studio\vc98\include\unknwn.h(109) : see declaration of 'QueryInterface' d:\files\vc\com\try\try.cpp(23) : error C2259: 'Try' : cannot instantiate abstract class due to following members: d:\files\vc\com\try\try.cpp(7) : see declaration of 'Try' d:\files\vc\com\try\try.cpp(23) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined d:\microsoft visual studio\vc98\include\unknwn.h(109) : see declaration of 'QueryInterface' ********************************************************** I really don't know what's wrong with the code. :confused: :confused: :confused:
Hi, The thing that you didn't get about COM technology is that "you will not deal with classes any more, what you have to care about is interfaces". Here you are trying to create an instance of Try class, which is wrong in COM world. You have to ask the component for a certain interface, and the component itself will create the class instance for you, and it will cast it to the required interface. To access your component you have to make a call like this one: { IX *pX; //See, you are defining the interface instead of the class HRESULT hr = CoCreateinstance(CLSID_Try, NULL, CLSCTX_SERVER, IID_IX,(void**) &pX); pX->fx(); pX->Release(); } Regards, ShadiK. Shadi Al-Kahwaji
-
Hi, The thing that you didn't get about COM technology is that "you will not deal with classes any more, what you have to care about is interfaces". Here you are trying to create an instance of Try class, which is wrong in COM world. You have to ask the component for a certain interface, and the component itself will create the class instance for you, and it will cast it to the required interface. To access your component you have to make a call like this one: { IX *pX; //See, you are defining the interface instead of the class HRESULT hr = CoCreateinstance(CLSID_Try, NULL, CLSCTX_SERVER, IID_IX,(void**) &pX); pX->fx(); pX->Release(); } Regards, ShadiK. Shadi Al-Kahwaji