Solved

OLE,COM in console apps

Posted on 1997-03-29
1
452 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
1 Comment
 
LVL 11

Accepted Solution

by:
mikeblas earned 150 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now