How to enable Yahoo Smiley search?

A smiley or happy face , is a stylized representation of a smiling human face, commonly represented as a yellow (many other colors are also used) circle (or sphere) with two black dots representing eyes and a black half circle representing the mouth. “Smiley” is also sometimes used as a generic term for any emoticon.
Nowadays the smileys are used in so many applications, such as desktop application, web application. You can see them everywhere, in your chat application, forums, blogs, etc… I use smiley for expressing my emotion when I am using Yahoo Messenger Application. However it is for me very difficult to remember all of codes of the smileys. They are for me little same. Code are combined of some normal character and special character. For example code starting with “:” are “:)”, “:(“, “:-/”, “:x”, “:”>” “:-p”, “:-*”, “:-o”. How can I distinguish between them? Luckily Yahoo Messenger provides us a useful function so that we can search smiley which we want. To enable this feature just download this SmileySearch.reg file, click on it and you have this search feature on in your chat dialog. From now on, we need to tip some symbols relevant to smiley and choose it from the suggested list.

During enabling this feature I found that there is a command allowing us to export a value (not complete key) in a registry to file. To make the .reg file, I exported it from registry with following command “REG QUERY HKCU\Software\Yahoo\Pager /v SmileySearch > SmileySearch.reg“. There are still a lot of available options for this command. You can study more about this command with option “/?”.
And at the end I would like to give you the links to all of smileys with their codes and meanings in Yahoo Messenger (up to date). Enjoy yourself with chatting.
Emoticons : http://messenger.yahoo.com/features/emoticons/
Hidden emoticons: http://messenger.yahoo.com/features/hiddenemoticons/

C# – Auto restart application after given idle time

What would you do if you want to restart your application after given idle time or log out your users out of your system. Surely you’ll think of setting a timer and process its Tick function to calculate the idle time and then take appropriate processing steps. So I would like to introduce a built-in Windows API function which gives us the last time when the user does something on our application. This API is called GetLastInputInfo. The GetLastInputInfo function retrieves the time of the last input event. This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function. You’ll need Windows 2000 as minimum OS to use it.
The following code snippet shows a small demonstration how this API works in C# format. First, you need to declare its signature

[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

The struct LASTINPUTINFO has following structure. It contains the time of the last input. cbSize must be set to sizeof(LASTINPUTINFO) and dwTime is the tick count when the last input event was received.

[StructLayout( LayoutKind.Sequential )]
struct LASTINPUTINFO
{
	public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

	[MarshalAs(UnmanagedType.U4)]
	public int cbSize;
	[MarshalAs(UnmanagedType.U4)]
	public UInt32 dwTime;
}   

And now we tinker them together to archive a working code.

static int GetLastInputTime()
{
	int nIdleTime = 0;
	LASTINPUTINFO liiInfo = new LASTINPUTINFO();
	liiInfo.cbSize = Marshal.SizeOf(liiInfo);
	liiInfo.dwTime = 0;
	int nEnvTicks = Environment.TickCount;
	if (GetLastInputInfo(ref liiInfo))
	{
		int nLastInputTick = liiInfo.dwTime;
		nIdleTime = nEnvTicks - nLastInputTick;
	}
	return ((nIdleTime > 0) ? (nIdleTime / 1000) : nIdleTime);
}  

The result of this function gives idle time in seconds. You should convert it to appropriate time format which you need and to restart you application call simply “Form.Restart” in your code. As you can see, the more we know about OS, the little code we must write and therefore we have more time to enjoy our life.

C# – How to determine processor 64 bit or 32 bit?

There are a lot of ways to find out which type of processor of your computer. Is it a 64 bit or 32 bit type? I would like to enumerate some methods which I know. You can use what you want.

1. Using environment variable

string strProcArchi = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
if (strProcArchi.IndexOf("64") > 0)
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

2. Using size of IntPtr (recommended)

if ( Marshal.SizeOf(typeof(IntPtr)) == 8 )
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

3. Using WMI

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
	if (queryObj["Architecture"].ToString() == "9")
	{
		Console.WriteLine("Processor 64 bit");
	}
	else
	{
		Console.WriteLine("Processor 32 bit");
	}
}

4. Reading registry

RegistryKey rkIdentifier = Registry.LocalMachine.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
if (rkIdentifier.GetValue("Identifier").ToString().IndexOf("64") > 0)
{
	Console.WriteLine("Processor 64 bit");
}
else
{
	Console.WriteLine("Processor 32 bit");
}

5. Using GetSystemInfo API

[DllImport("kernel32.dll")]
public static extern void GetSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
	internal _PROCESSOR_INFO_UNION uProcessorInfo;
	public uint dwPageSize;
	public IntPtr lpMinimumApplicationAddress;
	public IntPtr lpMaximumApplicationAddress;
	public IntPtr dwActiveProcessorMask;
	public uint dwNumberOfProcessors;
	public uint dwProcessorType;
	public uint dwAllocationGranularity;
	public ushort dwProcessorLevel;
	public ushort dwProcessorRevision;
}

