CLastError

Small class for resolving and presenting in user readable form the codes from ::GetLastError() Win32 API function. The class has 2 static methods:

1
2
3
4
5
6
7
8
9
10
void CLastError::Get( const DWORD& dwErrorCode, CString& strErrMessage )
{
	LPTSTR lpErrorText = NULL;
 
	::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
		0, dwErrorCode, 0, lpErrorText, MAX_PATH, 0 );
 
	strErrMessage = lpErrorText;
	::LocalFree( lpErrorText );
}

The ‘Get’ method supplied an error code will resolve the code to user readable error message and store it in the supplied ‘strErrMessage’ string!

1
2
3
4
5
6
7
8
9
10
11
12
void CLastError::Show( const DWORD& dwErrorCode, const CString& strAppName )
{
	CString strErrMessage;
	Get( dwErrorCode, strErrMessage );
 
	CString strDisplayError;
	strDisplayError.Format( 
		_T("%s encountered an error and needs to close!\n\nError was: %s"),
		strAppName, strErrMessage );
 
	AfxMessageBox( strDisplayError, MB_ICONERROR | MB_OK );
}

The ‘Show’ method supplied an error code will resolve the code to user readable error message and the message will be displayed to the user trough AfxMessageBox() function.

Class declaration:

class CLastError
{
public:
 
	CLastError( );
	virtual ~CLastError( );
 
public:
 
	static void Get( const DWORD& dwErrorCode, CString& strErrMessage );
	static void Show( const DWORD& dwErrorCode, const CString& strAppName );
};

Class implementation:

CLastError::CLastError( )
{
}
 
CLastError::~CLastError( )
{
}
 
void CLastError::Get( const DWORD& dwErrorCode, CString& strErrMessage )
{
	LPTSTR lpErrorText = NULL;
 
	::FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
		0, dwErrorCode, 0, lpErrorText, MAX_PATH, 0 );
 
	strErrMessage = lpErrorText;
	::LocalFree( lpErrorText );
}
 
void CLastError::Show( const DWORD& dwErrorCode, const CString& strAppName )
{
	CString strErrMessage;
	Get( dwErrorCode, strErrMessage );
 
	CString strDisplayError;
	strDisplayError.Format( 
		_T("%s encountered an error and needs to close!\n\nError was: %s"),
		strAppName, strErrMessage );
 
	AfxMessageBox( strDisplayError, MB_ICONERROR | MB_OK );
}

Unknown Undo/Redo Error

This happened to me couple a days ago using Visual Studio .NET 2003 developing an old ASP.net application in Windows  XP Mode. Using product released 9 years ago on a newer OS has it’s charms.

Unknown undo / redo error.

How to fix it: Re install the IDE ( on 2 out of 3 machines this seems to resolve the problem )  or better, upgrade your project to more recent version of Visual Studio and enjoy all the benefits. As for the company I work for we finally did the upgrade ( Visual Studio 2010 )

vHash

vHash is small Windows utility for generating hashes using various cryptography providers and algorithms that they support such as MD-5, SHA-1, etc. It’s written on C using Win32 API making it light and dependency free.

Currently the utility does not support keyed hash algorithms such as HMAC or MAC. Also the program is Unicode.

vHash

Downloads and more information are available on the vHash project page.

CLRF in SQL Server

An interesting problem I was facing was adding CLRF on a string in SQL Server. The solution was to add a combination of chars ( CHAR(13) + CHAR(10) ) where the string should break the line. The follwing example will add CLRF in the string being updated.

UPDATE NM196 SET 
        OSNOVANIE = 
	'Вал. превод Реф.@@500' + CHAR(13) + CHAR(10) + 
	'сума @@547 @@502 @@542' + CHAR(13) + CHAR(10) + 
	'ориг.сума @@501' + CHAR(13) + CHAR(10) + 
	'вальор @@505 ' + CHAR(13) + CHAR(10) + 
	'Наредител: @@522 ' + CHAR(13) + CHAR(10) + 
	'Бенефициент: @@526 ' + CHAR(13) + CHAR(10) + 
	'Основание: @@535 @@546'
WHERE CODE = 295 AND NOMID = 196

Windows 7 USB installation

Due to the enormous amount of software installs and uninstalls I decided it’s about time to reinstall my desktop machine. Having no spare DVD around my installation media of choice was a USB key.

Installing windows 7 from USB key is quite fast actually. Here is how to prepare Windows 7 bootable USB.

The requirements
NOTE: My installation was Windows 7 Professional SP1 x64. After completion of this process the space occupied on the USB key was 3,11 GB.

You will need a flash drive around 4 GB, an ISO image or DVD of Windows 7 and a machine running Windows XP or later. Also an archive tool able to extract ISO files. I use 7-Zip.

Preparing the installation media

1. Formatting the USB drive
Plug in the USB memory stick, right click on the drive in ‘Computer’ and select ‘Format…’. I recommend doing complete format by removing the check box next to ‘Quick Format’ option.

format

Depending on the drive size and speed the format will take some time.

2. Making the USB drive bootable
For this step we will use a command line utility called Diskpart. It’s bundled with every Windows version from 2000 onward.

To start Diskpart, click on the Start button and type diskpart. The diskpart utility will appear. Give it some time to load and enumerate your drives.

NOTE: For complete list of commands that the utility supports type: help at the DISKPART> command prompt.

diskpart1

To list your drives type list disks. From the list of your drives find the USB stick by comparing the size of the drive with the size of your USB stick. In my case the flash drive was ‘Disk 3’

Untitled3

At the ‘DISKPART>’ command prompt select your USB drive by entering the following command where # is the drive number of your flash drive:

DISKPART> select disk #

Now type in the following sequence of commands:

DISKPART> clean
DISKPART> create partition primary
DISKPART> active

Close the utility by typing:

DISKPART> exit

Your flash drive is now bootable. It’s time to copy the windows installation files on it.

3. Coping the Windows 7 installation

Extract the Windows 7 iso image to a location of your choice. If you are using 7-zip right click on the .iso image select 7-zip and from the sub menu select ‘Extract files…’

NOTE: If you are turning Windows 7 DVD in to a bootable Flash drive installation just copy the contents of the DVD on the flash drive.

Untitled7

Copy the contents of the extracted .ISO file on your USB drive as you normally would.

Untitled8 
Now you have a bootable Windows 7 installation. Restart your computer and set it to boot from the USB. Install Windows 7 as you normally would.

This method is applicable also for Windows 2000, XP and Vista.