Solved

Console program as COM server

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it possible to use a scroll bar to view records in a form? 7 44
Is COM supported from Apache 1 55
Authentication of Web Services 3 74
Problem to open text file 11 113
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

803 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