Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't make any TCP/IP connection from my ActiveX

Status
Not open for further replies.

43214007

Programmer
Mar 24, 2004
3
NL
This is puzzling my now for quite some time now. I can't seem to make any TCP/IP connection from my ActiveX component using the CAsyncSocket class. Somehow the CAsynSocket::Create() returns FALSE but I don't know why.

Even when I create a fresh new ActiveX component and add this code I can't even to create the socket.

CAsyncSocket Socket;
Socket.Create(); <- Returns false !?!?!?

Why does the Create() function fail when I use it from an ActiveX component?

This same code sure works when I use it from a .exe file.
 
Try to construct the object from the heap instead on the stack e.g. try:
Code:
CAsyncSocket* pSocket = new CAsyncSocket;
int iPort = 500;
pSocket->Create( iPort, SOCK_STREAM );
-obislavu-
 
I've tried to create the object in several ways. In my original ActiveX the CAsyncSocket object is a member variable of the COleControl and I create it on the heap.

I can trace into the mfc to see what happens. This MFC function returns FALSE:


BOOL CAsyncSocket::Create(UINT nSocketPort, int nSocketType,
long lEvent, LPCTSTR lpszSocketAddress)
{
if (Socket(nSocketType, lEvent))
{
if (Bind(nSocketPort,lpszSocketAddress))
return TRUE;
int nResult = GetLastError();
Close();
WSASetLastError(nResult);
}
return FALSE;
}
It never reaches the bind function.
 
I have been able to find the problem myself by remaining to search using google. It seems when you create a standard MFC-Exe project with the Classwizzard it automatically adds this code to the function InitInstance:

if (!AfxSocketInit())
{ AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}

However when you create a standard ActiveX it does not. This is why the socket cannot be created. Adding this code to my ActiveX was the solution to the problem.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top