Encapsulating Standard Network API Functions

Environment: NT/W2K

Getting to grips with the standard 32-bit network API calls is a pain to begin with, which is why I wrote this small class to encapsulate two of the network functions I most use: NetSessionEnum() and NetUserGetInfo(). Anyone picking up this code can easily extend its functionality to take in other calls such as NetShareAdd() or NetUserAdd() etc. I will probably do the same when I have cause to use them.

In my development environment, the source files sit in a folder dedicated to various utility classes and are included in the Visual C++ workspace for each project for which I require them via Project->Add To Project->Files and then including the class header where necessary.

Source Code

The comments in the source code are (I hope) self-explanatory.

Header File


/*
NETUTIL.H
Net utilities class header

Michael Pritchard Mar 2001

*/


// 32-bit net API headers
// include them here so the whole application
// can see them
#include <lmcons.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <lmaccess.h> 
#include <lmshare.h>

// just a big number to allow a large enough
// buffer for the return from NetSessionEnum()
// because I'm too lazy to call the function twice
#define bigbuff 64000	

class CNetutil {

public:

	// prototypes of member functions
	BYTE* get_sessions(CString, DWORD&, DWORD&, DWORD&);
	PUSER_INFO_10 get_name(CString, CString, DWORD&);

protected:

private:


};

Implementation File


/*
NETUTIL.CPP
Net utilities class source file

Michael Pritchard Mar 2001

This class encapsulates 32-bit network API calls.

Windows NT/W2K only.

Project must be linked with NETAPI32.LIB.

Build with precompiled header use set to automatic.

This class requires the application to be built in unicode. If you
don't do it this way, you must covert strings to unicode before
applying them to the net API functions and convert the results
back. To build an MFC application in Unicode, you first need to 
make sure you have installed Visual Studio with the relevant
Unicode libraries. If you haven't, you'll need to re-install. Once
you have done this, create your project via the App Wizard as normal
and then change the project settings as follows:

Under Project/Settings/C++/General change the preprocessor directive from
(Debug)
WIN32,_DEBUG,_WINDOWS,_MBCS
to
WIN32,_DEBUG,_WINDOWS,_UNICODE
or (Release)
WIN32,NDEBUG,_WINDOWS,_MBCS
to
WIN32,NDEBUG,_WINDOWS,_UNICODE

If the strings don't look exactly like this, don't panic - all you
need to do is change _MBCS to _UNICODE

Under Project/Settings/Link/Output add
wWinMainCRTStartup
to the entry point symbol box

If you are converting an existing project to Unicode, you will
probably need to change some functions over to the Unicode
versions. This is straight forward and there is plenty of
information in MSDN.

All string literals need to be encapsulated in the _T() macro.
*/


#include "afx.h"
#include "NetUtil.h"     // don't forget this, either

/*
member function: get_sessions()

Uses API:    NetSessionEnum()

Arguments:   server -     name of server on which user database is held
                          (normally PDC), no preceeding backslashes required
             dwErrmsg   - reference to DWORD to contain return code from API call
             dwRecords  - reference to DWORD to contain no. records returned
                          by API call
             dwSessions - reference to DWORD to contain total sessions returned
                          by API call

Returns:     BYTE pointer to buffer returned by API call

Sample call to get_sessions():

void CMyClass::MyFunction()
{
     CNetutil net;
     CString sServer = _T("myPDC");

     DWORD dwError, dwRecords, dwSessions;
     BYTE* buffer;
     PSESSION_INFO_10 pSession;

     buffer = net.get_sessions(sServer, dwError, dwRecords, dwSessions);

     if( dwError != 0)
     {
          CString sError;
          sError.Format(_T("get_sessions() failed - error %ld"), dwError);
          AfxMessageBox(sError);
          return;
     }

     // cast returned buffer to structure
     pSession = (PSESSION_INFO_10 )buffer;

     // move through the BYTE buffer, advancing the pointer
     // by length PSESSION_INFO_10 each time
     for (DWORD dwCount = 0; dwCount++ < dwRecords; pSession++)
     {
          // do something with each instance of the PSESSION_INFO_10 structure
          // e.g. CString sUserid = pSession->sesi10_username;
     }

     NetApiBufferFree(buffer);
}

Notes:     Before processing returned BYTE pointer, value of dwErrmsg MUST be checked.
           A return of 0 indicates net API call successful, anything else must be
           noted and checked against the error code tables

*/

BYTE* CNetutil::get_sessions(CString server, 
                             DWORD& dwErrmsg, 
                             DWORD& dwRecords, 
                             DWORD& dwSessions)
{
     // buffer to take return from net API call
     BYTE *pBuf;          
     
     // Build full UNC name for PDC
     CString sSer;          
     sSer = _T("\\\\");
     sSer += server;
     
     // call net function level 10 (general access)
     dwErrmsg = NetSessionEnum (sSer.GetBuffer(0),
                                NULL,
                                NULL,
                                10,
                                &pBuf,
                                bigbuff,     // see header 
                                &dwRecords,
                                &dwSessions,
                                NULL);

     // if return sucessful, set dwErrmsg to zero
     // (confines net function error checking to this class)
     if (dwErrmsg == NERR_Success || dwErrmsg == ERROR_MORE_DATA)
          dwErrmsg = 0;

     return pBuf;


}


/*
member function: get_name()

Uses API:     NetUserGetInfo()

Arguments:    userid  -  individual user ID, or one of buffer returned 
                         by NetSessionEnum()
              server  -  name of server on which user database is held
                         (normally PDC) no preceeding backslashes required
              dwErrmsg - pointer to int to contain return code from API call

Returns:      pointer to PUSER_INFO_10 structure

Sample call to get_name():

void CMyClass::MyFunction()
{
     DWORD dwError;
     CNetutil net;
     CString sServer = _T("MyPDC");
     CString sUserid = _T("AnyValidUserid");

     PUSER_INFO_10 pUser;
     pUser = net.get_name(sUserid, sServer, dwError);

     if(dwError == 0)
     {
          // do something with the PUSER_INFO_10 structure
          // e.g. CString sName = pUser->usri10_full_name;
     }
     else
     {
          CString sError;
          sError.Format(_T("%ld"), dwError);
          AfxMessageBox(sError);
     }
}

Notes:      Before processing returned PUSER_INFO_10 pointer, value of dwErrmsg 
            MUST be checked. A return of 0 indicates net API call successful, 
            anything else must be noted and checked against the error code tables

*/

PUSER_INFO_10 CNetutil::get_name(CString sUserid, 
                                 CString server,
                                 DWORD& dwErrmsg) 
{

     // buffer to take return from net API call
     BYTE *pBuf;
     PUSER_INFO_10 pUser;

     // Build full UNC name for PDC
     CString sSer;
     sSer = _T("\\\\");
     sSer += server;
     
     // call net function level 10 (general access)
     dwErrmsg = NetUserGetInfo( sSer,
                                sUserid,
                                10,
                                &pBuf);

     // if return successful, set dwErrmsg to zero
     // (confines net function error checking to this class)
     if (dwErrmsg == NERR_Success || dwErrmsg == ERROR_MORE_DATA)
          dwErrmsg = 0;

     // cast returned BYTE buffer
     pUser = (PUSER_INFO_10 ) pBuf;

     // free the BYTE buffer
     NetApiBufferFree(pBuf);
     
     return pUser;

}


Downloads

Download source - 3 Kb


