C Tip



FP_SEG And FP_OFF…

Sometimes while working with far pointers we need to break a far address into its segment and offset. In such situations we can use FP_SEG and FP_OFF macros. Following program illustrates the use of these two macros.

#include <dos.h>

main( )
{

unsigned s, o ;
char far *ptr = "Hello!" ;

s = FP_SEG ( ptr ) ;
o = FP_OFF ( ptr ) ;
printf ( "\n%u %u", s, o ) ;

}

Top


C++ Tip



Would the following code work?

#include <iostream.h>
void main( )
{

ostream o ;
o << "Dream. Then make it happen!" ;

}

Ans: No! This is because we cannot create an object of the ostream class since its constructor and copy constructor are declared private.

Top


VC++ Tip



If we create a list control dynamically then we cannot add handlers for it through ClassWizard. If we need to add a handler then we have to add it manually. For example, if we want to add double-click handler for a list control then we need to add following declaration in the '.h' file:

void OnDblclkItem ( NMLISTVIEW *p, LRESULT *q ) ;

Also we need to add following entry in the message map.

ON_NOTIFY_REFLECT ( NM_DBLCLK, OnDblclkItem )

Top


C# Tip



What are parameterized commands?

Ans: Often we require to execute the same command on a database repeatedly, in which only the values used in command are different. In such cases we can use parameterized commands. Parameterized commands are also useful in the commands where input values come from user input. They are also useful when values for only some of the fields in a row are to be stored. For example, suppose our database table consists of fields f1, f2, f3 . . . and so on, till f7. Suppose we have specified default values for f2, f3, f4 and f6 and we wish that f1, f5 and f7 be filled with values a, b and c respectively. Then the parameterized command will be

"Insert into table1 { f1, f5, f7 } values ( @a, @b, @c )"

The parameter is added to the command by using Parameters collection of the command object as shown below:

com.Parameters.Add ( "@a", 10 ) ;
com.Parameters.Add ( "@b", 230.0 ) ;
com.Parameters.Add ( "@c", 170 ) ;

where com is a reference to SqlCommand object.

 Top


VB.NET Tip



How do I write code that sends a mail via outlook express?

Ans: Create a Windows application and place a button named sendmail and a text box named emailaddr on the form. On adding the email address to the text box if sendmail button is clicked 'OutLook Express' dialog box should get popped up with an email address written in the text box. To get so write following code in the button handler.

