We help IT Professionals succeed at work.

CSocket in ATL COM DLL!

Medium Priority
701 Views
Last Modified: 2013-12-14
I created a full control object in the ATL project with MFC supported, and a MFC's CSocket was inserted as a listening socket.  I had trouble creating the CSocket object in ATL COM STDMETHODIMP method,and no error occurred.  

m_pSocket->Create(<PORT NUMBER>) would return FALSE.

An AfxMessageBox was incorporated in the code to display the result.  Can CSocket be used in ATL COM in-proc server? Or I made some mistake?

 
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Did you see http://support.microsoft.com/support/kb/articles/Q173/9/74.ASP ("HOWTO: Add MFC Support to an ATL Project")?

Author

Commented:
I'm using VC++ 6, and the MFC Support checkbox was selected when the ATL project was created.  Actually I use a MFC function in ATL STDMETHODIMP method, AfxMessageBox, which works fine.  
>>m_pSocket->Create(<PORT NUMBER>) would return FALSE.

call GetLastError() in order to check what cause the error.

--EC--

Commented:
I suggest you use the fun in winsock2.h,my project is about communicator ,I do:
//in you ATL project
//in stdafx.h you must include
#include <windows.h>
#include <atlbase.h>
#include <atlwin.h>
#include <atlcom.h>
#define WM_CONNECT 6001

//in your Communicator.h
#include "winsock2.h"
CCommunicator::CCommunicator()
{
WSAData wsaData1;
int nResult;
nResult = WSAStartup (MAKEWORD(1,1),&wsaData1);
Create( NULL, CWindow::rcDefault, _T("Window"),
               WS_OVERLAPPEDWINDOW|WS_VISIBLE );
//create the window ,this will set m_hWnd
}

class CCommunicator : public CWindowImpl<CCommunicator>  
{
    public:
     SOCKET m_sock;
     CCommunicator();
     virtual ~CCommunicator();
     ////////////////////////////////////
public:
     LRESULT Analyse(UINT uMsg, WPARAM wParam, LPARAM  
                        lParam,BOOL& bHandled);
     LRESULT Connect();
     BEGIN_MSG_MAP( CMcbWindow )
          MESSAGE_HANDLER( WM_CONNECT, Analyse)
     END_MSG_MAP()

}
class CCommunicator::Connect()
{
 int nResult;
 m_sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
 struct     sockaddr_in you;
     you.sin_family = AF_INET;
     you.sin_addr.s_addr = inet_addr("202.220.96.195");
     you.sin_port = htons(20001);
////////////////////////////////////////////////
 nResult = connect(m_sock,(LPSOCKADDR)&you,sizeof(you));
 WSAAsyncSelect( m_sock , this->m_hWnd , 0, 0 );
 nResult = WSAAsyncSelect( m_sock,this->m_hWnd ,
         WM_CONNECT, D_READ|FD_WRITE|FD_CLOSE);
return 0;
}

LRESULT CCommunicator::Analyse(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
{
     int nFlags = LOWORD(wParam);
          int nError = HIWORD(lParam);
          int nMessage = LOWORD(lParam);
          if(nMessage == FD_WRITE)
          {
             return 0;
          }
          if(nMessage == FD_CLOSE)
          {
             return 0;
          }
          if( nMessage == FD_READ)
               return 0;//recv the data
          return 0;
}

//you must append ws2_32.lib in setting

how about?

Commented:
sorry,the CCommunicator constructor should be after the class CCommunicator :public CWindoeImpl<CCommunicator>.

Author

Commented:
cococut:

My control is windowless.  Is it necessary to create a Window handle in this CWindowImpl<CCommunicator>?  Can this CCommunicator sending and receiving char array data with other winsock?  Can it fire events from within?  This is a new and unknown territory for me.

Cheers :-)

James  

Author

Commented:
Increase the points to 300
skyrider_tieus,
Did yu try to call GetLastError()? If so, what error code do you get?

--EC--

Author

Commented:
elcaptitan:

I tried GetLastError() function.  It was rather funny.  My code like this

if (m_pSocket->Create(<PORT NUMBER>)
    m_pSocket->listen();
else {
 
  PVOID lpMsgBuf;
  FormatMessage(
  FORMAT_MESSAGE_ALLOCATE_BUFFER |
  FORMAT_MESSAGE_FROM_SYSTEM |
  FORMAT_MESSAGE_IGNORE_INSERTS,
   NULL,
   GetLastError(),
   0, // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL
   );

   AfxMessageBox((LPCTSTR)lpMsgBuf);
 
}  

The Message said "The operation has completed successfully" despite the failing of creating socket.  If I add more codes  
m_pSocket->listen();
and then GetLastError(),  it would say the Either the application did not call WSAStartup or WSAStartup fails.
It seems that in this case GetLastError() wouldn't catch the exception.

Cheers!

James



Author

Commented:
Guys:

I found the answer to my question.  Need to include AfxSocketInit() in the project's InitInstance().
This solves the problem.  I'll move this question to PAQ.

thanks for all your participation.

James
Returning points and paqqing. Skyrider_tieus, thank you for posting your solution.

PandoraMod
Moderator @ Experts Exchange
e-mail: pandoramod@experts-exchange.com

Explore More ContentExplore courses, solutions, and other research materials related to this topic.