.NET Tip



Using Pointers In .NET...

Pointers can be used in C#, but only in the blocks of code that we have specifically marked for pointer use. The keyword to do so is unsafe. We can mark individual methods, classes, structures, blocks of code or even local variables as unsafe. For example we can write:

unsfae float myfunction( )
{

// Can use pointers

}

unsfae class myclass
{

unsafe int *x ;

}

unsafe
{

// unsafe block

}

 

Top


Device Drivers Tip



Data Transfer Techniques: Direct I/O...

In this technique no extra copying of data from user mode to kernel mode or vice versa takes place. Instead the application’s buffer is made non-paged and the address of the user mode memory is made available to the device driver. The device driver is now guaranteed that the data it would access would never get paged out to the disk and can safely access the memory. But if the process context switch takes place then it has to go through some rather complex steps to reach the physical location of the applications buffer and then access it. Obviously the technique works faster for large sized data but is difficult to use. Lastly as the device driver is finished accessing the data the user mode applications data is made paged again.

Top


VC++ Tip



We can get the memory information of the machine like total virtual memory, memory in use, etc using an API function GlobalMemoryStatus( ) as shown below:

void mydialog::OnButton1( )
{

DWORD var ;

MEMORYSTATUS memorystatus = { 0 } ;
memorystatus.dwLength = sizeof ( MEMORYSTATUS ) ;

GlobalMemoryStatus ( &memorystatus ) ;

CString m_memused ;
m_memused.Format ( "Memory used %ld", 
memorystatus.dwMemoryLoad ) ;

var = memorystatus.dwTotalPhys / 1024 ;
CString m_meminstalled ;
m_meminstalled.Format ( "Memory installed %ld", var ) ;
m_meminstalled += " KB" ;

var = memorystatus.dwAvailPhys / 1024 ;
CString m_memavailable ;
m_memavailable.Format ( "Memory available %ld", var ) ;
m_memavailable += " KB" ;

var = memorystatus.dwTotalVirtual / 1024 ;
CString m_memvirtual ;
m_memvirtual.Format ( "Total virtual memory %ld", var ) ;
m_memvirtual += " KB" ; 

CString meminfo ;
meminfo = m_memused ;
meminfo += "\n" ;
meminfo += m_meminstalled ;
meminfo += "\n" ;
meminfo += m_memavailable ;
meminfo += "\n" ;
meminfo += m_memvirtual ;

MessageBox ( meminfo ) ;

}

Top


C++ Tip



Macros VS. Inline Functions...

Inline functions which are similar to #define macros, provide better type checking and do not have the side effects so typically associated with macros. For example consider the following program:

#include <iostream.h>

#define AREA( r ) 3.14 * r * r
inline float area ( float r )
{

return 3.14 * r * r ;

}

void main ( )
{

float r = 1.5 ;

float c = AREA ( ++r ) ;
float d = area ( ++r ) ;

}

During preprocessing the macro AREA gets expanded into 

c = ++r * ++r ;

This causes an undesirable side effect in the macro expansion. The variable is getting incremented twice even though we have used the incrementation operator only once. Such side effects would not occur in the inline function.

Top


C Tip



Interrupts...

Every interrupt (be it a software or a hardware interrupt) has got a specific number. For example, on hitting a key from the keyboard interrupt number 9 is generated, whereas when the timer of the computer ticks, interrupt number 8 is generated. The 8086 family supports totally 256 interrupts, numbered from 0 to 255.

When a hardware or a software interrupt occurs, the microprocessor stops its current processing activity, and executes a small memory resident routine called Interrupt Service Routine (ISR). Each interrupt has got its corresponding ISR. Once the ISR has performed its task, the microprocessor resumes the task that was interrupted when the interrupt occured.

Top


Article – C# - Adding Events To Custom Controls



We can also add properties and events to a custom control. In last two articles we saw how to create and use a clock control. In this article we would add properties and an event to it.

The idea is to modify the clock control to an alarm clock control. We plan to add two propertiesAlarmTime and Message to the control. These properties can be set or retrieved programmatically as well as through the Properties Window. The user can set the AlarmTime property to some specific time and as soon as the specified time is reached a message gets displayed. The user can specify the message to be displayed through the Message property. The form data members-altime (of type DateTime) and almessage (of type string)-are controlled by these properties. We have added an event called alarm that would be fired as soon as specified AlarmTime is reached. The AlarmTime and Message properties are given below:

[ Category ( "User Defined" ) , Description ( "Controls the Alarm Time" ) ]
public DateTime AlarmTime
{

get
{

return altime ;

}
set
{

altime = value ;

}

}

[ Category ( "User Defined" ), Description ( "Controls the Alarm Message" ) ]
public string message
{

get
{

return almessage ;

}
set
{

almessage = value ;

}

}

