Just Like a Magic

February 8, 2010

BOOL or BOOLEAN?

Filed under: Data Types — Tags: , , , , — Mohammad Elsheimy @ 8:50 PM

Windows comes with two types that represent a Boolean variable (TRUE or FALSE.) Both represent FALSE if 0 and TRUE if non-zero.

The big difference you need to care when working with that two Booleans is that BOOL defined as int which is 32 bits (4 bytes) on 32-bit environments and 16 bits (2 bytes) on 16-bit environments. BOOLEAN on the other hand, defined as BYTE, which in turn defined as unsigned char. Thus, BOOLEAN only occupies 8 bits (1 byte) from memory.

Although you can convert between them easily, BOOL is much common than BOOLEAN and it is very popular in the Windows API.

Now, the decision is yours!

February 4, 2010

Download Microsoft Visual Studio 2010 Beta 2

Filed under: Visual Studio 2010 — Tags: , — Mohammad Elsheimy @ 5:17 PM

Now you can with with Visual Studio 2010 Beta 2

What’s new in 2010?

  • Set your ideas free
    Create what you can imagine, build on the strengths of your team, and open up new possibilities.
  • Simplicity through integration
    A single integrated development environment that takes your skills further and adjusts to the way you work.
  • Quality tools help ensure quality results
    Powerful testing tools with proactive project management features help you build the right app the right way.

Get it Now!

Microsoft Win32 to Microsoft .NET Framework API Map

Filed under: Win32 API — Tags: , , — Mohammad Elsheimy @ 4:57 PM

This MSDN article identifies the .NET Framework APIs that provide similar functionality to those of Microsoft Win32 API.

Click here to visit the article.

January 13, 2010

A short speech about Unions

Filed under: Data Types — Tags: , , , — Mohammad Elsheimy @ 3:10 PM

Contents

Contents of this article:

  • Contents
  • Definition
  • Unions and Structures
  • Example
  • Results
  • Unions Usefulness
  • Unions and the API

Definition

A union is a memory location that is shared by two or more different types of variables. A union provides a way for interpreting the same bit pattern in two or more different ways (or forms.)

Unions and Structures

In fact, unions share structures lots of characteristics, like the way they defined and marshaled. It might be helpful to know that, like structures, unions can be defined inside a structure or even as a single entity. In addition, unions can define complex types inside, like structures too.

Example

To understand unions, we will take a simple example. Consider the following union:

typedef union CHARACTER
{
	int i;
	char c;
};

This was a simple union defines a character. It declared two members, i and c, it defined them in the same memory location. Thus, it provides two ways for accessing the character, by its code (int) and by its value (char). For this to work it allocates enough memory storage for holding the largest member of the union and that member is called container. Other members will overlap with the container. In our case, the container is i because it is 4 bytes (on Win32, 16 on Win16), while c is only 1 byte. Figure 1 shows how the memory is allocated for the union.

Figure 1 - CHARACTER union into memory

Results

Because the two members are sharing the same memory location, when you change one member the other is changed too. Consider the following example:

int main()
{
	union CHARACTER ch;

	ch.i = 65;				// 65 for A
	printf("c = %c", ch.c);	// prints 'A'
	printf("\n");

	ch.c += 32;				// 97 for a
	printf("i = %d", ch.i);	// prints '97'
	printf("\n");

	return 0;
}

When you change any of the members of the union, other members change too because they are all same the same memory address.

Now consider the same example but with values that won’t fit into the char member:

int main()
{
	union CHARACTER ch;

	ch.i = 330;
	printf("c = %c", ch.c);	// prints 'J'
	printf("\n");		// Ops!

	ch.c += 32;
	printf("i = %d", ch.i);	// prints '362'
	printf("\n");

	return 0;
}

What’s happened? Because char is 1 bye wide, it interprets only the first 8 bits of the union that are equal to 32.

The same rule applies if you add another member to the union. See the following example. Notice that order of member declarations doesn’t matter.

int main()
{
	union {
		int i;
		char c;
		short n;
	} ch;

	ch.i = 2774186;

	printf("i = %d", ch.i);
	printf("\n");
	printf("c = %i",
		(unsigned char)ch.c);
	printf("\n");
	printf("n = %d", ch.n);
	printf("\n");

	return 0;
}

Now, i, the container, interprets the 32 bits. c, interprets the first 8 bits (notice that we converted it to unsigned char to not to show the negative value.) n, interprets the first high word (16 bits.)

Unions Usefulness

You might ask: Why I need unions at all? I could easily use the cast operator to convert between data types!

The answer is very easy. Unions come very efficient when casting between types require much overhead. Consider the following example: You are about to write an integer to a file. Unfortunately, there’s no function in the C standard library that allow you to write an int to a file, and to using fwrite function requires excessive overhead. The perfect solution is to define a union that contains an integer and a character array to allow it to be interpreted as an integer and as a character array when you need to pass it to fwrite for example. See the following code snippet:

