Solved

assertion fail

Posted on 2003-11-16
1
498 Views
Last Modified: 2008-02-01
The following code when built (0 error, 0 warning) gives a message:
Debug Assertion failed! program C:\...\..exe, file: afxwin1.inl line:19

#include "stdafx.h"
#include <afxsock.h>
#include <afxwin.h>

in WinMain:
{
      CString serverName="mail.earthlink.net";
      UINT nPort=25;
       CSocket socket;
      AfxSocketInit();

      if(!socket.Create())return 1;
      if(!socket.Connect(serverName, nPort))return 1;
      return 0;
}

If I comment out the "create()" line, I don't get that message..

What's the problem?
0
Comment
Question by:samliam
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 9757937
Since you are using the MFC CSocket class, you should initialize MFC properly. You can learn this creating the console application with MFC support using Application Wizard.
Make the following changes:

#include "stdafx.h"
#include <afxsock.h>
#include <afxwin.h>

CWinApp theApp;       // add this line


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // Add these lines to WinMain start
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        return 1;
    }


    CString serverName="mail.earthlink.net";
    UINT nPort=25;
     CSocket socket;
    AfxSocketInit();

    if(!socket.Create())return 1;
    if(!socket.Connect(serverName, nPort))return 1;
    return 0;
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now