Notice the two attributes that we applied to the properties. The Category attribute specifies the category under which the property would get displayed in the Properties window. This would be visible whenever we drag the control on the form and open the Properties window. The Description attribute accepts a String that would get displayed in a small part of the window present at the end of the Properties window. This string describes the property. The Properties window of the control is shown in the following figure.

To add the event we have first added a delegate called NotifyAlarm to the control that accepts two parameters-an object and a TimeEventArgs object.

public delegate void NotifyAlarm ( object sender, TimeEventArgs e ) ;

TimeEventArgs is a user-defined class. In this class we have added a data member named msg of type String and a one-argument constructor accepting a string that would be stored in msg. The class is shown below:

public class TimeEventArgs : EventArgs
{

public string msg ;
public TimeEventArgs ( string s )
{

msg = s ;

}

}

Next we added the event as data member of the class as shown below:

public event NotifyAlarm alarm ;

For the alarm to ring at the specified time, with every minute we must check whether the specified alarm time is equal to the time displayed in our control. We have done the checking in the Tick event handler. The modified Tick event handler is given below.

private void mytimer_Tick ( object sender, System.EventArgs e )
{

g.TranslateTransform ( 60, 60 ) ;
for ( int i = 0 ; i <= 12 ; i++ )
{

g.FillRectangle ( b, -2, -45, 4, 5 ) ;
g.RotateTransform ( 30 ) ;

}
g.ResetTransform( ) ;

//seconds
p.Width = 1 ;
p.EndCap = LineCap.Flat ;
p.Color = Color.Red ;
g.TranslateTransform ( 60, 60 ) ;
g.RotateTransform ( s ) ;
g.DrawLine ( p, 0, 0, 0, -40 ) ;
g.ResetTransform( ) ;

//minutes
p.Width = 3 ;
p.EndCap = LineCap.Triangle ;
p.Color = Color.Blue ;
g.TranslateTransform ( 60, 60 ) ;
g.RotateTransform ( m ) ;
g.DrawLine ( p, 0, 0, 0, -30 ) ;
g.ResetTransform( ) ;

//hours
p.Width = 4 ;
g.TranslateTransform ( 60, 60 ) ;
g.RotateTransform ( h ) ;
g.DrawLine ( p, 0, 0, 0, -20 ) ;
g.ResetTransform( ) ;

s += 6 ;
if (s == 360 )

s = 0 ;

sec = sec + 1 ;

if (sec == 60)
{

sec = 0 ;
m += 6 ;

if (m == 360)

m = 0 ;

min = min + 1 ;

if ( altime.Hour == hr && altime.Minute == min ) 
{

TimeEventArgs t = new TimeEventArgs ( almessage ) ;
alarm ( this, t ) ;

}

h += 0.5f ;
if ( h == 360 )

h = 0 ;

if ( min == 60 )
{

min = 0 ;
hr += 1 ;

}

}

Size sz = this.Size ;
Rectangle rect = new Rectangle ( 0, 0, sz.Width, sz.Height ) ;
Bitmap bm = mybmp.Clone ( rect, mybmp.PixelFormat ) ;
this.BackgroundImage = bm ;
Color bkclr = this.BackColor ;
g.Clear ( bkclr ) ;

}

As soon as altime.Hour becomes equal to hr and altime.Minute becomes equal to min, we created an object referred to by t of the TimeEventArgs class by passing almessage to it. Next we raised the event using RaiseEvent and passed the reference of the control to it. We have also added an attribute called DefaultEvent to the Clock class as shown below:

[ DefaultProperty ( "Message" ) , DefaultEvent ( "alarm" ) ]

As a result, whenever we drag our control on the form and double click on the control in the Client's Windows Designer, of all the event handlers available the alarm event handler would get added in the code.

Now let us see what we need to do to handle the event in the client. As soon as we double click on the control after dragging it on the form the myclock_alarm( ) event handler gets added to the form class. To display the alarm message we have simply popped a messagebox displaying the alarm message. The handler is shown below.

private void myclock_alarm ( object sender, ClockCtrl.TimeEventArgs e )
{

MessageBox.Show ( e.msg ) ;

}

In the client on selecting AlarmTime in the Properties window we would get a calendar control. We have set the values for AlarmTime and Message properties. When the clock would reach the alarm time it would display the message as shown in the following figure.

Top


Joke



A man goes into a pet shop to buy a parrot. The shop owner points to three identical looking parrots on a perch and says, "the parrot on the left costs 500 dollars".

"Why does the parrot cost so much," asks the man.
The shop owner says, "well, the parrot knows how to use a computer".

The man then asks about the next parrot to be told that this one costs 1,000 dollars because it can do everything the other parrot can do plus it knows how to use the UNIX operating system.

Naturally, the increasingly startled man asks about the third parrot to be told that it costs 2,000 dollars. Needless to say this begs the question, "What can it do?"

To which the shop owner replies, "to be honest I have never seen it do a thing, but the other two call him boss!"

Top


Different Strokes



Top