Hottest Forum Q&A on CodeGuru - November 17th


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 do I detect an Internet connection? (top)


In the column from 22nd September, I covered the problem from Jase Jennings. He was looking for a solution to determine whether he is connected to the Internet or not. Unfortunately, there was nobody to help him. All suggested solutions seems to fail for Jase.

  • The wininet functions are not reliable enough.
  • The Ras functions are not reliable enough.
  • The gethostbyname function is not reliable enough.

If you open IE, it instantly knows whether you are connected to the Internet or not. But how does IE recognize that? There must be a function that determines the connection. But where it is and what is the name of this function?

After reading all this, Mathew Joy suggested now to write a simple LSP. LSP—what is that? A LSP (Layered Service Provider) can log all functions that IE needs to connect to the Internet. Mathew has attached a simple LSP demo application that does just that. After running the demo app, we got the following sequence of functions called by IE via LSP.

  1. wspstartup
  2. wspbind
  3. wspgetsockname
  4. wspconnect
  5. wspselect

I am sure you are curious whether this method helped Jase to solve his problem. Take a look at the thread and you will know.

What is the correct way to read from a database? (top)


Well, this question was asked by me. A few days ago, I was working on a code in which I needed to read some data from a database. Actually, it's not a big deal, right? I thought the same. The problem was that the value stored in the database was in the currency format and I needed to show this value in a List control.

This is how the data is stored in the database:


And this the retrieved value:


And this is the code with which I was working:

CString csValue;
I also thought to convert the CString to double with the function strtod suggested by ergas, maybe this could solve my problem. But, unfortunately it didn't. After searching in the MSDN I came across the class CDBVariant. This class has several member variables that store the value from the database. In my case, it is a double. My code looks like this:

CDBVariant varValue;
// Database value:1.000.000,00
// Returned value: 12938672

Hmm.....what now? Why does it return such a strange value? I again did a search on MSDN but couldn't find anything. The fact is that 1000000.0000 is the same as 1.000.000,00. Only the format is not equal. So how to convert the 1000000.0000 to 1.000.000,00? Here, I got help from YvesM. He suggested that I use the GetCurrencyFormat() function and gave me the following code that worked perfectly. Here is the code:

TCHAR* CMyClass::ConvertStringToCurrency(CString csString)
    CString input = csString;
    int len = GetCurrencyFormat(LOCALE_USER_DEFAULT, 0, input,
                                0, 0, 0);

    TCHAR *buffer = new TCHAR[len + 1];
    memset(buffer, 0, sizeof(TCHAR) * (len + 1));

    GetCurrencyFormat(LOCALE_USER_DEFAULT, 0, input, 0, buffer,
                      len + 1);
    return buffer;

Thanks to all who helped me to solve this problem, especially to YvesM.

How can I end threads in a clean way? (top)


Marc from D has already solved his problem, but he is looking for suggestions to make it better. He wants to know a better way to close a thread than his one.

I already solved the problem, but I hope that you have a better 
My app uses at least one worker thread. When started, it handles 
the communication via the rs232-comport.

When the user wants to close the whole app., I first tell the 
worker-thread to close. The last thing it does before leaving 
is to set a flag. My main program in the meantime just polls in 
a loop whether that flag is set. When found, it closes also.

So, the worker thread sets the flag that it is closed already! 
I don't like my software to talk about future states of itself... 

The code is working, but I think it is ineffective.
Any suggestions?

Well, one solution could be to use the ExitThread(0) function. But, for the following reasons, you should avoid calling this function.

[Source=Programming Applications for Microsoft Windows by Richter]

This function terminates the thread and causes the operating system to clean up all of the operating system resources that were used by the thread. However, your C/C++ resources (such as C++ class objects) will not be destroyed. For this reason, it is much better to simply return from your thread function instead of calling ExitThread yourself.

The recommended way to have a thread terminate is by having its thread function simply return (as described in the previous section). However, if you use the method described in this section, be aware that the ExitThread function is the Windows function that kills a thread. If you are writing C/C++ code, you should never call ExitThread. Instead, you should use the Visual C++ runtime library function _endthreadex.

Note that calling ExitProcess or ExitThread causes a process or thread to die while inside a function. As far the operating system is concerned, this is fine and all of the process's or thread's operating system resources will be cleaned up perfectly. However, a C/C++ application should avoid calling these functions because the C/C++ runtime might not be able to clean up properly.

How do I define a class value STL deque? (top)


NYWalker can easily define a queue STL template, but unfortunately, he cannot define a class-based deque.

I'm novice in STL C++ librari, so my question will be very easy.
I have a structure, for example:
typedef struct _mystruct
    char name[256];
    bool sex;
    int age;
} mystruct, *pmystruct;
I can define a class based on queue STL template:
typedef queue<mystruct, list<mystruct> > MEMBERS_QUEUE;
        // This works perfectly
But I want also define a class based on deque:
typedef deque<mystruct, list<mystruct> > MEMBERS_DEQUE;
        // This doesn't work.
I suspect that I must define allocator for mystruct... 
What should I do to construct own class based on deque and

Well, the solution is simple—if you know it. Here is what Philip Nicoletti suggested:

when you use std::queue as follows:

typedef queue<mystruct, list<mystruct> > MEMBERS_QUEUE;

You are saying to use a list internally in the std::queue code. (By default, it uses a std::deque). A queue is a container adapter—it takes a standard container and restricts the operations that can be done on it (and gives the functions names that are more appropriate for a queue).

If you wanted the internal container in the queue to be a deque instead of a list, you would use:

typedef queue<mystruct, deque<mystruct> > MEMBERS_DEQUE;

or, since deque is used by default ...

typedef queue<mystruct> MEMBERS_DEQUE;

Why should I use the STL? (top)


In the column from 29th September, I covered the discussion started by darwen. Darwen is not a big fan of STL; he likes the MFC. In this thread, he discusses the postive parts of MFC, whereas people such as Paul McKenzie and others are showing the advantages of STL.

Although, as Andreas Masur already mentioned, this is a question like which operating system is better—Windows or Linux—I think it is interesting to know the advantages of STL as well as the disadvantages. Of course, on the other side, it's always good to know the MFC advantages and disadvantages. This discussion is still going on and I suggest you read the thread, because it is very informative.

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: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds