[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Console program as COM server

Posted on 1998-06-02
5
Medium Priority
?
570 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
[X]
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
  • 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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 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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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