.NET Tip



How do I write code to get the list of fonts available on machine in a combo-box?

Ans: Create a Windows application. Add a combo box on form. Add code to Load event as shown below:

Private Sub Form1_Load ( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load

Dim f( ) As FontFamily = FontFamily.Families
Dim l As FontFamily
For Each l In f

ComboBox1.Items.Add ( l.GetName ( 0 ) )

Next

End Sub

Top


Device Drivers Tip



I/O Mapped Device Registers...

Device registers have to be accessed with IN and OUT instructions. The IN instruction is used to read and the OUT instruction is used to write to the locations. It uses I/O address bus which comprises of only 16 bits of the entire address bus. The technique is only used for small sized data buffers. Data buffers is an array of device registers, i.e. if a device has only a few device registers it will prefer using I/O mapped technique. Accessing I/O mapped device registers however results into slower access.

Top


VC++ Tip



How do I write code so that when focus is set on edit control, its background and foreground color changes and when the focus is not on the edit control, it should change back to normal?

Ans: 
To change color of edit control when focus is set on it and bring it back to normal when focus goes on some other control, create a class derived from CEdit and handle EN_SETFOCUS and EN_KILLFOCUS events in it. Besides this also handle WM_CTLCOLOR message in this class. Add code to the event handlers as shown below:

HBRUSH MyEdit :: CtlColor ( CDC* pDC, UINT nCtlColor ) 
{

if ( CWnd :: GetFocus( ) == this )
{

static CBrush mybrush ( RGB ( 255, 0, 0 ) ) ;
pDC -> SetBkColor ( RGB ( 0, 255, 0 ) ) ; 
pDC -> SetTextColor ( RGB ( 0, 0, 255 ) ) ; 
return mybrush ;

}
return NULL ;

}

void MyEdit :: OnSetfocus( ) 
{

Invalidate( ) ; 
UpdateWindow( ) ;

}

void MyEdit :: OnKillfocus( ) 
{

Invalidate( ) ; 
UpdateWindow( ) ;

}

Now add a member variable of MyEdit class to the dialog class of your dialog-based application. Also add an edit control on to the dialog template. To subclass this control add code to OnInitDialog( ) as shown below:

BOOL CMyDlg :: OnInitDialog( )
{

m_edit.SubclassDlgItem ( IDC_EDIT1, this ) ; // Where m_edit is a variable of MyEdit
CDialog :: OnInitDialog( ) ;
// AppWizard generated code

}

Top


C++ Tip



Using a smart pointer can we make an object appear like a pointer?

Ans: Yes, we can! If a class overloads the operator -> then any object of that class can appear like a pointer when the
operator -> ( ) is called. This is shown in the following example:

#include <iostream.h>
class test
{

public :

void fun( ) 
{

cout << "fun of smart pointer" ;

}

} ;

class smartpointer
{

test t ;

public :

test* operator ->( )
{

return &t ;

}

} ;

void main( )
{

smartpointer sp ;
sp -> fun( ) ;

}

The beauty of overloading operator -> is that even though sp is an object, we can make it work like a pointer. The operator -> ( ) returns the address of the object of the type test. Using this the function fun( ) of the class test gets called. Thus even though fun( ) is not a member of smartpointer class, we can still call it using sp.

Top


C Tip



An easy way to find and count the total number of vowels in given string is to use strpbrk( ) function as shown in the following code snippet...

#include <string.h>
main( )
{

char str[] = "Good Morning" ;
char *p = str ;
int c = 0 ;

while ( *p )
{

p = strpbrk ( p, "aeiou" ) ;
if ( p )
{

c++ ;
p++ ;

}
else

break ;

}

printf ( "\nThe given string contains %d vowels", c ) ;

}

Top


Article – C# - Tapping the Processes Using WMI - I



In the last article we saw how to list the processes currently running on the local machine. In this article we would see a similar application that displays the processes currently running on the machine but with additional functionality. We would display more information of each process and also extend the program to view the processes running on a remote machine. The application would also allow us to terminate the running process.

Create a WinForm application and place controls on it as shown below:

Change the names of controls as shown in the following table.

 Control

 Name

 ListView 

 m_plist

 Machine Name TextBox

 m_mname

 Username TextBox

 m_uname

 Password TextBox

 m_password

 Refresh Button

 m_brefresh

 Terminate Process Button

 m_bterminate

When the application is executed, it would display the list of processes and their information of local machine. To view the processes of remote machine, the user should enter the machine name, username and password and click the Refresh button. To terminate the process, the user should select the process from list view control and click the ‘Terminate Process’ button.

Let us first add code to the constructor of the Form1 class after the call to InitializeComponent( ) method to display processes of the current machine. 


ManagementClass mc = new ManagementClass ( @"root\cimv2:Win32_Process" ) ;
ManagementObjectCollection mobjects = mc.GetInstances( ) ;

addprocesses ( mobjects ) ;

The object collection returned by the GetInstances( ) method is passed to a user-defined function addprocesses( ). We have separated code to add the processes in a function, because we need to execute the same code again when user clicks the ‘Refresh’ button. The addprocesses( ) function is given below:


public void addprocesses ( ManagementObjectCollection moc )
{

m_plist.Items.Clear( ) ;

string[ ] items = new string [ 5 ] ;
foreach ( ManagementObject mo in moc )
{

items [ 0 ] = mo [ "Caption" ].ToString( ) ;
items [ 1 ] = mo [ "ProcessId" ].ToString( ) ;

int vs = Int32.Parse ( mo [ "WorkingSetSize" ].ToString( ) ) ;
vs = vs / 1024 ;
items [ 2 ] = vs.ToString( ) + " Kb" ;

if ( mo [ "ExecutablePath" ] == null )

items [ 3 ] = "NA" ;

else

items [ 3 ] = mo [ "ExecutablePath" ].ToString( ) ;


items [ 4 ] = mo [ "Priority" ].ToString( ) ;

ListViewItem lvitems = new ListViewItem ( items ) ;
m_plist.Items.Add ( lvitems ) ;

}

}

In this function, at first, we have removed all the previously added list items from the control by calling the Clear( ) method. Next, we have enumerated the objects from the collection passed to this function in a foreach loop. Each object in the collection represents a process. We would display the process name, process ID, memory the process is using, path of the EXE and priority of process in the list control. To obtain this information of the process, we have used various properties of the Win32_Process class. They are given below:

Caption: It returns the name of the process. 

ProcessId: It returns the ID of the process. 

WorkingSetSize: It returns the amount of memory (in bytes) the process is required to run efficiently. 

ExecutablePath: It returns the path of the process.

Priority: It returns the priority of the process.

To display the values returned by these properties, firstly we have created an array of five strings. Since the properties return value of type object we have converted them to strings. The WorkingSetSize property returns memory size in bytes. We have converted bytes into kilobytes before converting them to string. 

After populating the string array, we have created an object of the ListViewItem and passed the array to its constructor. The 0th element in the array will be treated as new list item, and rest of the elements as its sub-items. Then we have called the Add( ) method to add the new list item to the list control. 

So, when we execute the application, it would look as shown in the following figure.

We can now enter the machine name, user name and password and click the ‘Refresh’ button to view the processes of that machine. We would see the ‘Refresh’ button handler next time.

To be continued...

Top


Joke



Stammerer: "I hea..hea... heard tha...that you can hel...hel...help me".
Speech therapist: "Yes, sure. Ease yourself in the chair, look straight in my eyes, and count slowly till ten".
Stammerer: "O...one, t...two, th...th...three, ..... eight, nine, ten. It's wonderful, I don't stammer anymore!"
Speech therapist: "My fee is 300 dollar."
Stammerer: "H...h...how mu...mu...much?!"

Top


Different Strokes



Top