?
Solved

Console program as COM server

Posted on 1998-06-02
5
Medium Priority
?
575 Views
Last Modified: 2013-12-04
I've got an existing console program that we'd like to add
a COM interface to.  We'd like it to be a COM server for
a particular interface, that will query it for some data.

Using VC++ 5.0 and the ATL Wizard I've produced a COM server that is a Windows program without the UI.  My question is:  Do I need to have a HINSTANCE pointer to be
a COM server?  Microsoft is using a macro generated table to
provide access to my COM interface.  Is this necessary?

Does anyone have the skeleton code for a COM server that is
a console application?

Wizard generated code:

CExeModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
      OBJECT_ENTRY(CLSID_quotag, Cquotag)
END_OBJECT_MAP()


/////////////////////////////////////////////////////////////////////////////
//
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
      HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)
{
      lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
      HRESULT hRes = CoInitialize(NULL);
//  If you are running on NT 4.0 or higher you can use the following call
//      instead to make the EXE free threaded.
//  This means that calls come in on a random RPC thread
//      HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
      _ASSERTE(SUCCEEDED(hRes));
      _Module.Init(ObjectMap, hInstance);
      _Module.dwThreadID = GetCurrentThreadId();
      TCHAR szTokens[] = _T("-/");

      int nRet = 0;
      BOOL bRun = TRUE;
      LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
      while (lpszToken != NULL)
      {
            if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
            {
                  _Module.UpdateRegistryFromResource(IDR_Com, FALSE);
                  nRet = _Module.UnregisterServer();
                  bRun = FALSE;
                  break;
            }
            if (lstrcmpi(lpszToken, _T("RegServer"))==0)
            {
                  _Module.UpdateRegistryFromResource(IDR_Com, TRUE);
                  nRet = _Module.RegisterServer(TRUE);
                  bRun = FALSE;
                  break;
            }
            lpszToken = FindOneOf(lpszToken, szTokens);
      }

      if (bRun)
      {
            hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
                  REGCLS_MULTIPLEUSE);
            _ASSERTE(SUCCEEDED(hRes));

            MSG msg;
            while (GetMessage(&msg, 0, 0, 0))
                  DispatchMessage(&msg);

            _Module.RevokeClassObjects();
      }

      CoUninitialize();
      return nRet;
}
0
Comment
Question by:gerry99
  • 3
5 Comments
 

Expert Comment

by:lbrucher
ID: 1406142
One of the differences between Console and window app is the entry point, main() vs WinMain(). main() does not give you a hInstance but I think you can get it using GetModuleHandle(NULL).
Also, a console app has the _CONSOLE macro defined instead of _WINDOWS. Dunno whether it makes a difference or not with ATL.
0
 
LVL 3

Expert Comment

by:altena
ID: 1406143
No way gerry..

EXE-servers need a message loop. period

Your safest bet:
Port your code to an ATL-exe COM server. If your console
application is "clean", that should give you not too
much trouble.

Good Luck
0
 
LVL 3

Accepted Solution

by:
altena earned 300 total points
ID: 1406144
ehhh I intended that last comment to be an answer. Sorry.
0
 
LVL 1

Author Comment

by:gerry99
ID: 1406145
altena,

Your answer makes sense.  However, why do EXE COM servers need a message loop?  COM is a binary object method interface that does not seem to mention windows messages anywhere.  I think of it as a way to call the functions of a module, why would I need a message loop?

The program I am working with is a high volume information server that reads new messages from an input file and diseminates them to clients who have made socket connections on the internet.  Providing the COM interface will allow this program to add support for a query we normally make to our database.  However, COM support is not meant to be the primary function of this server.  If I add a message loop, can I run it from one of our threads, instead of the main?

0
 
LVL 3

Expert Comment

by:altena
ID: 1406146
Good Questions gerry...

I will answer the first one:
"Why do EXE COM servers need a message loop?"

Com needs a way to call your "binary object method interface" and
to do that it will send messages to a hidden window that com
creates whenever your exe-com server gets started.
Now if you do not have a message loop, com can send all the messages it wants, they will not get processed....

I hope that makes the pieces fall in place.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Screencast - Getting to Know the Pipeline

864 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