About netstat.exe using MFC

how to write the netstat.exe (net statistics program) using MFC of visual C++ 6.0?
tttanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Point your browser to http://www.sysinternals.com/tcpview.htm 

The source code of 'netstatp' should give you all the necessary information on how to obtain the data, so just the GUI design is up to you ;-)
0
tttanAuthor Commented:
i have visited that site and i found that the source code was not wrote using MFC.
0
jkrCommented:
Well, that IMHO doesn't matter - it's C/C++, if you want to convert it to MFC, just add a

#include <StdAfx.h>

at the beginning...

As I said, it gives you the basic information on how to get the data.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

tttanAuthor Commented:
i have tried to do in that way but errors occured when i compilied the files.
0
tttanAuthor Commented:
maybe you can have a try on it. if success, then please inform me how to do it right.
0
jkrCommented:
Hmm, what errors did you get?
0
jkrCommented:
Ooops, I think I know where the problem is: The code uses 'printf()' to print the output to a console window - this is all you have to chenge. I'll post an adpted version later...
0
jkrCommented:
OK, change the example's 'main()' to this function:

int NetstatMain ( CStringList& lstOutput)
{
    char                    acBuf [ 1024];
    HANDLE                  hTrapEvent;
    AsnObjectIdentifier     hIdentifier;
    RFC1157VarBindList      bindList;
    RFC1157VarBind          bindEntry;
    UINT                    tcpidentifiers[] = { 1,3,6,1,2,1,6,13,1,1};
    UINT                    udpidentifiers[] = { 1,3,6,1,2,1,7,5,1,1};
    AsnInteger              errorStatus, errorIndex;
    TCPINFO                 *currentEntry, *newEntry;
    UINT                    currentIndex;
    WORD                    wVersionRequested;
    WSADATA                 wsaData;
    char                    localname[HOSTNAMELEN], remotename[HOSTNAMELEN];
    char                    remoteport[PORTNAMELEN], localport[PORTNAMELEN];
    char                    localaddr[ADDRESSLEN], remoteaddr[ADDRESSLEN];

    //
    // Initialize winsock
    //
    wVersionRequested = MAKEWORD( 1, 1 );
    if( WSAStartup(  wVersionRequested, &wsaData ) ) {
        return 1;
    }

    //
    // Locate and initialize INETMIB1
    //
    if( !LoadInetMibEntryPoints()) {

        return 1;
    }

    if( !SnmpExtensionInit( GetCurrentTime(), &hTrapEvent, &hIdentifier )) {
        return 1;
    }

    //
    // Initialize the query structure once
    //
    bindEntry.name.idLength = 0xA;
    bindEntry.name.ids = tcpidentifiers;
    bindList.list = &bindEntry;
    bindList.len  = 1;

    TcpInfoTable.prev = &TcpInfoTable;
    TcpInfoTable.next = &TcpInfoTable;

    //
    // Roll through TCP connections
    //
    currentIndex = 1;
    currentEntry = &TcpInfoTable;
    while(1) {

        if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
            &bindList, &errorStatus, &errorIndex )) {

            return 1;
        }

        //
        // Terminate when we're no longer seeing TCP information
        //
        if( bindEntry.name.idLength < 0xA ) break;

        //
        // Go back to start of table if we're reading info
        // about the next byte
        //
        if( currentIndex != bindEntry.name.ids[9] ) {

            currentEntry = TcpInfoTable.next;
            currentIndex = bindEntry.name.ids[9];
        }

        //
        // Build our TCP information table
        //
        switch( bindEntry.name.ids[9] ) {

        case 1:
           
            //
            // Always allocate a new structure
            //
            newEntry = (TCPINFO *) malloc( sizeof(TCPINFO ));
            newEntry->prev = currentEntry;
            newEntry->next = &TcpInfoTable;
            currentEntry->next = newEntry;
            currentEntry = newEntry;

            currentEntry->state = bindEntry.value.asnValue.number;
            break;

        case 2:

            currentEntry->localip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            currentEntry = currentEntry->next;
            break;

        case 3:
           
            currentEntry->localport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;

        case 4:

            currentEntry->remoteip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            currentEntry = currentEntry->next;
            break;

        case 5:

            currentEntry->remoteport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;
        }

    }
   
    //
    // Now print the connection information
    //
    sprintf(acBuf, "%7s %-30s %-30s %s\n", "Proto", "Local", "Remote", "State" );

    lstOutput.AddTail ( new CString ( acBuf));

    currentEntry = TcpInfoTable.next;
    while( currentEntry != &TcpInfoTable ) {

        sprintf( localaddr, "%s:%s",
            GetIpHostName( TRUE, currentEntry->localip, localname, HOSTNAMELEN),
            GetPortName( currentEntry->localport, "tcp", localport, PORTNAMELEN ));

        sprintf( remoteaddr, "%s:%s",
            GetIpHostName( FALSE, currentEntry->remoteip, remotename, HOSTNAMELEN),
            currentEntry->remoteip ?
                GetPortName( currentEntry->remoteport, "tcp", remoteport, PORTNAMELEN ):
                "0" );

        sprintf(acBuf,"%7s %-30s %-30s %s\n", "TCP",
            localaddr, remoteaddr,
            TcpState[currentEntry->state]);

        lstOutput.AddTail ( new CString ( acBuf));
       
        currentEntry = currentEntry->next;
    }

    //
    // Initialize the query structure once
    //
    bindEntry.name.idLength = 0xA;
    bindEntry.name.ids = udpidentifiers;
    bindList.list = &bindEntry;
    bindList.len  = 1;

    UdpInfoTable.prev = &UdpInfoTable;
    UdpInfoTable.next = &UdpInfoTable;

    //
    // Roll through UDP endpoints
    //
    currentIndex = 1;
    currentEntry = &UdpInfoTable;
    while(1) {

        if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
            &bindList, &errorStatus, &errorIndex )) {

            return 1;
        }

        //
        // Terminate when we're no longer seeing TCP information
        //
        if( bindEntry.name.idLength < 0xA ) break;

        //
        // Go back to start of table if we're reading info
        // about the next byte
        //
        if( currentIndex != bindEntry.name.ids[9] ) {

            currentEntry = UdpInfoTable.next;
            currentIndex = bindEntry.name.ids[9];
        }

        //
        // Build our TCP information table
        //
        switch( bindEntry.name.ids[9] ) {

        case 1:
           
            //
            // Always allocate a new structure
            //
            newEntry = (TCPINFO *) malloc( sizeof(TCPINFO ));
            newEntry->prev = currentEntry;
            newEntry->next = &UdpInfoTable;
            currentEntry->next = newEntry;
            currentEntry = newEntry;

            currentEntry->localip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            break;

        case 2:
           
            currentEntry->localport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;
        }
    }
   
    //
    // Now print the connection information
    //
    currentEntry = UdpInfoTable.next;
    while( currentEntry != &UdpInfoTable ) {

        sprintf(acBuf, "%7s %s:%s\n", "UDP",
                GetIpHostName( TRUE, currentEntry->localip, localname, HOSTNAMELEN),
                GetPortName( currentEntry->localport, "udp", localport, PORTNAMELEN ) );
       
        lstOutput.AddTail ( new CString ( acBuf));

        currentEntry = currentEntry->next;
    }
}