union myval{
	int i;
	char str[4];
};

In addition, unions offer you more performance than casts. Moreover, your code will be more readable and efficient when you use unions.

Unions and the API

Unions exist throughout the API, however, they are usually declared inside structures and not as a single unit. A good example is the DEVMODE structure.

Why is it preferred using unions inside structures? It doesn’t make much sense if they were a single unit. For our example, you could easily convert the integer to a character rather than creating a union. However, the efficiency of unions comes when they are declared inside structures. In addition, you gain more performance (and scalability of course) when you

January 7, 2010

Introducing Marshaling

Filed under: Marshaling — Tags: , , , , , — Mohammad Elsheimy @ 2:58 PM

What is Marshaling?

Marshaling is the process of creating a bridge between managed code and unmanaged code; it is the homer that carries messages from the managed to the unmanaged environment and reverse. It is one of the core services offered by the CLR (Common Language Runtime.)

Because much of the types in unmanaged environment do not have counterparts in managed environment, you need to create conversion routines that convert the managed types into unmanaged and vice versa; and that is the marshaling process.

As a refresher, we call .NET code “managed” because it is controlled (managed) by the CLR. Other code that is not controlled by the CLR is called unmanaged.

Why Marshaling?

You already know that there is no such compatibility between managed and unmanaged environments. In other words, .NET does not contain such the types HRESULT, DWORD, and HANDLE that exist in the realm of unmanaged code. Therefore, you need to find a .NET substitute or create your own if needed. That is what called marshaling.

An example is the unmanaged DWORD; it is an unsigned 32-bit integer, so we can marshal it in .NET as System.UInt32. Therefore, System.UInt32 is a substitute for the unmanaged DWORD. On the other hand, unmanaged compound types (structures, unions, etc.) do not have counterparts or substitutes in the managed environment. Thus, you’ll need to create your own managed types (structures/classes) that will serve as the substitutes for the unmanaged types you use.

When I Need to Marshal?

Marshaling comes handy when you are working with unmanaged code, whether you are working with Windows API or COM components. It helps you interoperating (i.e. working) correctly with these environments by providing a way to share data between the two environments. Figure 1 shows the marshaling process, where it fall, and how it is required in the communication process between the two environments.

Figure 1 - The Marshaling Process

September 13, 2009

Happy Programmer Day! Thank You Programmers!

Filed under: Just Like a Magic — Tags: — Mohammad Elsheimy @ 3:30 PM

Programmer DayToday on the 256s day of the year we are celebrating Programmers and thank them for all they do. The history of Programmer Day is longer than the 2 year life of this site, but unfortunately a lack of comments and poor documentation have obfuscated that history.

Now, you can visit ProgrammerDay.info that was created to promote and provide a home for the day.

Would Bill Gates wears Santa Claus and gives us free versions of Visual Studio Team System and SQL Server Enterprise? hmmmm…

Happy Programmy Day!

Thank You All Programmers!

September 10, 2009

Poll: How do you access your twitter account?

Filed under: Polls — Tags: , — Mohammad Elsheimy @ 4:27 PM

August 21, 2009

Book: Network Programming for the Microsoft .NET Framework

Filed under: Books on Networking — Tags: , , — Mohammad Elsheimy @ 11:02 PM
Network Programming for the Microsoft .NET Framework

Network Programming for the Microsoft .NET Framework

Anthony Jones , Jim Ohlund , and Lance Olson

0-7356-1959-X

Download Here

We made every effort to bring these books to you. If you found a bad link please report it to us as soon as possible.

In addition, you can find lots of books here.

August 20, 2009

ILMerge 2.9.0727 is now available

Filed under: Developer Tools — Tags: , , — Mohammad Elsheimy @ 9:22 PM

Latestly, Mike Barnett the developer of ILMerge released the new version 2.9.0727 which fixes major problems that was exist in the previous version 2.9.0210.

ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly. ILMerge takes a set of input assemblies and merges them into one target assembly.

Read more abour ILMerge and download the new version v2.0.0727 here.

ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly. ILMerge takes a set of input assemblies and merges them into one target assembly.ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly. ILMerge takes a set of input assemblies and merges them into one target assembly.

July 25, 2009

مع الدوت نت, everything about .NET in Arabic

Filed under: Just Like a Magic — Tags: — Mohammad Elsheimy @ 1:41 PM

مع الدوت نت is a new Arabic site by the folks of Just Like a Magic devoted for .NET.

مع الدوت نت discusses many .NET topics in Arabic. It discusses new topics besides translating Just Like a Magic ’s hot topics.

Don’t hesitate to visit مع الدوت نت…

http://WithDotNet.WordPress.com

WithDotNet

Older Posts »

Blog at WordPress.com.