Solved

Console program as COM server

Posted on 1998-06-02
5
550 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 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

18 Experts available now in Live!

Get 1:1 Help Now