You'll basically have to pass in a CStringList object which will be filled with the output line by line.

Let me know if you still encounter problems.

(BTW: Your Q was 'How to write', not 'Please write for me' ;-)
0
tttanAuthor Commented:
is it possible to convert the program into class with CDialog as the base class.


0
jkrCommented:
Yes, of course. Simply create a MFC dialog based project and use the above function to populate a list control, e.g.

(in 'OnInitDialog()')

POSITION pos;
CStringList lstCtrl;
CString* pstrEntry;
int n = 0;
NetstatMain ( lstCtrl);

pos = lstCtrl.GetHeadPosition();

while ( pos)
{
 pstrEntry = lstCtrl.GetNext( pos);

 m_myListControl.InsetItem ( n++, pstrEntry->GetBuffer(0));
}

(just a sample)

0
tttanAuthor Commented:
could you send the complete program that you write using MFC with class CDialog.

besides, how to solve the problems below ?


G:\My Documents\Learn\Tcp\TcpDlg.cpp(134) : error C2440: '=' : cannot convert from 'void *' to 'int (__stdcall *)(unsigned long,void ** ,AsnObjectIdentifier *)'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast


G:\My Documents\Learn\Tcp\TcpDlg.cpp(140) : error C2440: '=' : cannot convert from 'void *' to 'int (__stdcall *)(unsigned char,SnmpVarBindList *,long *,long *)'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Error executing cl.exe.


