?
Solved

OLE,COM in console apps

Posted on 1997-03-29
1
Medium Priority
?
489 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:phonet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 11

Accepted Solution

by:
mikeblas earned 300 total points
ID: 1300726
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question