CInternetSession Debug Assertion Failed (Fixed)

I had completely forgotten about the Debug Assertion Failure when invoking the MFC CInternetSession from a console application.

I recently needed to teach an Old Dog a New Trick by performing an MFC FTP upload from a .NET (C#) console application.

When running the app in debug mode, I got the "Debug Assertion Failed" dialog box that requires an answer of Abort, Retry or Ignore.

I remembered (from long ago) this is a benign error and can just be ignored (if you want to press a button), but I didn't want any surprises and I wanted "clean" code.

In the rare instance someone else might run across this, here is the SHORT solution:

  1. Modify your header to include <afx.h>, <afxinet.h> and <afxwin.h>
  2. Add a module-level instance of CWinApp (yes, even in a console app).
  3. Set a value in your code for the variable afxCurrentAppName before invoking the CInternetSession


Here is the LONG solution

The error reported says there is a failure in the file afxwin1.inl at line 29.
After some disk searching, I found the file, afxwin1.inl, and saw the problem line 29:

...which simply checks to see if the variable afxCurrentAppName has a value.

afxCurrentAppName is an alias for AFX_MODULE_STATE::m_lpszCurrentAppName of type LPCTSTR in file afxstat_.h that is NOT automatically when making a console app (deeper explanation not given in this post).

Here's a test line of code that will cause the error:

Here is the dialog box showing the Debug Assertion Failure:

Here are the modifications to stop the error:
First, modify the header:

...then modify the code:

I have not run across any negative side-effects by doing this.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Friday, December 18, 2009 1:08 PM
Print

Comments on this entry:

# re: CInternetSession Debug Assertion Failed (Fixed)

Left by Derrick at 1/27/2010 10:09 AM
Gravatar
Thanks, this was helpful.

# re: CInternetSession Debug Assertion Failed (Fixed)

Left by Patrick at 9/9/2011 5:38 PM
Gravatar
Thank you!! This was VERY helpful and fixed my problem. You provided a much needed understanding of what and why this issue was happening.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910