Simple Chat Program

.
Max Vigilante.
Bartolo Sorrentino.

Environment: NT4 SP3; Developed with VC 60 SP3;

Unitalk2 is a simple talk with some strange detail:

  • It’s an application built on ATL technology
  • It utilizes Connection Points
  • The UniTalk application (UniTalk2.exe) acts as both the
    server and client depending on the start method*

*if you start the application from Explorer by double-clicking
it, it acts as a client. The “other” instance to which it connects
through DCOM is then the server.

UniTalk2 is a simple chat. Hence, you can talk via a network connection
with another user. The application presents you with a standard (horizontal)
split window format to distinguish the text you’re writing from that
of the remote computer.

In the combo box control, you can choose the friend to “call”. The list displayed
in the combo box is maintained via a simple text file named
IpAddress.txt. When you choose to initiate a conversation with a remote
machine, the user on that machine will see a dialog that informs him
of an incoming chat request. They need only to simply click the Yes button
to acknowledge the request and begin chatting when the application starts
up.

Important Notes

  • The UniTalk application (UniTalk2.exe) must be installed on both
    machines that want to participate in the chat.

  • Both users must be on the same domain
  • UniTalk2 is an interactive, COM-based application,
    that exchanges data across a network connection via DCOM. Therefore,
    you must configure dcomcnfg.exe for the launch of .exe

Description of the UniTalk2 Code

The _tWinMain function (UniTalk2.cpp) function decides (based on how
the application was invoked) if it is to act as a server or client.

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
 HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int nShowCmd)
{
 if( nShowCmd == 1)
 {
  MainBool = true;
 };

...

 if( MainBool )
 {
  _ASSERTE(CCall2::mpDialo1 == NULL);
  CCall2::mpDialo1 = new CDialogo(true);
  CCall2::mpDialo1->DoModal();
 }
 else
 {
  // if MainBool is false, the appplication started in
  // remote-mode and this branch of 'if' run
  _ASSERTE(CCall2::mpDialo2 == NULL);
  CCall2::mpDialo2 = new CDialogo(false);
  CCall2::mpDialo2->DoModal();

  //CCall2::mpDialo2->Create(NULL);
  //CCall2::mpDialo2->ShowWindow(SW_SHOWNORMAL);
 }
}

When CCall2::mpDialo1 is equal to “new CDialog(TRUE)”,
the application is launched as a chat client. Otherwise,
it starts up as the server.

The central part of the application is the CDialog.
This is responsible for the dialogs, creation of interfaces
and creation of connection points. In the RunConnection() function,
you can see the creation of the remote interface via the
call to the standard COM function ::CoCreateInstanceEx()
as well as the creation of CComObject<CRx2>
(mpRx2 = new CComObject<CRx2>) that represents the
connection-point.

void CDialogo::RunConnection()
{
 USES_CONVERSION;
 i = res = 0;
 COSERVERINFO info;
 MULTI_QI qi;
 int result = 0;
 info.dwReserved1 = NULL;
 info.dwReserved2 = NULL;
 info.pAuthInfo   = NULL;
 //info.pwszName	 = NULL;
 info.pwszName	 = A2W(SelectedIp);
 //info.pwszName	= L"150.197.55.101";//max

 qi.pIID = &IID_IUnknown;
 qi.hr	= 0;
 qi.pItf = NULL;

 int	mConnCLCTX;

 if (SelectedIp[0] == ''')
 {
  mLocalTalk = true;

  info.pwszName	 = NULL;
  mConnCLCTX = CLSCTX_LOCAL_SERVER;
 }else
 {
  mConnCLCTX = CLSCTX_REMOTE_SERVER;
 }

 hr = CoCreateInstanceEx(CLSID_Call2,
                         NULL,
                         mConnCLCTX,
                         &info,
                         1,
                         &qi );

 //ATLASSERT(hr==NOERROR);
 if (!SUCCEEDED(hr))
 {
  ::MessageBox(m_hWnd,
               _T("Could not create ExeChat object."
               " Make sure the server is registered."),
               _T("Object Instantiation Error."),
               MB_OK | MB_ICONINFORMATION);

  // return 0L;
 }
 _ASSERT(qi.pItf != NULL);

 hr = qi.pItf->QueryInterface(IID_ICall2,(void**)&pCall2);
 _ASSERT(pCall2 != NULL);

 hr = pCall2->QueryInterface(IID_IConnectionPointContainer,
  (void **)&pConnPtContainer);

 _ASSERT(SUCCEEDED(hr) && pConnPtContainer != NULL);

 //BSTR per il chiamante
 //	wchar_t  nome[] = L"ivan";
 //BSTR mTempBstr = SysAllocString(nome);

 hr = pCall2->Run(&res);
 _ASSERT(SUCCEEDED(hr));

 mpRx2 = new CComObject<CRx2>;
 _ASSERT(mpRx2 != NULL);
 ((IRx2*)mpRx2)->AddRef();

 hr = pConnPtContainer->FindConnectionPoint(IID_IRx2,
  &m_pIRx2ConnectionPoint);

 _ASSERT(SUCCEEDED(hr) && mpRx2 != NULL);

 qi.pItf->Release();
 pConnPtContainer->Release();

 //////////////////////////	Advise	////////////////////////
 _ASSERT(mpRx2 != NULL);
 hr = m_pIRx2ConnectionPoint->Advise((IUnknown*)mpRx2,
  &m_dwCookie);

 _ASSERT(SUCCEEDED(hr) && m_dwCookie != 0);
 ///////////////////////////////////////////////////////////
}

If the CDialogo::RunConnection() function returns
without error, you are linked with your friend
and are ready to chat.

Installation

  • On each machine where you want to run UniTalk,
    you must register the UniTalk2 application by typing the following
    into the Windows “start box” (obviously, you’ll need to qualify
    the name of the application with the name of the directory where
    the application resides):
  • <path>Unitalk2.exe -Regserver
  • You must register UniTalk2ps.dll
    (this is the proxy/stub) on the machines of each user as follows:
  • regsvr32 <path>UniTalk2ps.dll;
  • Place the IpAddress.txt user database in the same
    directory with Unitalk2.exe
  • You must edit the IpAddress.txt text file and add
    the names and IP addresses of those with which you wish to chat.
  • You must configure DCOM (using the DCOMCNFG.EXE application)
    as follows:
    • Default Properties tab

    • Check the Enable Distributed COM on this computer check box
    • In the Default Authentication Level combo box, select the None option
    • In the Default Impersonation Level combo box, select the Delegate option
      Default Security tab

    • On all Access, Launch and Configuration security, give “full control” to INTERACTIVE USER
      Application tab

    • Double click UniTalk2 or IVirtInterface
    • On the General tab, set the Authentication level to None
    • On the Location tab, check the “Run application on this computer…” check box
    • On the Security tab, set “full control” for the INTERACTIVE USER on Launch, Access and Configuration security
    • On the Identity tab, check the “The Interactive user” check box

You must so configure Dcomcnfg.exe on the client and server
machines.

Downloads


Download source code – 31 Kb
Download UniTalk2 application – 41 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read