The problems occurred from the program below:

BOOL (__stdcall *pSnmpExtensionInit)(
    IN  DWORD dwTimeZeroReference,
    OUT HANDLE *hPollForTrapEvent,
    OUT AsnObjectIdentifier *supportedView);

BOOL (__stdcall *pSnmpExtensionQuery)(
    IN BYTE requestType,
    IN OUT RFC1157VarBindList *variableBindings,
    OUT AsnInteger *errorStatus,
    OUT AsnInteger *errorIndex);


if( !(pSnmpExtensionInit = (void *)GetProcAddress( hInetLib,
      "SnmpExtensionInit" )) ) {
      return FALSE;
      }

if( !(pSnmpExtensionQuery = (void *)GetProcAddress( hInetLib,
      "SnmpExtensionQuery" )) ) {
      return FALSE;
      }

0
tttanAuthor Commented:
My email is tttan@pd.jaring.my
0
jkrCommented:
>>could you send the complete program that you write using
>>MFC with class CDialog

Well, I haven't written a complete program for that...

The problems you encountered are from the conversion from C to C++; to remedy this, we need a function pointer typecase, e.g.

typedef BOOL __stdcall ( *EXTINIT)( DWORD, HANDLE*, AsnObjectIdentifier*);

typedef BOOL __stdcall ( *EXTQUERY)( BYTE, RFC1157VarBindList*, AsnInteger*, AsnInteger*);

if( !(pSnmpExtensionInit = (EXTINIT)GetProcAddress( hInetLib,
"SnmpExtensionInit" )) ) {
return FALSE;
}

if( !(pSnmpExtensionQuery = (EXTQUERY)GetProcAddress( hInetLib,
"SnmpExtensionQuery" )) ) {
return FALSE;
}
0
tttanAuthor Commented:
could you give me your email address so that i can send the program for you.
0
jkrCommented:
I'll try to assemble en example when I'm back in the office on monday, as I don't have VC++ at home. You'll get my email address along with the sample - I hardly post it here...
0
tttanAuthor Commented:
thanks!
0
tttanAuthor Commented:
i have prepared to send you the source code of my program, but i need your email address. you could send your email to my account "tttan@pd.jaring.my"
thanks!
0
jkrCommented:
Ok, here it is:

// WNetstatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WNetstat.h"
#include "WNetstatDlg.h"
#include "snmp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define HOSTNAMELEN 256
#define PORTNAMELEN 256
#define ADDRESSLEN HOSTNAMELEN+PORTNAMELEN

typedef struct _tcpinfo {
      struct _tcpinfo            *prev;
      struct _tcpinfo            *next;
      UINT                        state;
      UINT                        localip;
      UINT                        localport;
      UINT                        remoteip;
      UINT                        remoteport;
} TCPINFO, *PTCPINFO;

typedef BOOL (__stdcall *PSEI)( DWORD, HANDLE*, AsnObjectIdentifier*);
typedef BOOL (__stdcall *PSEQ)( BYTE, RFC1157VarBindList*, AsnInteger*, AsnInteger*);


BOOL (__stdcall *SnmpExtensionInit)(
    IN  DWORD               dwTimeZeroReference,
    OUT HANDLE              *hPollForTrapEvent,
    OUT AsnObjectIdentifier *supportedView);

