© 2004 Microsoft Corporation. All rights reserved.
Figure 1 Comma Priority
////////////////////////////////////////////////////////////////
// MSDN Magazine — June 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// This dopey program demonstrates the precedence of = over comma. Zzzzz.
// To compile, type:
//
// cl comma.cpp
//
#include <stdio.h>

void main()
{
  int a;
  a = 1,2,3;               // a = 1; equivalent to (a=1),2,3;
  printf("a=%d\n",a);

  a =(1,2,3);              // a = 3
  printf("a=%d\n",a);
}
Figure 2 getip1
////////////////////////////////////////////////////////////////
// MSDN Magazine — June 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Set tabsize = 3 in your editor.
//
// getip1.cpp
//
// This program reports the IP address for each adapter in your machine.
// To compile from command-line type:
//
// cl getip1.cpp wsock32.lib
//
// Make sure your INCLUDE and LIB environment variables are set up properly;
// you can run vcvars32.bat
//
#include <winsock.h>
#include <wsipx.h>
#include <wsnwlink.h>
#include <stdio.h>

int main()
{
   ////////////////
   // Initialize windows sockets API. Ask for version 1.1
   //
   WORD wVersionRequested = MAKEWORD(1, 1);
   WSADATA wsaData;
   if (WSAStartup(wVersionRequested, &wsaData)) {
      printf("WSAStartup failed %s\n", WSAGetLastError());
      return -1;
   }

   //////////////////
   // Get host name.
   //
   char hostname[256];
   int res = gethostname(hostname, sizeof(hostname));
   if (res != 0) {
      printf("Error: %u\n", WSAGetLastError());
      return -1;
   }
   printf("hostname=%s\n", hostname);

   ////////////////
   // Get host info for hostname. 
   //
   hostent* pHostent = gethostbyname(hostname);
   if (pHostent==NULL) {
      printf("Error: %u\n", WSAGetLastError());
      return -1;
   }

   //////////////////
   // Parse the hostent information returned
   //
   hostent& he = *pHostent;
   printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
      he.h_name, he.h_aliases, he.h_addrtype, he.h_length);
   
   sockaddr_in sa;
   for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
      memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
      // Output the machines IP Address.
      printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // display as string
   }

   //////////////////
   // Terminate windows sockets API
   //
   WSACleanup();

   return 0;
}
Figure 5 getip2.cs
////////////////////////////////////////////////////////////////
// MSDN Magazine — June 2002
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with C# 7.0/.Net on Windows XP.
// Set tabsize = 3 in your editor.
//
// This program reports the IP address for each adapter in your machine.
// To compile, type:
//
// csc getip2.cs
//
// Make sure your INCLUDE and LIB environment variables are set up 
// properly for VS 7 and .NET. You can run vsvars32.bat
//
using System;
using System.Net;                       // network stuff
using System.Collections;               // array iterator

//////////////////
// standard C# application class
//
class MainApp {

   // Application entry point.
   public static void Main() {

      // Display host name.
      string hostname = Dns.GetHostName();
      Console.WriteLine("hostname = {0}", hostname);

      // Display each IP address.
      IPHostEntry hostent = Dns.GetHostByName(hostname); // host info
      Array addrs = hostent.AddressList;            // array of IP addrs
      IEnumerator it = addrs.GetEnumerator();       // iterator:
      while(it.MoveNext()) {                        // while next IP addr
         IPAddress ip = (IPAddress)it.Current;      // ..get IP address
         Console.WriteLine("Address: {0}", ip);     // ..display it
      }
   }
};
Figure 6 csclass.cs
////////////////////////////////////////////////////////////////
// This program illustrates how to get the class name of an object from 
// the base class constructor. Unlike C++, the class name is the name of 
// the most-derived class.
// To compile, type: csc csclass.cs
//
using System;

/////////////////
// Base class
//
class MyBase: Object {
   public MyBase() {
      Console.WriteLine("Inside MyBase ctor");

      // Display this object's class name and base class name.
      // This will display the derived class name, not MyBase.
      Console.WriteLine("Constucting {0}, base type = {1}.", GetType(),
         GetType().BaseType);
      // Show the base class for MyBase.
      Console.WriteLine("Base class for MyBase is {0}.",
         Type.GetType("MyBase").BaseType);

      Print(); // call virtual fn from ctor
      Console.WriteLine("");
   }

   public virtual void Print() {
      Console.WriteLine("Inside MyBase::Print");
   }
};

//////////////////
// Derived class
//
class MyDerived: MyBase {
   public MyDerived() {
      Console.WriteLine("Inside MyDerived ctor");

      // Display this object's class name and base class name.
      // This will display the derived class name, not MyBase.
      Console.WriteLine("Constucting {0}, base type = {1}.", GetType(),
         GetType().BaseType);
      // Show the base class for MyDerived.
      Console.WriteLine("Base class for MyDerived is {0}.",
         Type.GetType("MyDerived").BaseType);

      Print(); // call virtual fn from ctor
      Console.WriteLine("");
   }

   public override void Print() {
      base.Print();
      Console.WriteLine("Inside MyDerived::Print");
   }
};

class MainApp {
   // Main application entry point.
   public static void Main() {

      // Create instance of MyDerived. This will display ctor messages.
      MyDerived d = new MyDerived();

      // Write full derivation of object.
      Console.WriteLine("Object d derivation:");
      int indent=0;
      Type type = d.GetType();
      while (type!=null) {
         String s = new String(' ',indent);
         Console.Write(s);
         Console.WriteLine(type);
         type = type.BaseType;
         indent++;
      }
   }
};

Home
Topics
Issues and Downloads
Subscribe
Submit an Article
RSS

MSDN Magazine