Thursday, January 29, 2009

[VC++] Programmatically checking internet connection is active

You might have thought of how to check your internet connection in your program, here is how to check for internet connection programmatically. Following are the two functions of interest,
InternetGetConnectedState()
InternetCheckConnection ()

InternetGetConnectedState()
This function Retrieves the connected state of the local system. It Returns TRUE if there is an active modem or a LAN Internet connection. Below code snippet explains how to use the function.
DWORD dwFlags = 0;
if( InternetGetConnectedState ( &dwFlags, 0 ))
{
if( dwFlags & INTERNET_CONNECTION_OFFLINE )
{
// Connected but in offline mode
}
else
{
// Connection is active
}
}
else
{
// No connection
}

InternetCheckConnection()
InternetGetConnectedState() indicates that at least one connection to the Internet is available. It does not guarantee that a connection to a specific host can be established and InternetCheckConnection() function can be called to check if a connection to a specific destination can be established. Below code snippet may explain you the same.
if( InternetCheckConnection( "http://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0 ))
{
// Connection to specified host exist
}
else
{
// Connection to specified host does not exist
}

[VC++] Programmatically shutdown, restart or logoff

If you are in need of shutting down, restart or log off you system using your VC++ code, then here are two APIs which will do exactly what you think !
ExitWindowsEx()
InitiateSystemShutdownEx()
Following section demonstrate ExitWindowsEx() function for shutdown, restart & log off using ExitWindowsEx,

Shutdown
// Shutdown forcefully
if( ExitWindowsEx( EWX_SHUTDOWN EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM ))
{
// Successfully requested for shutdown
}

Restart
// Restart forcefully
if( ExitWindowsEx( EWX_REBOOT EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM ))
{
// Successfully requested for reboot
}

Log Off
// Logging off forcefully
if( ExitWindowsEx( EWX_LOGOFF EWX_FORCE, SHTDN_REASON_MINOR_HUNG ))
{
// Successfully requested for loging off
}

EWX_FORCE flag is responsible for forceful operation request. Second parameter of ExitWindowsEx() specify the reason of exiting windows, check here for more flags/options. If you need a much user friendly Shutdown/Restart/Log off operation with a timer and a dialog box that prompts the user to log off you can go for InitiateSystemShutdownEx().

Monday, January 19, 2009

[.NET] Understanding ildasm & Reflector

When I was a newbie to .NET world one of the most interesting thing I find is .NET's Common Intermediate Language (CIL)code. You may be wondering whats special with an intermediate language code ! Ok let me explain,
When .NET languages are compiled, the source code is translated into CIL code rather than platform specific object code. CIL is a CPU/Platform-independent instruction set that can be executed in any environment supporting the .NET framework. Since CIL is defined and is generic it is obvious that it can be read and interpreted and reverse engineered. So it is the time to call your attention to two disassembler utilities Ildasm and Reflector.



ildasm
Intermediate Language Disassembler utility (ildasm.exe), is shipped with .NET Framework 3.5 SDK. It allows you to load any .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata. Take Ildasm from Microsoft Visual Studio 2008 Tools and if it is not available search ildasm.exe some where in the Visual studio install directories say C:\Program Files\Microsoft.NET\FrameworkSDK\bin. Follow the below stops,
1. Start ildasm and take File->Open menu.
2. Browse and select the assembly (.NET binary) for exploring.
3. Try exploring the details available (CIL code and assembly metadata)
Check out the below figure which shows exploring a Main() of an assembly using ildasm.




Reflector
Next most important and most popular object browser available is Reflector (originally written by Lutz Roeder). MSDN magazine named it as one of the Ten Must-Have utilities for developers ! So what's special with this ?
It allows to inspect, navigate, search, analyze, and browse the contents of a NET assembly and translates the intermediate language code to choosen .NET programming language, i.e Reflector allows you to decompile .NET assemblies into C#, Visual Basic, Delphi, MC++. It will show you the metadata, each call tree details, resources and XML documentation (what else you need ?). Follow the below steps and experience the wonder !
1. Download Reflector from here.
2. Take File->Open and browse for the .NET binary.
3. Select the class or function you want to explore.
4. For changing the Intermediate language code displayed to any other .NET language choose the same from the drop down. See the below snapshots of Reflector.




.NET Reflector's major use include tracking down performance problems and bugs, browsing classes, and familiarizing the code bases. It is also powerful in finding assembly dependencies, and even windows DLL dependencies, by using the Analyzer option. So try it and find what all you can do with Reflector !


Thursday, January 15, 2009

[.NET] Stepping to .NET

Who should read this ?
This is a write up targeted for the .NET beginners to understand the basic concepts of .NET to start with. This article contains the information I earned from my little experience in .NET, MSDN and some other books/sites.
.NET What is it ?
The Microsoft .NET Framework is a software technology that includes a large library of pre-coded solutions to common programming problems and a virtual machine that manages the execution of programs written specifically for the framework. We can divide Microsoft .NET to two,
>> Runtime environment to execute .NET application developed by any .NET complaint compiler.
>> SDK( Software Development Kit ) to develop .NET applications.
Why .NET ?
You may wonder why the .NET influenced our world with a much greater pace than any other programming technology/environment after C. Consider some of the existing programming languages and their disadvantages which made Microsoft thinking of .NET world.
>> C/Win32 API Programmer's life is a complex undertaking; Building application using the raw API is a difficult task because C developers are forced to do manual memory management, pointer arithmetic, bad looking syntactical constructs. After all it is not an Object oriented language.
>> Coming to C++/MFC programming it is upgraded to an object oriented programming language but it still has the manual memory management, complex syntatical constructs & pointer arithmetic.
>> Visual Basic 6 was designed to make life easy also it hides the complexity of raw Win32 API but VB6 is not a fully object oriented eventhough it is object aware making it less powerful.
>> Java/J2EE programming have the advantage of running platform independent with Java Virtual Machine. Eventhough it have many advantages the disadvantage is that cross language integration is not possible with Java.
>> Microsoft's COM made it possible to reuse binary code but it is not fully language independent for example deriving a class from existing COM class is not possible. Also COM will force to register and numerous deployment issues may be waiting for the developer.
Above disadvantages are solved with Microsoft.NET. Microsoft's initiative is an approach to make the programmer's life easier. The .NET Framework provides the following advantages (quoting MS):
1. A consistent, object-oriented programming environment.
2. A code-execution environment that: Promotes safe execution of code, Eliminates the performance problems of scripted or interpreted environments, Minimizes software deployment and versioning conflicts.
3. A consistent experience for both developers and users across various types of Windows-based and Web-based applications on multiple devices.
4. Communication built on the industry standards to ensure that code based on the .NET Framework can integrate with any other code.
Also .NET is based on open Internet standards, which include Hypertext Transfer Protocol (HTTP), Extensible Markup Language (XML), and Simple Object Access Protocol (SOAP).
Basic building blocks of .NET
Following are the basic building blocks which makes up .NET.
CLR(Common Language Runtime)
The Common Language Runtime (CLR) is the virtual machine component of the .NET framework. The core of the .NET Framework is the CLR; it manages code at execution time and provides core services such as memory management, thread management, remoting, and strict type safety enforcement. Code that targets the CLR is known as managed code, whereas code that does not target the CLR is known as unmanaged code. The most important part of the CLR is physically represented by a library named mscoree.dll (Common Object Runtime Execution Engine).
.NET Framework Base Class Library
The Microsoft .NET base class library is an object-oriented class library providing an integrated set of classes that expose the underlying functionality of the Win32 API as well as some other additional capabilities. These classes integrate tightly with the CLR. In Microsoft .NET library, all class (types) are grouped in namespaces (grouping of similar classes). Although the entire base class library has been broken into a number of discrete assemblies, the key assembly is mscorlib.dll.
CTS(Common Type System)
CTS is a formal specification that documents how data types must be defined in order to be hosted by the CLR. Only persons who are deeply concerned with the inner workings of the CTS are those building tools and/or compilers that target the .NET platform. The common type system supports two general categories of types,
Value types:
Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations.
Reference types:
Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types.
CLS(Common Language System)
The CLS is a set of rules that describe in vivid detail the minimal and complete set of features a given .NET-aware compiler must support to produce code that can be hosted by the CLR, while at the same time be accessed in a uniform manner by all languages that target the .NET platform. The CLS is ultimately a set of rules that compiler builders must conform to, if they intend their products to function seamlessly within the .NET universe.
A word about Visual Studio 2008
Microsoft started development on the .NET Framework in the late 1990s originally under the name of Next Generation Windows Services (NGWS). By late 2000 the first beta versions of .NET 1.0 were released and latest (at the time this is wrote) .NET Framework available is major version 3.5 (released on Nov 2007). When .NET Framework 3.5 was released Microsoft Visual Studio 2008 from Microsoft was released alongside .NET Framework 3.5 and it is the latest released development environment.
Below figure shows the overall architecture of the .NET Framework components,

CLR in Action !
Regardless of which .NET language you choose to program with, understand the fact that .NET binaries take the file extension *.dll or *.exe. When a *.dll or a *.exe has been created using a .NET-aware compiler, the resulting module is bundled into an assembly. .NET assemblies do not contain platform specific instructions, but platform independent Common intermediate language (CIL) and type metadata. Due to the fact that assemblies contain CIL instructions, rather than platform-specific instructions, CIL code must be compiled to platform specific instructions. One which compiles CIL code into CPU instructions is called as just-in-time (JIT) compiler (called as Jitter). Below figure illustrates the workflow that takes place between source code (which is made using base class library), specific .NET compiler for your source code, and the .NET execution engine.


Tuesday, January 6, 2009

[C++] Potential danger with global const objects

Its a common practise of most of us to use statements like the below for defining string constants for our applications.
const CString csSomeStr = "Some String"

Let me say the above declaration may eat your private bytes(VM Size) with out your knowledge. Suppose you have added the above global declaration statement in some header file and the same is,
- included in 10 cpp files then it will create 10 copies of the global constant objects (instead of 1 as we expect)
- if it is included in header files and those header files are included in some other files then the result will be more worse (the included header file will inherit the created copy characteristics for the const objects!!!).
Altogether the result will be that your module become heavy in terms of Private Bytes for no valid reason (even without using the declared global constants). One interesting thing is that your module will not even link successfully if you remove the const of the global variable and its header file is included in multiple files. After all let me say const objects will be created by calling constructor for the above case; csSomeStr will
allocate heap memory for the "Some String" and will be copied inside the constructor.
For confirming the above facts let me show you an example that you can try out through below steps,
1. Create an SDI application (say TestGlobalObj) using MFC Classwizard then build & execute, the VM Size may be approximately some
564 K
2. Now create a header file common.h with below content. Please note that we are not referring this object !
#ifndef _COMMON_H_
#define _COMMON_H_

const CString csTempStr[1024*1024] = {"Test string"};
#endif
3. Include the common.h in the View class header file (say TestGlobalObjView.h) now build & execute, VMSize may be approximately 10404 K
4. Now include common.h in the Doc class header file (say TestGlobalObjDoc.h) and build & execute, VMSize may be approximately 15326 K
5. You put a break point at the above declaration of the csTempStr and you can find that this will be executed twice and CString constructor will be called each time for initializing the object.
6. Now modify the common.h with below content and build & execute to find the memory well around
564 K
#ifndef _COMMON_H_
#define _COMMON_H_
const LPCTSTR csTempStr[1024*1024] = {"Test string"};
#endif

So from this you will be clear about the hidden danger on declaring constant global objects, (danger with very common global constant CString objects) Always prefer primitive data types for global constants where ever possible. If you find your module is consuming more start up memory than expected then check for the global constant objects and confirm primitive data types are used wherever possible and unwanted objects are not kept global and included in the header files.

[VC++] Memory info with GetProcessMemoryInfo()

If you need to know the memory usage information of a process programmatically for some tracing purpose or for debugging purpose you can use GetProcessMemoryInfo() API. This API retrieves information about the memory usage of the specified process. Following are the memory statics of a process returned,
PageFaultCount
PeakWorkingSetSize
WorkingSetSize
QuotaPeakPagedPoolUsage
QuotaPagedPoolUsage
QuotaPeakNonPagedPoolUsage
QuotaNonPagedPoolUsage
PagefileUsage
PeakPagefileUsage


Below code snippet will show how to use the same for displaying the memory statics Page File Usage(VM Size) and Peak Page File Usage of the current process in Kilo Bytes. you can use the same logic for other counters also.

#include "psapi.h"
.......
.......
PPROCESS_MEMORY_COUNTERS pMemCountr = new PROCESS_MEMORY_COUNTERS;
if( GetProcessMemoryInfo(GetCurrentProcess(),pMemCountr, sizeof(PROCESS_MEMORY_COUNTERS)))
{
CString csMsg;
csMsg.Format(" PagefileUsage = %uK\n PeakPagefileUsage = %uK", pMemCountr->PagefileUsage/1024, pMemCountr->PeakPagefileUsage/1024 );
MessageBox( csMsg, "Process Memory Information" );
}
delete pMemCountr;

Output Message Box may look something like this for a simple dialog based application,



Please note that this API is declared in the psapi.h so you may need to include the same and need to link to psapi.lib.

[General] Perfmon for windows performance metrics

Perfmon(Performance Monitor) is a Microsoft utility which includes Performance counters for monitoring different aspects of system performance. You can use these counters to measure both hardware and software performance. Not only a real time value updation/graphical display of the monitoring counters but also performance logs can be saved. These Performance logs will help you know the patterns of a specific counter for a period of time, and help you locate/diagnose bottlenecks that can slow down your system. Following sections explains how to use performance monitor for watching performance counters and how to save it to file.

Starting Perfmon & adding counters
1). Go to Start menu, select Run. Type perfmon and press OK.
2). Perfmon by default display/monitor the counters,
-Pages/Sec(Memory)
-Avg.Disk Queue Length(Physical Disk)
-% Processor Time(Processor).
3). For adding new counters right click and select Add Counters. Select Performance Object from the drop down, select Performance Counter from the list and Instances from the list. Press Add button and Close the Add Counters dialog. Now you can see the value & graph plotting.

Perfmon main window

Adding Counter


Select Object,Counters & instances
Saving Log file
1). Expand Performance Logs and Alerts tree item in the left pane of the Perfmon and select Counter logs.
2). Right click on the Perfmon right pane shown and choose New Log Settings. Enter the name of the log you want to have and press OK.
3). Press the Add Counters button on the log configuration dialog shown and choose the needed counters to monitor and save to file.
4). You can configure many options such as data capture Interval, Log file type, Schedule etc in the Log Configuration dialog.


Adding new log setting


Adding counter for logging and configuring.