BOOL (__stdcall *SnmpExtensionQuery)(
    IN BYTE                   requestType,
    IN OUT RFC1157VarBindList *variableBindings,
    OUT AsnInteger            *errorStatus,
    OUT AsnInteger            *errorIndex);

static char TcpState[][32] = {
      "???",
      "CLOSED",
      "LISTENING",
      "SYN_SENT",
      "SEN_RECEIVED",
      "ESTABLISHED",
      "FIN_WAIT",
      "FIN_WAIT2",
      "CLOSE_WAIT",
      "CLOSING",
      "LAST_ACK",
      "TIME_WAIT"
};
      
TCPINFO            TcpInfoTable;
TCPINFO            UdpInfoTable;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
      CAboutDlg();

// Dialog Data
      //{{AFX_DATA(CAboutDlg)
      enum { IDD = IDD_ABOUTBOX };
      //}}AFX_DATA

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CAboutDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      //{{AFX_MSG(CAboutDlg)
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
      //{{AFX_DATA_INIT(CAboutDlg)
      //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CAboutDlg)
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
      //{{AFX_MSG_MAP(CAboutDlg)
            // No message handlers
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWNetstatDlg dialog

CWNetstatDlg::CWNetstatDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CWNetstatDlg::IDD, pParent)
{
      //{{AFX_DATA_INIT(CWNetstatDlg)
            // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWNetstatDlg::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CWNetstatDlg)
      DDX_Control(pDX, IDC_LIST1, m_output);
      //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWNetstatDlg, CDialog)
      //{{AFX_MSG_MAP(CWNetstatDlg)
      ON_WM_SYSCOMMAND()
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_BN_CLICKED(ID_UPDATE, OnUpdate)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWNetstatDlg message handlers

BOOL CWNetstatDlg::OnInitDialog()
{
      CDialog::OnInitDialog();

      // Add "About..." menu item to system menu.

      // IDM_ABOUTBOX must be in the system command range.
      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
      ASSERT(IDM_ABOUTBOX < 0xF000);

      CMenu* pSysMenu = GetSystemMenu(FALSE);
      if (pSysMenu != NULL)
      {
            CString strAboutMenu;
            strAboutMenu.LoadString(IDS_ABOUTBOX);
            if (!strAboutMenu.IsEmpty())
            {
                  pSysMenu->AppendMenu(MF_SEPARATOR);
                  pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
            }
      }

      // Set the icon for this dialog.  The framework does this automatically
      //  when the application's main window is not a dialog
      SetIcon(m_hIcon, TRUE);                  // Set big icon
      SetIcon(m_hIcon, FALSE);            // Set small icon
      
      // TODO: Add extra initialization here
      
      return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWNetstatDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
      if ((nID & 0xFFF0) == IDM_ABOUTBOX)
      {
            CAboutDlg dlgAbout;
            dlgAbout.DoModal();
      }
      else
      {
            CDialog::OnSysCommand(nID, lParam);
      }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWNetstatDlg::OnPaint()
{
      if (IsIconic())
      {
            CPaintDC dc(this); // device context for painting

            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
            CDialog::OnPaint();
      }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWNetstatDlg::OnQueryDragIcon()
{
      return (HCURSOR) m_hIcon;
}

void CWNetstatDlg::OnUpdate()
{
      // TODO: Add your control notification handler code here
      NetStat();
}

int CWNetstatDlg::NetStat()
{
       char                    acBuf [ 1024];
    HANDLE                  hTrapEvent;
    AsnObjectIdentifier     hIdentifier;
    RFC1157VarBindList      bindList;
    RFC1157VarBind          bindEntry;
    UINT                    tcpidentifiers[] = { 1,3,6,1,2,1,6,13,1,1};
    UINT                    udpidentifiers[] = { 1,3,6,1,2,1,7,5,1,1};
    AsnInteger              errorStatus, errorIndex;
    TCPINFO                 *currentEntry, *newEntry;
    UINT                    currentIndex;
    WORD                    wVersionRequested;
    WSADATA                 wsaData;
    char                    localname[HOSTNAMELEN], remotename[HOSTNAMELEN];
    char                    remoteport[PORTNAMELEN], localport[PORTNAMELEN];
    char                    localaddr[ADDRESSLEN], remoteaddr[ADDRESSLEN];

    //
    // Initialize winsock
    //
    wVersionRequested = MAKEWORD( 1, 1 );
    if( WSAStartup(  wVersionRequested, &wsaData ) ) {
        return 1;
    }

    //
    // Locate and initialize INETMIB1
    //
    if( !LoadInetMibEntryPoints()) {

        return 1;
    }

    if( !SnmpExtensionInit( GetCurrentTime(), &hTrapEvent, &hIdentifier )) {
        return 1;
    }

    //
    // Initialize the query structure once
    //
    bindEntry.name.idLength = 0xA;
    bindEntry.name.ids = tcpidentifiers;
    bindList.list = &bindEntry;
    bindList.len  = 1;

    TcpInfoTable.prev = &TcpInfoTable;
    TcpInfoTable.next = &TcpInfoTable;

    //
    // Roll through TCP connections
    //
    currentIndex = 1;
    currentEntry = &TcpInfoTable;
    while(1) {

        if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
            &bindList, &errorStatus, &errorIndex )) {

            return 1;
        }

        //
        // Terminate when we're no longer seeing TCP information
        //
        if( bindEntry.name.idLength < 0xA ) break;

        //
        // Go back to start of table if we're reading info
        // about the next byte
        //
        if( currentIndex != bindEntry.name.ids[9] ) {

            currentEntry = TcpInfoTable.next;
            currentIndex = bindEntry.name.ids[9];
        }

        //
        // Build our TCP information table
        //
        switch( bindEntry.name.ids[9] ) {

        case 1:
             
            //
            // Always allocate a new structure
            //
            newEntry = (TCPINFO *) malloc( sizeof(TCPINFO ));
            newEntry->prev = currentEntry;
            newEntry->next = &TcpInfoTable;
            currentEntry->next = newEntry;
            currentEntry = newEntry;
            currentEntry->state = bindEntry.value.asnValue.number;
            break;

        case 2:

            currentEntry->localip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            currentEntry = currentEntry->next;
            break;

        case 3:
             
            currentEntry->localport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;

        case 4:

            currentEntry->remoteip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            currentEntry = currentEntry->next;
            break;

        case 5:

            currentEntry->remoteport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;
        }

    }
     
    //
    // Now print the connection information
    //
    sprintf(acBuf, "%7s %-30s %-30s %s", "Proto", "Local", "Remote", "State" );
      m_output.AddString(acBuf);

    currentEntry = TcpInfoTable.next;
    while( currentEntry != &TcpInfoTable ) {

        sprintf( localaddr, "%s:%s",
            GetIpHostName( TRUE, currentEntry->localip, localname, HOSTNAMELEN),
            GetPortName( currentEntry->localport, "tcp", localport, PORTNAMELEN ));

        sprintf( remoteaddr, "%s:%s",
            GetIpHostName( FALSE, currentEntry->remoteip, remotename, HOSTNAMELEN),
            currentEntry->remoteip ?
                GetPortName( currentEntry->remoteport, "tcp", remoteport, PORTNAMELEN ):
                "0" );

        sprintf(acBuf,"%7s %-30s %-30s %s", "TCP",
            localaddr, remoteaddr,
            TcpState[currentEntry->state]);
            m_output.AddString(acBuf);

        currentEntry = currentEntry->next;
    }

    //
    // Initialize the query structure once
    //
    bindEntry.name.idLength = 0xA;
    bindEntry.name.ids = udpidentifiers;
    bindList.list = &bindEntry;
    bindList.len  = 1;

    UdpInfoTable.prev = &UdpInfoTable;
    UdpInfoTable.next = &UdpInfoTable;

    //
    // Roll through UDP endpoints
    //
    currentIndex = 1;
    currentEntry = &UdpInfoTable;
    while(1) {

        if( !SnmpExtensionQuery( ASN_RFC1157_GETNEXTREQUEST,
            &bindList, &errorStatus, &errorIndex )) {

            return 1;
        }

        //
        // Terminate when we're no longer seeing TCP information
        //
        if( bindEntry.name.idLength < 0xA ) break;

        //
        // Go back to start of table if we're reading info
        // about the next byte
        //
        if( currentIndex != bindEntry.name.ids[9] ) {

            currentEntry = UdpInfoTable.next;
            currentIndex = bindEntry.name.ids[9];
        }

        //
        // Build our TCP information table
        //
        switch( bindEntry.name.ids[9] ) {

        case 1:
             
            //
            // Always allocate a new structure
            //
            newEntry = (TCPINFO *) malloc( sizeof(TCPINFO ));
            newEntry->prev = currentEntry;
            newEntry->next = &UdpInfoTable;
            currentEntry->next = newEntry;
            currentEntry = newEntry;

            currentEntry->localip =
                *(UINT *) bindEntry.value.asnValue.address.stream;
            break;

        case 2:
             
            currentEntry->localport =
                bindEntry.value.asnValue.number;
            currentEntry = currentEntry->next;
            break;
        }
    }
     
    //
    // Now print the connection information
    //
    currentEntry = UdpInfoTable.next;
    while( currentEntry != &UdpInfoTable ) {

        sprintf(acBuf, "%7s %s:%s", "UDP",
                GetIpHostName( TRUE, currentEntry->localip, localname, HOSTNAMELEN),
                GetPortName( currentEntry->localport, "udp", localport, PORTNAMELEN ) );
            m_output.AddString(acBuf);
         
        currentEntry = currentEntry->next;
    }
      return 0;
}

char *CWNetstatDlg::GetPortName(UINT port, char *proto, char *name, int namelen)
{
      struct servent *psrvent;

      if( psrvent = getservbyport( htons( (USHORT) port ), proto )) {

            strcpy( name, psrvent->s_name );

      } else {

            sprintf(name, "%d", port);

      }            
      return name;
}

char *CWNetstatDlg::GetIpHostName(BOOL local, UINT ipaddr, char *name, int namelen)
{
      struct hostent                  *phostent;
      UINT                              nipaddr;

      nipaddr = htonl( ipaddr );
      if( !ipaddr  ) {

            if( !local ) {

                  sprintf( name, "%d.%d.%d.%d",
                        (nipaddr >> 24) & 0xFF,
                        (nipaddr >> 16) & 0xFF,
                        (nipaddr >> 8) & 0xFF,
                        (nipaddr) & 0xFF);

            } else {

                  gethostname(name, namelen);
            }

      } else if( ipaddr == 0x0100007f ) {

            if( local ) {

                  gethostname(name, namelen);
            } else {

                  strcpy( name, "localhost" );
            }

      } else if( phostent = gethostbyaddr( (char *) &ipaddr,
            sizeof( nipaddr ), PF_INET )) {

            strcpy( name, phostent->h_name );

      } else {

            sprintf( name, "%d.%d.%d.%d",
                  (nipaddr >> 24) & 0xFF,
                  (nipaddr >> 16) & 0xFF,
                  (nipaddr >> 8) & 0xFF,
                  (nipaddr) & 0xFF);
      }
      return name;
}

BOOL CWNetstatDlg::LoadInetMibEntryPoints()
{
      HINSTANCE      hInetLib;

      if( !(hInetLib = LoadLibrary( "inetmib1.dll" ))) {

            return FALSE;
      }

      if( !(SnmpExtensionInit = (PSEI)GetProcAddress( hInetLib,
                  "SnmpExtensionInit" )) ) {

            return FALSE;
      }

      if( !(SnmpExtensionQuery = (PSEQ)GetProcAddress( hInetLib,
                  "SnmpExtensionQuery" )) ) {

            return FALSE;
      }
      return TRUE;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Ooops, BTW: Excuse the delay (again), but there's a lot of work to do here...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.