skyrider_tieus
asked on
CSocket in ATL COM DLL!
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?
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?
Did you see http://support.microsoft.com/support/kb/articles/Q173/9/74.ASP ("HOWTO: Add MFC Support to an ATL Project")?
ASKER
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--
call GetLastError() in order to check what cause the error.
--EC--
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::CCommunicat or()
{
WSAData wsaData1;
int nResult;
nResult = WSAStartup (MAKEWORD(1,1),&wsaData1);
Create( NULL, CWindow::rcDefault, _T("Window"),
WS_OVERLAPPEDWINDOW|WS_VIS IBLE );
//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,size of(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(UIN T 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?
//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::CCommunicat
{
WSAData wsaData1;
int nResult;
nResult = WSAStartup (MAKEWORD(1,1),&wsaData1);
Create( NULL, CWindow::rcDefault, _T("Window"),
WS_OVERLAPPEDWINDOW|WS_VIS
//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
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(UIN
{
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?
sorry,the CCommunicator constructor should be after the class CCommunicator :public CWindoeImpl<CCommunicator> .
ASKER
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
My control is windowless. Is it necessary to create a Window handle in this CWindowImpl<CCommunicator>
Cheers :-)
James
ASKER
Increase the points to 300
skyrider_tieus,
Did yu try to call GetLastError()? If so, what error code do you get?
--EC--
Did yu try to call GetLastError()? If so, what error code do you get?
--EC--
ASKER
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_BU FFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSE RTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
AfxMessageBox((LPCTSTR)lpM sgBuf);
}
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
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_BU
FORMAT_MESSAGE_FROM_SYSTEM
FORMAT_MESSAGE_IGNORE_INSE
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
AfxMessageBox((LPCTSTR)lpM
}
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.