Private Sub sendmail_Click ( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles mailsend.Click

If emailaddr.Text = "" then

MessageBox.Show ( "Enter The Email Address" ) 
Return

End If
System.Diagnostics.Process.Start ( "mailto:" & emailaddr )

End Sub

Top


Device Driver Tip



Hardware Environment…

Most of the devices do not use the memory mapping schemes. They use what is known as port mapped I/O scheme. The x86 architecture defines a separate address space for the devices. The device registers of the hardware device is mapped into the locations of this address space (I/O address space). A 16 bit address bus is used for accessing the locations within this I/O address space. A 32-bit bus is used for transferring data between the locations and the CPU.
In actual practice the data bus for memory address space and I/O address space is common. More over the address bus for accessing I/O address space and memory address space is also common. A dedicated line of the control bus is used to distinguish whether the address bus is being used for accessing the memory address space or I/O address space.

Top


Article: C#- Code Access Security



Code access Security is a new concept brought in by the .NET framework. Code access security essentially involves granting permissions to an assembly. Code access security uses the location of assembly and other information about the identity of code as a primary factor in determining permissions to be granted to the assembly. This information about the identity of an assembly is called evidence. Once loaded in runtime, the code starts executing in the restricted environment depending upon permissions granted to it. If the code is not trusted enough to run or runs but performs an action for which it has no permissions, then a security exception is thrown. The code access security system means we can stop running of malicious code and also restrict the code to run in a restricted environment where we are sure that it won’t do any damage to the resources. Let us now see few cases where code access security becomes crucial.

Suppose an organization is working with some extremely important data, we would use code access security system to state which code is allowed to access the database and which is not. Similarly if we run an application from a network drive and it tries to access the file on a local drive an exception must be thrown. This can be done using code access security system.
It is important to know that code access security protects resources like local disk, network, user interface from malicious code. It is not a tool for protecting software from users.

Code access security works on the basis of Code Groups and Permissions.

Code Group

Code groups are a grouping of assemblies with same privileges. For example, code groups include “Internet” and “Intranet”. The group Internet includes code downloaded from Internet and the group Intranet includes code sourced from LAN. The information based on which the assemblies are grouped is called evidence. The assembly loader is responsible for collecting the evidence at load time based on where the code is loaded from. The CLR ships with seven types of evidence. 

  - Zone: Region from which the code originated
  - Site: Web site from which the code was downloaded
  - URL: Specific location of the code
  - Application Directory: Location of the assembly
  - Strong name: The unique name for the code
  - Publisher: Publisher of the code
  - Hash value: Hash value for the assembly 

Collectively these seven types of evidence are called host evidence as they are implemented by the host environment. 

Permission

Permission means authorization to perform actions. These actions often involve accessing certain resources. These permissions are exposed programmatically through the System.Security.PermissionSet namespace. 

Whenever a code is running, the CLR has to decide which permissions to grant that code and which ones to deny. The CLR decides which permissions to grant and which to deny by doing a simple trick, it checks whether the caller of our code has the permission perm1 or not. CLR repeats this process for the callers of our assembly till there are no more callers are left. If all the callers have that permission, our code is granted the requested permission. This can be better explained by the following example. 

Consider an assembly A. which calls Assembly B, B in turn calls C. C finally calls our assembly D. If our assembly demands a certain permission say perm1, the CLR will first check whether our assembly's caller (C in this case) has that permission. If it has, the CLR further looks whether B has it or not. If B has that permission, the CLR finally checks to see whether the root assembly A has the demanded permission. If all of the preceding assemblies C, B and A have that permission, our assembly is also granted the permission. 

Even if any one intermediate assembly does not have the demanded permission, our assembly is denied perm1. This process of checking permissions of all the parent assemblies is known as Stack Walking. Stack Walking is a costly activity, but considering the gains it has to offer in terms of security implementation, Microsoft decided to implement it in the CLR. 

Assemblies demand permissions when it is clear that without those permissions the assembly will not execute. Demanding permissions is done programmatically. The following program shows us how.

Our program will read a file from the local disk and display the first line. In our program we will demand complete access to the ‘C:\’ drive. If the access demand is denied, an exception will be raised which we will catch. 

Create a Windows based application and design the form as shown below.

Name the text box as t, Read button as bread and Exit button as bexit. Add the following code in the constructor of the Form1 class just below the call to InitializeComponent( ) method.

FileIOPermission fp ;
try
{

fp = new FileIOPermission( FileIOPermissionAccess.AllAccess, @"C:\" );
fp.Demand( ) ;

}
catch ( Exception e1 )
{

MessageBox.Show ( e1.Message ) ;

}

Here we create a reference of type FileIOPermission. We have inisialised the reference in the try block. In the constructor we have passed the AllAccess enumerated type to specify that our code needs complete access permissions to the ‘C:\’ drive of the machine where our application is to execute. The constructor is followed by a demand for the above permission.

It is necessary to place the demand part in a try-catch block because if the permission is denied, the Demand( ) method would raise an exception and our program would be terminated abruptly. 

Now add a handler for the Read button. Write the code as shown below to it.

private void bread_Click ( object sender, EventArgs e )
{

StreamReader sr = new StreamReader ( "c:\\hi.txt" ) ;
t.Text = sr.ReadLine( ) ;
sr.Close( ) ;

}

We have created a stream reader object to read the “c:\hi.txt” file. Next we have read a line from that file and displayed it in the text box t.
Now add a handler for the Exit button and call Dispose( ) method from it. Calling Dispose( ) would terminate the application if the Exit button is clicked.

Finally add the following declarations at the beginning of the program.

using System.IO ;
using System.Security ;
using System.Security.Permissions ;

Run this application in various scenarios, like on the local machine with security on, local machine with security off, copy it to another machine and run it from the remote computer. Configure security policy to give full trust for code from the remote machine and run the program. Also try out running the program from the remote computer with security switched off. To switch the security on/off we can use the ‘caspol’ utility that is shipped with Visual Studio.NET. This exercise will give you a better insight into the working of code access security.

Top


Joke



Mr. and Mrs. Shaw were on safari in Africa, walking through the jungle. Suddenly a huge lion sprang out of the bushes and seized Mrs. Shaw, dragging her off. "Shoot!" she screamed to her husband, "Shoot!"
"I can't!" he shouted back. "I've run out of film!"

Top


Different Strokes





An Illusion!

Top