Archive for May, 2007

C, Source Code, Windows

Detect when an usb device is inserted or removed.

Normally the autorun in usb storage devices like pendrives is disabled for security reasons, so i wrote a little utility in c++ to launch a custom application in my pendrive when I insert it in a usb port.

Logo usb

Normally if you want to detect when a “hotplug” device has been inserted or removed from your system, you have to intercept the system message “WM_DEVICECHANGE” in “WndProc” function of the main window of your application.
Continue Reading »

C, Windows

C++ Timer

To create a timer in c++ on Windows we must invoke the SetTimer() method, defined in windows.h.

UINT_PTR SetTimer(
// handle to the window associated to the timer
HWND hWnd,
// Timer ID
UINT_PTR nIDEvent,
// Elapse time in milliseconds
UINT uElapse,
// callback method to invoke when the timer expires
TIMERPROC lpTimerFunc
);

The hWnd argument is the handle to a window: for semplicity we don’t use any Handle, so we set it to NULL.

The function always returns an uint that represents the ID of the timer. if hWnd is NULL, the second parameter will be ignored and we can’t directly set the timer ID, but it will be assigned from the function. We can check if the creation of the timer didn’t have any problem if the uint returned from the function it’s not zero. Instead, if hWnd is not null, the timer ID will be assigned from the nIDEvent parameter

Add a global variable (ok, it’s not a good programming pratice adding global variabile, but this is only an howto) where we can save the timer ID:

uint m_timerID;

Continue Reading »

C, Source Code

Optimized Access to Bitmaps

If we need image processing in c#, the fastest way for a programmer to access the pixels of a Bitmap is to use the SetPixel() and GetPixel() .NET methods. Unfortunately the fastest way for a programmer, often does’nt correspond to the fastest way for the program: These functions are REALLY slow, so if your objective is to analyze all pixels of an image, or, still worse, to make an elaboration of many images, you are not adviced to use them. Fortunately the c# is a very language flexible: It allows to make things at high levels, but often gives the opportunity to do low-level programming when is necessary.

For semplicity, in this article i will deal only the images at 8bpp (8 bit per pixel): every byte will be a pixel.

The only way to work fast with an image is to use the pointer to the image data. Work with the pointers is like working with the char * in c++: Initialliy, the pointer will point to the memory address of the first pixel of the bitmap. Increasing the pointer we would gradually obtain the values of the next pixels.
Pointer to image example
Continue Reading »

.NET, C, Source Code

CRC in C#

This is a post about the CRC (Cyclic Redundancy Check) in C#. I searched through the internet a C# class that can calculate the crc-12, but I found only links about crc-8, crc-16 and crc-32.

binary matrix

After an hard search, i founded a class that can calculate the crc from 1 to 32 (this is the link), I cleaned it and I added:

  • The crc-12 standard
  • A class that simplifies the insert of the values (CRCSettings)
  • A function that generate non-standard polynomys.

Continue Reading »