[StructLayout(LayoutKind.Explicit)]
public struct _PROCESSOR_INFO_UNION
{
	[FieldOffset(0)]
	internal uint dwOemId;
	[FieldOffset(0)]
	internal ushort wProcessorArchitecture;
	[FieldOffset(2)]
	internal ushort wReserved;
}
public enum eOperatingSystem
{
	X64,
	x86,
	Itanium,
	Unknown
}

public eOperatingSystem DeterminateOperatingSystem()
{
	eOperatingSystem result = eOperatingSystem.Unknown;
	SYSTEM_INFO l_System_Info = new SYSTEM_INFO();
	GetSystemInfo(ref l_System_Info);

	switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
	{
		case 9: // PROCESSOR_ARCHITECTURE_AMD64
		result = eOperatingSystem.X64;
		break;
		case 6: // PROCESSOR_ARCHITECTURE_IA64
		result = eOperatingSystem.Itanium;
		break;
		case 0: // PROCESSOR_ARCHITECTURE_INTEL
		result = eOperatingSystem.x86;
		break;
		default: // PROCESSOR_ARCHITECTURE_UNKNOWN
		result = eOperatingSystem.Unknown;
		break;
	}
	return result;
}

6. Using IsWow64Process API

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); 
...
bool bIsWow64;
bool bSuccess = IsWow64Process(Process.GetCurrentProcess().Handle, out bIsWow64);
if (!bSuccess)
	Console.WriteLine("Processor 32 bit");
else
{
	if (bIsWow64)
	{
		Console.WriteLine("Process 32 bit runs on processor 64 bit");
	}
	else
	{
		Console.WriteLine("Process 64 bit runs on processor 64 bit");
	}
}

If you know another way, then post here your code. I would like to know many other methods too.

ARP -a – Display cache of ARP entries

Today I read a thread on forum hvaonline asking about some strange IP addresses when using the command “arp -a” to see the cache of ARP entries. The author of this thread is using Windows 7 and he thought that his computer was hacked. When I use this command on my machine, I also see some strange IP addresses. I never think that my computer was hacked so I decide to find out what does these IP addresses mean.

The figure above shows the result of the command. Let’s go through each row and explain them.

1. “Interface: 192.168.1.2 — 0xb”
The “192.168.1.2” is my IP address. That is also the only one network interface working now on my computer.

2. “192.168.1.1 00-22-**-**-**-e2 dynamic”
The “192.168.1.1” is the IP address of my router and “00-22-**-**-**-e2” is his MAC Address. Its type is dynamic. That means that depends on which router you use, you’ll get various IP Address. For example, I am now using a netgear router which normally use 192.168.1.1. But some years ago, I used an Asus router which normally use 192.168.123.1.

3. “192.168.1.3 00-16-**-**-**-8b dynamic”
The “192.168.1.3” is the IP address of the computer of my wife. She has a laptop and the MAC address is of course the MAC of her laptop. The type is of this address is dynamic, too. Normally which computer connects to the router first, will get the lower address. In this example, my desktop got connection first. It got the “192.168.1.2”. Then the computer of my wife got the “192.168.1.3”. After that if another computer gets connection, he’ll got the “192.168.1.4” and so on.

4. “192.168.1.255 ff-ff-ff-ff-ff-ff static”
The “192.169.1.255” is a broadcast address. It allows information to be sent to all host of network, rather than a specific network host. This address is generally obtained by performing a bitwise OR operation between the bit complement of the subnet mask and the ip address. In this example, my subnet mask is “255.255.255.0” and its bit complement is “0.0.0.255” and therefor the broadcast address should be (192.168.1.0 | 0.0.0.255) = 192.168.1.255.
We can see the MAC address of this ip is a range of “ff” because broadcast is possible also on the underlying Data Link Layer. Ethernet frames can be addressed to every computer on a given LAN segment if they are addressed to MAC address FF:FF:FF:FF:FF:FF. Ethernet frames that contain IP broadcast packages are usually sent to this address.

5. “224.0.0.22 01-00-5e-00-00-16 static”, “224.0.0.252 01-00-5e-00-00-fc static”, “224.0.0.253 01-00-5e-00-00-fd static”
The ip range 224.0.0.0 through 239.255.255.255 is for multicast. Multicast addressing is a network technology for the delivery of information to a group of destinations simultaneously using the most efficient strategy to deliver the messages over each link of the network only once, creating copies only when the links to the multiple destinations split.
In our example, windows 7 automatically turned on 3 multicast addresses. Each of them has its own mission.
224.0.0.22 IGMP [Deering]
224.0.0.252 Link-local Multicast Name Resolution [RFC4795]
224.0.0.253 Teredo [RFC-huitema-v6ops-teredo-05.txt]
For more information about multicast address you can find as this link http://www.iana.org/assignments/multicast-addresses/

