OLE,COM in console apps

Have any of you used COM or OLE interfaces in a console app?

I have to use DAO in my console app (by use of MFC classes).
Everything works fine, except that when my app is running, some strange things happen  :
-When I try to use the Find feature of the Windows Explorer, the explorer hangs.
-When I try to use the Add Profile wizard (from my Inbox), it hangs.
As soon as I close my app, the find dialog and the profile wizard windows are shown.
I seems that my app is locking some resources.

So, I tried to see what's the reason for this.
I commented everything out of my main() function.
After eliminating many possibilities, there was only one line of code left : AfxDaoInit(), which initializes the DAO interfaces (it is usually called from within the MFC DAO classes, but I suspected this was the problem so I called it myself).
When I ommited this line, the various hang problems mentioned above disappeared!!!

I went one step further : Instead of calling AfxDaoInit, I copied its contents to my main() function.
After further elimination I was left with this code (taken from AfxDaoInit()) :
------------------
DAO_CHECK_ERROR(::CoInitialize(NULL),
AFX_DAO_ERROR_ENGINE_INITIALIZATION);

// Set the interface type
GUID guidEngine;
#ifdef _UNICODE
guidEngine = IID_IDAODBEngineW;
#else
guidEngine = IID_IDAODBEngine;
#endif

LPCLASSFACTORY2 pCF2;
DAO_CHECK_ERROR(::CoGetClassObject(CLSID_CDAODBEngine,
CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory2, (LPVOID*)&pCF2),
AFX_DAO_ERROR_ENGINE_INITIALIZATION);

pCF2->Release();
--------------------
All the calls were successful.
However, when I commented out the CoGetClassObject call, the hang
problems disappeared.
I tried to call OleInitialized instead of CoInitialize but it didn't
help.

I wanted to see if the reason for the problem is DAO, so instead of initalizing DAO interfaces I used another OLE object - the MS Calendar OCX.
I used this code :
-------------------------
ASSERT(::CoInitialize(NULL) == S_OK);
            
LPUNKNOWN pUnk = NULL;

CLSID const CLSID_MSCAL =
{ 0x8e27c92b, 0x1264, 0x101c, { 0x8a, 0x2f, 0x4, 0x2, 0x24, 0x0, 0x9c, 0x2 } };

::CoGetClassObject(CLSID_MSCAL,
CLSCTX_ALL, NULL, IID_IUnknown, (LPVOID*)&pUnk);
            
pUnk->Release();
-----------------------------
All the calls were successful. However, the hang problems were still there. This suggests that the problem is with OLE or COM being used from a console app.
When I commented out CoGetClassObject nothin hung.

I tried one more thing - I wrote a simple Windows app (no-MFC) and called AfxDaoInit in its WinMain.
The hang problems showed up again untill I closed my app.

Am I missing something?
Does anyone have a working console app with OLE and can check out if he/her has those problems?

I'd be most grateful if you could help me with this.

Sincerely,
Shami Reshtik
PhoNet Communication

Pls reply via email also to phonet@shani.net or reshtik@ibm.net.

p.s.
I'm using  VC++ 4.2b on Win95Windows 95
phonetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikeblasCommented:
Your application, if it uses OLE, must have a message loop. OLE uses hidden windows and sent messages to handle some synchronization issues.

It's possible (and just fine) to write a console applicaiton that has a message pump.  You could call CWinApp::Run() and let it pump messages for you, but you probably don't wnat to do that because the loop in Run() isn't easy to preempt and runs until completion.

You should write a message loop of your own that pumps messages until the queue is exhausted and then return to you. You could call this function periodically (the more often, the better) through the lifetime of your application.

.B ekiM


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.