Hottest Forum Q&A on CodeGuru - September 22nd


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

How to detect an Internet connection? (top)


Jase Jennings is looking for a solution to determine whether he is connected to the Internet or not. Sounds simple, doesn't it? Well, it only sounds simple.

My application connects to the internet to check for updates.

It does so using functions in ras32.dll (RasGetConnectStatus)
If ras32.dll does not exist, I use win inet functions

Two problems:

1. If not connected, the wininet functions take a while to time out.
I always call it in a thread which i timeout after 5 seconds,
but still it's not perfect.

2. RAS functions only work when an internet dialup is present.
If you have cable modem / router / lan, RAS will always return
"not connected", even if you are.

I read a newsgroup posting from a microsoft guy who suggested also
using IsNetworkAlive() and IsDestinationReachable(), but both of
those fail for me, regardless of connection state.

I have writtten a class which works well for dialup connection.
But in the case of "always on" connections, I have yet to find a
good method for detection of internet availability.

Any suggestions guys?

So, what can be done here??
Well, there are several possibilities that determine an Internet connection. Kochhar suggested the first one. Here is what his suggested code looks like:

BOOL Online()
    DWORD dwState = 0; 
    DWORD dwSize = sizeof(DWORD);
      return InternetQueryOption(NULL,
        && (dwState & INTERNET_STATE_CONNECTED);

Although the code seems to work for Kochhar, it does not work for Jase. But why? The problem is that Jase is using a router/LAN to connect, whereas Kochhar uses a dialup connection.

The second solution is suggested by Mick_2002. He wants to use a ping to check the connectivity. Although the solution should work without having problems, it does again not work for Jase. The problem is that the company in which Jase works uses a firewall that does not allow any ICMP connections.

Unfortunately, Jase still got no solution for his problem. Take a look at his thread; maybe you know a solution.

How to display Arabic characters in a Richedit control? (top)


Memeteau wants to know how to use arabic characters in a CRichEditCtrl in a unicode application.

I am desesperatly trying to display an arabic word in a
CRichEditCtrl (in a unicode application). I manage to paste an
arabic word from a pdf file to a CRichEditCtrl, but when I try to
copy (from code) that string to another CRichEditCtrl, i only get
I tried to set the charformat of the RichEditCtrl to
ARABIC_CHARSET, but it seems to be ignored and forced to

MFC Automatically uses RichEdit 1.0. Unless you use a RichEdit 2.0, 3.0, or 4.0, you will not have true Unicode support by the rich edit. If it is a CRichEditCtrl that you added in a dialog box, you should change the properties in the resource file manually: Change the class name from "RICHEDIT" to "RichEdit20W" for RichEdit 2.0 (with Unicode support).

What's the difference between BeginPaint() and GetDC()? (top)


SilentJackqh asked a very interesting question. He want to know the difference between both techniques.

I usually used GetDC(), never used BeginPaint() to paint something
in WM_PAINT. But what's the different and which one is faster?

BeginPaint() does a little more than GetDC(). For instance, it automatically validates the regions that have been invalidated. Because it sets up clipping regions so that you won't paint in areas that are not invalidated, BeginPaint() will often be faster than GetDC(). In Response to WM_PAINT always call BeginPaint().

GetDC() can be used when painting but not in response to a WM_PAINT message. For instance, if you set up a timer to animate a sprite or something, you have to use GetDC(). After painting with a common DC, always call ReleaseDC to release the DC.

How to change an application's name at run-time? (top)


Haim B is looking for a way to change the application name and icon at run-time.

I know that I can change MFC application name by cahnging
IDR_MAINFRAME/AFX_IDS_APP_TITLE. But I need to change that value
in run-time. Any ideas?

Changing the application name is not a big deal in VC++. myth7676 provided the needed code for that.

// put this in the InitInstance of your app before the return TRUE
// First free the string allocated by MFC at CWinApp startup.
// The string is allocated before InitInstance is called.


// Change the name of the application file.
// The CWinApp destructor will free the memory.

m_pszAppName = _tcsdup(_T("MyApp's New Name"));

But how should Haim B change the icon of his application? Do you know how?

Well, the solution, provided by Sam Hobbs, is to use version resources. You don't need to recompile the entire application. If you can use the version resource data for all data that changes, you only need to recompile the resources and then re-link. The icon can also be updated in the resources, but not the version resources.

You don't even need to do that. There is a Windows API for updating resource data in a EXE file without even re-linking and there are various utilities written that use it.

How to launch the default player for an audio CD? (top)


Stephens has developed an an autorun app for a Data + Music CD-Extra CD, and wants to include a "Play CD" button on it, which simply launches the default audio CD player.

I am trying figure out how I can get my app to launch the default
audio CD player, with a CD Extra (i.e., audio + data) in the
CD drive. Oh, and I'm trying to avoid playing with the registry!!

I know (thanks to a post by Ondrak in Nov 2000) that I could do it
by parsing the HKEY_CLASSES_ROOT/AudioCD/shell/play/command
registry entry to get the default AudioCD program, substituting
parameters, and calling ShellExecute ... but that's way too ugly
for my liking.

I feel like I SHOULD be able to do it with a more simple
ShellExecute call, but I can't for the life of me figure out how.
If I try to "open" the drive, it simply brings up Explorer.
If I try to "open" the first track (i.e. <drive>:\\Track01.cda),
nothing happens (and I can't see the tracks in explorer anyway).
"play" isn't a valid verb.

Any thoughts???

The problem with a simple ShellExecute() is that it does only work for a normal CD not for a CD Extra (audio + data). So, the following code does not work.

ShellExecute(NULL, "open", "<drive>:\\Track01.cda", NULL, NULL,

The solution is to use ShellExecuteEx(), the big brother of ShellExecute. ShellExecuteEx() takes a structure of LPSHELLEXECUTEINFO as a parameter. In this parameter, you need to specify the following:

lpClass = _T("AudioCD")
lpVerb  = _T("play")

This simply launches the "play" command for the default audio CD player. Currently, this is the only solution that works for CD Extra format CDs.

What is aim of Microsoft to convert int into INT_PTR? (top)

the one works with the .NET 2002 SDK and encountered that the return type of CList::GetCount() returns INT_PTR. But what kind of datatype is that??

The new SDK changes some return types from 'int' to 'INT_PTR'.
e.g. CList::GetCount(). My some files are old, and now they give
many 'compile-warnings: truncation from int to INT_PTR'.

'int' types were more meaningful for such functions like GetCount().

Is it a big problem to keep them still 'int'? or
What is aim of Microsoft to convert them into 'INT_PTR' ?

Microsoft designed the INT_PTR to keep the 64-bit platform in mind. So, if you want to port your code to a 64-bit platform, it will be very useful. You can ignore the warnings, as they are harmless, but if you don't feel comfortable with those, you can remove them by going to the Properties of your project and in the C/C++ section, select NO for the "Detect 64-bit portability issues" option.

How to derive a class from ofstream? (top)

SteveMurphy needs to know how to derive the ofstream in VC++. Tell me, guys, how would you accomplish this task? Well, using the class wizard to derive the class would be enough, right? SteveMurphy tried the same, but unfortunately, this does not work.

From within VC++ (6.0) I want to derive a class from ofstream.
I have an existing project and I used the class wizard to add a
class. After displaying the New Class dialog box, here are my
actions and the results:
1) I chose Generic Class from the Class Type list.
2) I entered my class name (COutputFile) in the Name Field.
3) I entered ofstream in the Derived From field, and click Ok, and
I got the following message:

"The New Class Wizard could not find the appropriate header file(s)
to include for the base class(es) of ofstream. If you choose to
derive from the class(es) anyway, you may need to manually add the
appropriate header file(s) to <file name>."

I got the same results when trying to derive my class from other
stream classes like ios, iostream, ostrstream. Can anyone tell me
what I'm doing wrong? Or is it just not possible to do this?

galathaea is a regular contributor on CodeGuru, who provided the perfect answer. The typename ofstream is not an actual class. It is a helper typedef alias of basic_ofstream<char, char_traits<char> >. You cannot inherit from an alias, and because there is no class of that name, the wizard is correct. Instead, derive from the basic_ofstream class with the proper template parameters. You should be careful that you understand the basic semantics of such an inheritance, such as what to pass to the base constructor, whether or not some of the methods should be implemented, the existence or nonexistence of copyability, and so forth. But, luckily, basic_ofstream is designed to allow it to be extended through inheritance, so it has the necessary structure (virtual dtors, and so on).

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds