Network Services

   

Network services allow communication between applications on different computers on a network. The network functions are used to create and manage connections to shared resources, such as directories and network printers, on computers in the network. The Win32 network interfaces include Windows Networking, Windows Sockets, NetBIOS, RAS, SNMP, and Network DDE and are described in Table 14.17.

Table 14.17. The Win32 Network Interfaces

Network Interface

Description

Windows Networking (WNet)

Provides a set of network-independent functions for implementing networking capabilities within an application. Functionality is provided by the Win32 Network Interface DLL ( mpr.dll ). WNet prefixes all network functions.

Ported LAN Manager Functions

Provides functionality for a network operating system and was originally designed for OS/2-based servers. Now it is no longer considered to be part of the Win32 API. However, much of the functionality is still provided by Windows through the netapi32.dll .

NetBIOS Interface

Used to communicate via a network with applications on other computers. The Network Basic Input/Output System (NetBIOS) functionality is provided by the netapi32.dll .

Network Dynamic Data Exchange

Enables DDE to work over a network. Functionality is provided by the WFW DDE Share Interface ( nddeapi.dll ). All network DDE functions are prefixed by NDde .

Remote Access Service (RAS)

Allows users at remote locations to connect directly to a computer network, accessing one or more RAS servers. Supports dial-up networking. RAS functionality is provided by the rasapi32.dll . All RAS functions are prefixed by RAS .

Simple Network Management Protocol (SNMP)

Used for exchanging network management information through the SNMP Internet standard protocol. See Windows Help for more information.

Windows Sockets (WinSock)

Used for TCP and UDP network data exchange. WinSock is based on the Berkeley Software Distribution (BSD) Unix sockets paradigm. Windows Socket functionality is provided by wsock32.dll (WinSock version 1.1) or by ws2_32.dll (WinSock version 2.0).

By far, the most utilized network component of the Win32 API is Window Sockets (WinSock). WinSock provides the basis for all Internet activity and applications: email, Web browsers, udp broadcasting, ftp, and so on. Let's look at a useful example pertaining to the WinSock library.

Getting Network Info

One of the things that can be really helpful is to know the IP address of the local computer (see Figure 14.11).

Figure 14.11. Network Info Example Using WinSock.

graphics/14fig11.gif

One of the easiest ways to determine the IP address for your primary network card or modem is to use the WinSock library. Other useful information might include the WinSock version, maximum size , and host name . The code example in Listing 14.15 illustrates how we can obtain this information.