Comments

  • network security

    Posted by Legacy on 02/18/2003 12:00am

    Originally posted by: ujjaval

    i am doing project on network security.In this project i want to add facility like firewall,locking client,protection against viruses and show the graph of any client usage.

    Reply
  • Can i use API "net user get info" using NT server on the workstation window 2000

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: Fawad Ehsan

    Hello
    First of all thank u CodeGure to solving the problem of the developer. I have a problem. I am newly enter in the visual c++. I made a program in vc++ using api "netuser get info", in which i use level 2 and use these properties.
    LPWSTR usri2_name;
    LPWSTR usri2_full_name;
    DWORD usri2_last_logon;
    DWORD usri2_last_logoff;
    in out put I enter the user name and server . I also mention that i am working in LAN environment and some user have a NT workstation, and some have a window 2000 professional workstation, but the server is window NT ver 4.
    This api work well, for the workstation for NT and give right out put , but if i enter the name of user who use workstation window 2000 professional, it create a problem and not give right out put only in the last logoff all remaining functions well and give right out put . I write down the out put of both user NT and window 2000;
    // for the user who use NT workstation
    Fawad Ehsan // user full name
    Stpk1 // server name
    8/9/02 7:50:46 PM //Date
    Fri Aug 09 18:37:53 2002 // last log on
    Fri Aug 09 18:37:54 2002// last log off
    // for user who use window 2000
    ali ahmed // use full name
    Stpk1 // server name
    8/9/02 7:50:46 PM //Date
    Fri Aug 09 17:16:37 2002 // last log on
    Thu Apr 11 16:25:05 2002 // last log off

    last log off in the window 2000 workstation is not right value, it is my problem. I made this program in window NT and also run this program on window 2000. But the problem is same. Either on window NT or on window 2000.
    Can any body help me.
    Thank u CodeGure .

    With best of regard


    Reply
  • Can i use API "net user get info" using server NT on workstation window 2000

    Posted by Legacy on 08/12/2002 12:00am

    Originally posted by: Fawad Ehsan

    Hello
    First of all thank u to CodeGure to solving the problem of the developer. I have a problem. I am newly enter in the visual c++. I made a program in vc++ using api "netuser get info", in which i use level 2 and use these properties.
    LPWSTR usri2_name;
    LPWSTR usri2_full_name;
    DWORD usri2_last_logon;
    DWORD usri2_last_logoff;
    in out put I enter the user name and server . I also mention that i am working in LAN environment and some user have a NT workstation, and some have a window 2000 professional workstation, but the server is window NT ver 4.
    This api work well, for the workstation for NT and give right out put , but if i enter the name of user who use workstation window 2000 professional, it create a problem and not give right out put only in the last logoff all remaining functions well and give right out put . I write down the out put of both user NT and window 2000;
    // for the user who use NT workstation
    Fawad Ehsan // user full name
    Stpk1 // server name
    8/9/02 7:50:46 PM //Date
    Fri Aug 09 18:37:53 2002 // last log on
    Fri Aug 09 18:37:54 2002// last log off
    // for user who use window 2000
    ali ahmed // use full name
    Stpk1 // server name
    8/9/02 7:50:46 PM //Date
    Fri Aug 09 17:16:37 2002 // last log on
    Thu Apr 11 16:25:05 2002 // last log off

    last log off in the window 2000 workstation is not right value, it is my problem. I made this program in window NT and also run this program on window 2000. But the problem is same. Either on window NT or on window 2000.
    Can any body help me. I am waiting the reply, if u know the other email address who help me kindly reply me. I have this seriously problem and wait the reply of CodeGure.

    Thank u CodeGure.
    With best of regard
    fawad

    Reply
  • NET STOP WORKSTATION

    Posted by Legacy on 11/30/2001 12:00am

    Originally posted by: Adis

    How to make this in VB 6.0, using API functions, or something else ?

    Reply
  • Encapsulating Standard Network API Functions

    Posted by Legacy on 07/02/2001 12:00am

    Originally posted by: Sam Johnson

    Is there an API function (call) that has the functionally of the "NET SEND" command?

    Reply
  • Network functions

    Posted by Legacy on 05/17/2001 12:00am

    Originally posted by: Praveen

    I found most of the network functions with examples.  There is one more api function called NetAccessAdd.  Can any of you give an example as to how to use this api using VB6.0.
    

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

Top White Papers and Webcasts

  • Cisco and Intel have harnessed flash memory technology and truly innovative system software to blast through the boundaries of today's I/O-bound server/storage architectures. See how they are bringing real-time responsiveness to data-intensive applications—for unmatched business advantage. Sponsored by Cisco and Intel® Partnering in Innovation

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds