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
Solved

OLE,COM in console apps

Posted on 1997-03-29
1
468 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

856 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