6. “239.255.255.250 01-00-5e-7f-ff-fa static”
Windows OS uses 239.255.255.250 as a sort of broadcast address to announce the presence of a machine on the network. Windows uses the following Universal Plug and Play services:
* The Simple Service Discovery Protocol (SSDP) discovery service: This service discovers Universal Plug and Play devices on your home network.
* Universal Plug and Play Device Host: This service provides support to host Universal Plug and Play devices.
And the SSDP will be host on the ip 239.255.255.250 exactly at port 1900. For more information you can find at this article http://support.microsoft.com/kb/317843/en-us

C# – Treeview databinding with 3 layers model programming

As I reviewed database programming techniques with C#, I discovered that I have never used TreeView to display my data so I decide to make a small practice with loading data into TreeView and show sub-information on the other component when clicking on its node. The TreeView has no DataSource and DataBinding property as ComboBox and ListBox does. Therefore we must load data ourselves manually, node by node. I know this is very simple example. I believe that all of programmers can do it. So do not take this example as working with Treeview but take it as a review about “3 layers model prgramming”.

The picture below figures out the main idea of “3 layers model programming”.

Our application will consist of 3 layers: Data Access Layer, Business Logic Layer and Presentation Layer (User Interface).

  • A Data Access Layer (DAL) provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database. For example, the DAL might return a reference to an object (in terms of object-oriented programming) complete with its attributes instead of a row of fields from a database table. This allows the client (or user) modules to be created with a higher level of abstraction.  The DAL hides the complexity of the underlying data store from the external world. In the code snippet below, DAL return the list of employees and completely hide the structure of database. The external world will only receive what this layer gives although in the database there are still a lot of information of each employee not only his name, his id and his function.
class DatabaseAccessLayer
 {
 public List<Employees> GetEmployees()
 {
 List<Employees> lstEmployees = new List<Employees>();
 dbConnection dbConn = new dbConnection();
 DbDataReader reader = dbConn.ExecuteSQLCommand("SELECT * FROM Personal");
 while (reader.Read())
 {
 lstEmployees.Add(new Employees(reader["Nachname"] + "", reader["Vorname"] + "", reader["PersonalNr"] + "", reader["Funktion"] + ""));
 }
 reader.Close();
 return lstEmployees;
 }
 }
  • A Business logic layer (BLL) is a software engineering practice of compartmentalizing the rules and calculations of a software application from its other design elements. For example, in an application with a properly separated Business logic layer and Data access layer, the Data access layer could be rewritten to retrieve data from a different database, without affecting any of the business logic. This practice allows software application development to be more effectively split into teams, with each team working on a different tier simultaneously. Let’s see the source code below of BLL. BLL has no idea what DAL does with database. He just takes the result of DAL and does his work. Completely blind with each other.
class BusinessLogicLayer
 {
 DatabaseAccessLayer dalLayer = new DatabaseAccessLayer();
 List<Employees> m_lstEmployees = new List<Employees>();

 public BusinessLogicLayer()
 {
 m_lstEmployees = dalLayer.GetEmployees();
 }
 public Employees GetUserByLastName(string LastName)
 {

 for (int nIndex = 0; nIndex < m_lstEmployees.Count; nIndex++)
 {
 if (m_lstEmployees[nIndex].LastName == LastName)
 {
 return m_lstEmployees[nIndex];
 }
 }
 return null;
 }

 public List<Employees> GetAllEmployees()
 {
 return m_lstEmployees;
 }
 }
  • Presentation Layer (User Interface) takes over the mission to show what user should see.
private void btnLoad_Click(object sender, EventArgs e)
 {

 for (int nIndex = 0; nIndex < m_bllLayer.GetAllEmployees().Count; nIndex++)
 {
 trvEmployee.Nodes.Add(m_bllLayer.GetAllEmployees()[nIndex].LastName);
 }
 }

 private void trvEmployee_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
 {
 Employees empTemp = m_bllLayer.GetUserByLastName(e.Node.Text);
 lblEmployeeID.Text = empTemp.EmployeeID;
 lblFirstname.Text = empTemp.FirstName;
 lblFunction.Text = empTemp.Function;
 }

At the end we have a small example with TreeView.

When clicking Load-Button, the TreeView loads data from table “Personal” from database Nordwind.mdb and show last name of each employee on each node. Clicking on node the sub-info of an employee will be shown on right labels. You can download the source code of “TreeView databinding with 3 layers model programming” as reference.