April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Hottest Forum Q&A on CodeGuru

  • November 4, 2003
  • By Sonu Kapoor
  • Send Email »
  • More Articles »


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 can I get the current traffic bandwidth of my computer? (top)


George2 wants to know the current bandwidth that his system his receiving or sending.

Hello, everyone!
I want to get the current bandwidth (input and output respectively)
of a Windows system, for example, 20.2kB for input and 30.1kB for

Where can I find some sample codes?

Well, the solution is to use the PDH Interface. This interface provides the necessary information to get the total bandwidth. Here is some sample code:

#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <pdh.h>

#pragma comment(lib,"pdh.lib")

int main(int argc, char* argv[])
    PDH_STATUS           pdhStatus = ERROR_SUCCESS;
    LPTSTR               szCounterListBuffer = NULL;
    DWORD                dwCounterListSize  = 0;
    LPTSTR               szInstanceListBuffer = NULL;
    DWORD                dwInstanceListSize  = 0;
    HQUERY               hQuery;
    HCOUNTER             hCounter;
    DWORD                dwType = 0;
    char                 szCounter[256] = {0};

    pdhStatus = PdhEnumObjectItems (NULL,NULL,
                                    "Network Interface",

    szCounterListBuffer = (LPTSTR)malloc ((dwCounterListSize *
                                           sizeof (char)));
    szInstanceListBuffer = (LPTSTR)malloc ((dwInstanceListSize *
                                            sizeof (char)));

    if(!szCounterListBuffer || !szInstanceListBuffer)
        printf ("unable to allocate buffer\n");
        return 1;

    pdhStatus = PdhEnumObjectItems (NULL,NULL,
                                    "Network Interface",

    if(pdhStatus == ERROR_SUCCESS) 
        sprintf(szCounter,"\\Network Interface(%s)
                           \\Current Bandwidth",

        printf ("unable to allocate buffer\n");
        return 1;

    if( PdhOpenQuery(NULL ,0 ,&hQuery))
        printf("PdhOpenQuery failed\n");
        return 1;

    if( PdhAddCounter(hQuery,szCounter,NULL,&hCounter))
        printf("PdhAddCounter failed\n");
        return 1;

            printf("PdhCollectQueryData failed\n");

        if( PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG ,
            &dwType, &value))
            printf("PdhGetFormattedCounterValue failed\n");

        printf("Current Bandwidth : %ld\r",value.longValue);

    if (szCounterListBuffer != NULL) 
        free (szCounterListBuffer);

    if (szInstanceListBuffer != NULL) 
        free (szInstanceListBuffer);


    return 0;

How can I run one executable inside another's client window space? (top)


scott meyer asked a very interesting question and I was curious whether or not this is possible. He needs to run an application in another application's window.

Is it possible to run an application inside another
application's window space?

For example, if I have fred.exe and I want to have it appear
to run as a child window of wilma.exe, an MDI app, is this possible?

One caveat - I write wilma.exe, but fred.exe is someone else's app
and I can't modify it.

What I'm trying to do is host another application's instance inside
my application.

Do you know any possibilities about how to do this?

OReubens sees the possiblities and explains it in detail.

Wilma is going to be picky about what kind of app fred is...

If Fred is a console app, it should be fairly easy for wilma to use a pipe to capture the output from fred and display it in her own (console) window. This is, after all, a bit like how MSDev displays the output of the compiler/linker.

If Fred is a GUI program, it's still somewhat possible, although it'll require LOTS of work becausee you'll have to trap all window creation and window messages, and pass them along from wilma to fred. This is recommended only for ultra advanced programmers, though. I started something along the lines of this myself a looong time ago, but was only able to get partial support for it working.

Generally, you can safely safe that if fred doesn't provide for having itself run in a client window of another app (typically, this would be done via OLE/Com/ActiveX), the effort for making it do that anyway is probably not going to be worth it.

How do I serialize a CMap? (top)


martenbengtsson needs to store and load a CMap. He wants to use SerializeElements, but is not sure about it. Here is his comment.

I am trying to save and load a CMap. I thought I could just call
the SerializeElements. But everybody seams to overload the
SerializeElements why is that necessesary? I have not seen one
single example of a call to the function.
SerializeElements takes the size of the CMap as input argument.
But how do I know the size of the CMap when I load a CMap from a
void CGsdoc_b1Doc::Serialize(CArchive& ar)
int size = m_UserMap.GetCount();
if (ar.IsStoring())
    // TODO: add storing code here
    SerializeElements ( ar, &m_UserMap , size);

The trick is to override the SerializeElements in the Document implementaion. The SerializeElements function is called like the above in the Serialize function in the Document. It goes something like this:

void AFXAPI SerializeElements  ( CArchive& ar, CUser* pNewUser,
                                 int nCount )
       for ( int i = 0; i < nCount; i++, pNewUser++ )
            // Serialize each CPerson object
           pNewUser->Serialize( ar );

Also take a look at the MSDN article, which explains the topic pretty well.

How do I initialize the size of a char*[]? (top)


waverdr9 is working on a shell application that accepts a char *arg[]. He has set up the size as char *arg[20] but unfortunaly it always returns the size of 0.

I am writing a program that acts as a simple shell.
Basically, my program outputs a prompt, then waits for the user to
input a command. My program then handles that command and ouputs a
new prompt. This process continues until the user quits. The
specifics of the program are not relevant though. The only question
I have is with the variable char *arg[] inside of my Info struct.
I am not sure how to initialize it's size. I tried just saying
char *arg[20], but that ended up giving it a size of 0 for some
reason. Can anyone correct this problem in my code for me?
Can anyone tell me how to initialize the size of a char*[]?
I have the entire program below, or you can download the
attachment of it.

waverdr9 has also put the cpp in his thread. Unfortunaly, it's too large to show it here in the column. I suggest that you download the file. The problem is that if you only input one word (ex: "ls"), it works. But if you input more than one word, it goes out of bounds of the array. So, the problem is that there you have not allocated any space for the char*. There is no new[] or malloc to create this space. Initialize the char* to something like this and it will work.

argv[0] = new char [SomeLength];
delete [] argv[0];

What is the best way to create a CEdit variable? (top)


dineshns wants to know the correct way to create a member varaible for CEdit. Although this might be a simple question, I think such questions are very important.

Assume that a dialog Box has a Edit Control. To access the
edit control we should define a member variable. What is the best
way of definning Member variable?
CEdit* m_pedtName;    // Pointer
CEdit m_edtName;

The solution is simple. Use the second one. But why?

Pointers always introduce the possibility of memory leaks, invalid addreses, and such. There is basically no need to use them for the controls.

There might be really rare cases where you would use a pointer to controls for a good reason, in my eyes. Besides that, if you assign these member variables via the class wizard....then you only have one choice, anyway.

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Sitemap | Contact Us

Rocket Fuel