mfc >> QueryInterface for user defined interface is NULL in non MFC application

by vijay » Tue, 02 Dec 2003 18:22:40 GMT

Platform: VC 6.0, NT4.0





I created a class ImyObject which is derived from CCmdTarget as given below



static const GUID IID_IMyObject =

{ 0xe71032e0, 0x60e9, 0x11d1, { 0xb1, 0xaa, 0x0, 0x0, 0x21, 0x85, 0x0,
0x10 } };



interface IMyObject: public IUnknown

{

virtual HRESULT STDMETHODCALLTYPE GetObjectPointer(DWORD* pdwPtr)

= 0;

};



class CStorage : public CCmdTarget

{

public:



.

BEGIN_INTERFACE_PART(PersistStreamObj, IPersistStream)


...

END_INTERFACE_PART(PersistStreamObj)



BEGIN_INTERFACE_PART(MyObject, IMyObject)

STDMETHOD_(HRESULT,GetObjectPointer)(DWORD* pdwPtr);

END_INTERFACE_PART(IMyObject)

.



};





IMPLEMENT_SERIAL(CStorage, CCmdTarget, 0)



BEGIN_INTERFACE_MAP(CStorage, CCmdTarget)

INTERFACE_PART(CStorage, IID_IPersistStream, PersistStreamObj)

INTERFACE_PART(CStorage, IID_IMyObject, MyObject)

END_INTERFACE_MAP()



STATSTG memstat;

IPersistStream* pPersistStm = NULL;

CoCreateInstance(statstg.clsid, NULL, CLSCTX_ALL, IID_IPersistStream,

(void**)&pPersistStm);



IMyObject* pMyObject;

HRESULT h;

h = pPersistStm->QueryInterface(IID_IMyObject,(void**)&pMyObject);



Above code is reside in a DLL and it is being executed through global
function. My problem is that



1.. QueryInterface for IID_IMyObject is NULL i.e pMyObject is NULL when
call global function from non MFC application.
2.. It works fine when call function from MFC application


May I know the reason for that problem? Is there any initialization required
before to execute above code in non-MFC application?



Thanks in advance.



Regards

Vijay




mfc >> QueryInterface for user defined interface is NULL in non MFC application

by Trevor » Tue, 02 Dec 2003 21:47:05 GMT





below
required

Vijay,
Yes, you need to call CoInitialize(Ex), CoUninitialize.





Similar Threads

1. Using MFC classes in non-mfc .net application

2. MFC/non-MFC application

How to let the compiler know this is a MFC application?

like the code as follow to let the compiler know this is a WIN32 app.:
#ifdef _WIN32
...
#endif


3. Use MFC from non-MFC application

4. mfc dll with non-mfc application

Hi
I need to show MFC based windows (CWnd inherited) from an old application
that does not use MFC (but has its own window though). Therefore I have put
the code for the new windows in a DLL which I then loads dynamically when
needed. When I test the DLL with a small MFC-based dialog test program it
works perfectly.
When I use it in the old application and steps through the calls with my
debugger everything seems to go allright (no function calls fails) but the
windows is never shown, and it does not seem to receive any window messages
(not even OnPaint messages which is why it isn't show I figure).
I have tried to explicitly call RunModalLoop() for the window from a thread
started with AfxBeginThread but it did not help.
Anyone have an idea of what could be wrong?
Below is the lines I use to create the window.
Best regards,
Peter

// code in dll that creates window
 CTBalloon * pballoon = new CTBalloon(150,100); //CWnd inherited
 pballoon->CreateAndShow();//see below
 AfxBeginThread(RunBalloon,(void*)pballoon); // RunBalloon calls
pballoon->RunModalLoop

pballoon->CreateAndShow() calls among others
CreateEx(dwExStyle,p,NULL,dwStyle,rect,NULL,NULL);
and starts a timer that with intervals should launch among others the
following command:
ShowWindow(SW_RESTORE);


5. Using MFC dll from non-MFC application

6. including a MFC program into a non mfc application

Hello,
I am working on a application that is written in C, but the GUI is
written in win32 and Motif for Unix. I want to add a grid control
based on Chris Maunder's MFC Grid control which is in MFC to the
program. I have a stand alone version that does what I want it to do,
but I don't know how to add it to the program. I can add it as a new
project but then I don't know how to call the grid.
so my question is when you add a MFC application to a non MFC
application what should you do

thanks
es

7. Type casting object type to user defined class or interface - CSharp/C#

8. declaring user defined class in MFc

Hi all,

I am new at MFC and i am trying to learn it. In my application, if i need to 
add a user defined class,which is the best place to declare it. For example, 
i want to add a class called node_info as

class node_info
{
public:
int proc_id;
int port;
};

Now i am using this class in my main application's file (from where i 
interact with the gui). Which is the best place to declare the class and 
where should i instantiate the objects(i want to instantiate an array of 
objects)

kunal