Personal C Sharp         by    famsoft.org

Home | Demonstrative Examples | PC# Methods | PC# Reference | Links
Accessing External Objects

 

                                 DEMONSTRATIVE EXAMPLES
                                 ======================
                         EXAMPLES ON ACCESSING EXTERNAL OBJECTS
                         ======================================

Although the main purpose behind PC# was to find the simplest possible way which can allow
a person with less programming experience to write his own programs using C# and the .NET
framework, the techniques we have developed have proven to do even better jobs with
advanced features like Object Serialization and Remote Method Invocation.
 
OBJECT SERIALIZATION:
 
Serialization is the process of converting the state of an object into a form that can be
persisted or transported.
 
As you have already seen in the demonstrative examples, when you use PC# methods you
rarely need to use variables, and when you do, you can always use the variables which
have been pre-defined by PC# and assigned to your programs. This makes things alot
easier for you.
 
Unless you have declared your own variables which you like to serialize, Serialization and
Deserialization of necessary variables, including the pre-defined ones which your programs
use are totally done by PC# for you. You do not need to get involved in serialization.
 
EXECUTING EXTERNAL PROGRAMS:
 
Since serialization is no work, exiting your program to run something else then returning
back to find everything exactly the same can easily be done. Immediately before you exit,
PC# serializes all var's and save the resulting byte stream into file and immediately
after your return, PC# deserializes all data and restores all vars. All it takes is a
mechanism which executes the exit and return. Let us have an example to see how it works.
 
ABOUT THE NEXT EXAMPLE:
 
Networking Engineers use the "Ping" utility to check the connection with a host. The
utility sends packets of information to the host and receives them back. The number
of packets received and the time they took for the trips measure the connection quality.
 
To use the ping utility, you need to supply it with the host's IP Address. If you don't
know it, you need to run the "ipconfig" utility first which displays all TCP/IP
configuration values. The next example show how to do the two operations programatically.
=========================================================================================
EXAMPLE 1: Write a program  which uses the "ipconfig" utility to get networking
information, searches the data to obtain the WAN's IP Address then use the "ping" utility
to check connection with that address.
=========================================================================================
public class a:pcs {
  public override void init() {
    tia=toa="t";                               // Use text screen for text output
    bli=0;                                     // Start at block 0.
    base.init();                               // Initialize pcx
  }
  public override void run() {
    if (blp==0) {                              // Startup block
      os="ipconfig /all";bli=1;xm("r");return; // run ipconfig then goto block 1
    }
    if (blp==1) {                              // Re-entry with os="ipconfig" output text
      fns="crb12";tm();                        // Display ipconfig output
      txs=os;                                  // Assign text to searchable string (txs)
      js="IP Address";j=2;tm("s");             // Move pointer to end of second phrase
                                               // "IP Address". Then
      js=": ";ks="\n";tm("s");                 // return string following ": " and ending
                                               // immediately before new line code in (os)
      os="ping "+os;bli=2;xm("r");return;      // ** run "ping IPAddress" then goto blk 2
    }
    if (blp==2) {                              // Re-entry with os="ping" output text
      fns="crb12";tm();                        // Display ping utility's output string.
    }
  }
}
=========================================================================================
HOW TO COMPILE AND RUN THIS PROGRAM:
 
To compile: pcp a [Enter]
To run:    xrun a [Enter]
 
The "xrun" tool must be used to execute a program which requires exit and re-entry.
=========================================================================================
TUTORIAL:
The program should be developed in two steps. In the first step you need to comment the
line marked with "**" to prevent jumping to block 2. So you can see the output of the
"ipconfig" utility and find how to obtain the IP address of the WAN. Then you uncomment
the line and let the program continue until the text output of the "ping" utility shows
on display.
 
Accessing utilities programatically can be extremely valuable. You can write programs which
can manage every property in your system. If you have a server you can do most of your
system administration with programs. There are many utilities which allows you to create,
modify or delete Active Directory objects like users, computers or services.
 
You learn more about most of the utilities by entering the following at the command prompt:
 
                           UtilityName /?  then pushing {Enter] key.
 
Here is a list of some of these utilities:
 
ARP           Physical to IP address translation.
AT            Task scheduling
CHKDSK        Checks for disk errors
COMPACT       Compresses files
CONVERT       Converts FAT to NTFS
DATE          Displays date
EXPAND        Decompresses files
FC            Compares files
FORMAT        Formats disks
FTP           FTP Communication
FTYPE         File types
IPCONFIG      Displays TCP/IP Configuration values
NBSTAT        NetBios over TCP/IP
NET ACCOUNTS  Manages user accounts
NET COMPUTER  Adds/Removes computers from Domains
NET CONFIG SERVER  Server Configuration
NET CONFIG WORKSTATION Workstation Configuration
NET CONTINUE  Resume paused service
NET FILE      Lists open files
NET GROUP     Manages Global Groups
NET LOCALGROUP Manages local groups
NET PAUSE     Pauses a service
NET PRINT     Print jobs and queues
NET SEND      Sends messages     
NET SESSION   Lists sessions on local and remote computers
NET SHARE     Manages shared printers and directories
NET STATISTICS Displays Workstation and server statistics
NET STOP      Stops Network Services
NET TIME      Displays time and synchronizes time with remote computers
NET USE       Manages Remote Connections
NET USER      Manages User Accounts
NET VIEW      Displays available network resources
NETSTAT       Displays status of network connections
NSLOOKUP      Performs DNS resolutions
NTBACKUP      Backup of files
PATH          Displays or sets a search path for files
PING          Tests network connections
RECOVER       Recovers information from a defective disk
ROUTE         Manages Network Routing Table
TIME          Displays or sets system time
TRACERT       Displays the path between local computer and remote one
 
ACCESSING SCRIPTS:
 
If all these utilities are not enough for you, you can write your own script program to do
the wildest operations like restarting or shutting off the system and execute the script
file programatically.  Let us see how to do that.

demo44.jpg

Example 1: This is the first part when the text output of the IPCONFIG utility is being analyzed in order
to obtain the WAN's IP address and use the PING utility to check the connection.

demo45.jpg

Example 1: This is a display of the PING utility's text output which indicates that the connection is fine.

=========================================================================================
EXAMPLE 2: Write a program  in "JScripts" which restarts the system and show how to
execute it with a C# program.
=========================================================================================
The Script program:  Save into a file named "restart.js".
 
var oo = WScript.CreateObject("Shell.Application");// Create Shell object
oo.ShutdownWindows();                              // Display Shutdown / Restart Dialog
=========================================================================================
The C# program:   Save into a file named "a.cs", compile it and run it with "xrun"
 
public class a:pcs {
  public override void init() {
    bli=0;
    base.init();
  }
  public override void run() {
    if (blp==0) {
      os="Are you sure you like to restart your computer?";
      ks="yn";cm("d");                             // Display msg with yes/no buttons
      if (os.Equals("y")) {                        // If user selects "yes"
        os="restart.js";bli=1;xm("r");return;      // Run "restart.js" file
      }
      else sm("e");
    }
  }
}
=========================================================================================
Use Notepad to write both programs. Name the script file "restart.js". Name the C# file
"a.cs", Compile it with "pcp a" and run it with "xrun a".
=========================================================================================
TUTORIAL: When we used method xm("r"), we supplied it with a block number and followed
the statement with "return". This was just to keep the routine. After system restarts or
shuts dowm, jumping to a new block or returning are meaningless.