Archive for the 'C' Category

C

Deprecated methods in C#

A deprecated method is a method that is no more supported. For example, you wrote an API with a function that saves something on the disk but in the future versions of your program you want to support only Database saves. In this case you can deprecate the method that saves to disk for a lot of versions: the programmers are now warned from the compiler every time they use the function, and have the time to migrate the code.

stop

If you want to deprecate a method in c#, you can simply add the Obsolete attribute in the function.

[Obsolete("This is a deprecated method.")]
public void DeprecatedMethod()
{
	MessageBox.Show(”I’m a deprevated method! XD”);
}

Continue Reading »

C, MySQL

How to save a SQL Blob in C#

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

Today at work i had my daily dilemma: how can i save a blob field in a MySQL DB with c#??? The solution it’s pretty simple, but it can be hard to find.

The Blob is a binary field where data can be saved: the problem is that we must write these binary data in a textual SQL string. To resolve this problem we can convert the binary data in an hexadecimal representation in order to write the data in a textual format.

Binary data

The resulting SQL will look like that:

INSERT INTO tableWithBlob VALUES(1,0xab425fa52d3e13);

Where 0x… are our binary data.

To start, we must first convert the data structure that we want to put in the database in a byte array (byte[]), or in a format in which we can access all structure’s bytes.
Continue Reading »

C, Source Code, Windows

C++ Timer Class

Starting from the article “C++ timer”, we created a simply class that act as a wrapper to the Windows API and that easily allows to start a timer, stop it. and assign the function to call when the timer is elapsed.

Timer

The following is a little example that explains how to use the class:
Continue Reading »

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 »

C, MySQL, Source Code

Convert a C# Object to a SQL string

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

Every time that we need to create SQL queries in a programming language, often we do something like:

int id = GetTableID();
string place = GetPlace();
DateTime timestamp = GetLastActionDate();
string strTimestamp = Utils.GetSQLDate(DateTime.Now);
string sql = "INSERT INTO table VALUES(" + id + ",'" + place + "'," + timestamp + "')";

During the string creation we must add , ‘ and ” with attention.
In this article we will create a function that takes as input a c# Object and gives as output the formatted SQL String:

public static string GetSQLObject(object value)

Continue Reading »

.NET, C

How to use Visual Studio Snippets

Snippets are a fast and intuitive way to write ripetitive code in your project.

Let’s try the prop snippet to write a property.

Write prop in the code:

snippet property 1

Continue Reading »

« Prev