Listing 14.15 Getting Network Info
 void __fastcall TFormWinSockIPInfo::ButtonGetInfoClick(TObject *Sender)  {      WSADATA             localWSA;      if (WSAStartup ((MAKEWORD(1,1)), &localWSA) != 0)      {         MessageBox (NULL, "Sorry - Unable to load Windows Sockets - "                           "Please close other applications and try again.",                           "UDP Network Interface", MB_OK  MB_ICONASTERISK);         WSACleanup ();         return;      }      LabelMaxSize->Caption = localWSA.iMaxUdpDg;      LabelVersion->Caption = AnsiString(localWSA.szDescription);      char szHost[MAX_PATH];  // should be big enough.      memset(szHost, 0, MAX_PATH);  // clear it      gethostname(szHost, MAX_PATH);      struct hostent *hp;      hp = gethostbyname(szHost);      if( ! hp )             return;      LabelName->Caption = hp->h_name;      char szIpAddress[96];      sprintf(szIpAddress, "%d.%d.%d.%d",                            UC(hp->h_addr[0]), UC(hp->h_addr[1]),                            UC(hp->h_addr[2]), UC(hp->h_addr[3]));      LabelIPAddress->Caption = AnsiString(szIpAddress);      // clean-up      WSACleanup ();  }  

The first WinSock call made in this example is WSAStartup(). This call initiates the use of the Windows Sockets DLL by our program. The first parameter passed into the WSAStarup() call indicates the version of WinSock we need to perform our query of information. In this case, version 1.1 is enough to get the information we need. That is to say that no calls or features require anything newer than WinSock version 1.1. The second item passed into the WSAStartup() call is a pointer to the WSADATA structure defined by the variable localWSA . This is the first nugget of information that we receive. Let's take a look at the WSADATA structure definition.

 typedef struct WSAData {          WORD                    wVersion;          WORD                    wHighVersion;          char                    szDescription[WSADESCRIPTION_LEN+1];          char                    szSystemStatus[WSASYS_STATUS_LEN+1];          unsigned short          iMaxSockets;          unsigned short          iMaxUdpDg;          char FAR *              lpVendorInfo;  } WSADATA, FAR * LPWSADATA; 

The information displayed on the form within our example includes the descriptive version using the character array szDescription , the maximum size of a UDP packet using iMaxUdpDg , and the maximum number of sockets that can be established using iMaxSockets .

After we gather this information using WSAStartup() , we use the gethost() call to determine the host name for the local machine. This information is displayed to the user . Finally, we retrieve the IP address using this host name through a call to geth ostbyname() , which returns a pointer to the hostent structure. The hostent structure is defined as follows :

 struct hostent {      char FAR *       h_name;      char FAR * FAR * h_aliases;      short            h_addrtype;      short            h_length;      char FAR * FAR * h_addr_list;  }; 

Each IP byte is contained in the list for h_addr , which we can access by index. However, this data is contained in an integer. Again, all we care about for each index is the first byte. So, as demonstrated in our example, we apply the following macro, which clears all the values greater than 255 to give us one byte.

 #  define  UC(b)  (((int)b)&0xff) 

Using sprintf() , we format the IP address value in its normal notation so that it can be displayed to the user.

Adding System Support

It's sometimes useful to add system-support capabilities to an application, such as the capability to quickly lock an NT workstation, disable Ctrl+Alt+Delete, or shut down (and reboot) a machine. The Win32 API provides many useful routines to help perform these types of tasks . However, it's important to note the danger of providing these types of capabilities. If you're going to implement these types of functionality (and debugging in the process), be sure to save early and often, or you might just wish you had selected another hobby or profession.

Locking an NT Workstation

It's time to introduce you to another new Win32 API named LockWorkStation() , which is used to automatically lock an NT system. LockWorkStation() takes no parameters, and it's automatic. Locking workstations or servers couldn't be easier. The function mimics the well-known Ctrl+Alt+Delete keys and selects Lock Workstation . The system is then locked until later use. This function is in the WINUSER.H header file.

With the following one line of code, you can automate what used to be a manual operation under NT. Unfortunately, Windows 9x does not support this function:

 LockWorkStation(); 
System Shutdown

A lot of beginners often ask how to automate the shutdown of a Windows PC. By using the ExitWindowsEx() or ExitWindows() function, you can properly shut down the system. The following code demonstrates how to use the ExitWindowsEx() API function:

 ExitWindowsEx(EWX_SHUTDOWN,0); 

This routine is not new like the LockWorkStation() function, but it sure has a lot of power in manipulating your system ”by turning it off. If your PC has power conservation methods in the BIOS, ExitWindowEx() will automatically shut down your PC's power, too.

ExitWindowsEx() is an easy way to shut down Windows, but you can do a lot more than just shut down Windows. The ExitWindowEx() function has more flags with which to work. The format is as follows:

 BOOL ExitWindowsEx(      UINT uFlags,    // shutdown operation      DWORD dwReserved     // reserved     ); 

uFlags are flags to specify which shutdown type you wish to perform. Table 14.18 shows the values.

Table 14.18. System Shutdown Function Flags

Flags

Value

EWX_FORCE

Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data, so you should use this flag only in an emergency.

EWX_LOGOFF

Shuts down all processes running in the security context of the process that called the ExitWindowsEx() function. Then it logs the user off.

EWX_POWEROFF

Shuts down the system and turns off the power. The system must support the power-off feature. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. Windows 95: Security privileges are not supported or required.

EWX_REBOOT

Shuts down and then restarts the system. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege.

EWX_SHUTDOWN

Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped . Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege.

The dwReserved parameter is currently not used.


   
Top


C++ Builder Developers Guide
C++Builder 5 Developers Guide
ISBN: 0672319721
EAN: 2147483647
Year: 2002
Pages: 253

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net