Thursday, October 22, 2009

C++ - Referring to an enum inside a type

When you refer to an enum inside a type, you do not have to specify the name of the enum. See the code snippets below.

Say you have a class like what is given below.

class MyClass {
enum MyEnum { a };
};


Incorrect use of a value of type MyEnum



int i = MyClass::MyEnum::a;


Correct use of a value of type MyEnum



int j = MyClass::a;

Tuesday, October 13, 2009

Why AFX_MANAGE_STATE is important in Windows programming?

I happened to write a Windows shell extension about an year back. It was this macro that helped me fix issues when my application was crashing here and there. I am developing an MS Excel extension for a client project and again I ran into the same problem. I spent a couple of days figuring out what was going wrong and finally found that this same old macro was going to fix my issue.

So what is this macro and what does it do?

MFC uses the resource handle of the main application to load the resource templates. If you have an exported function in a DLL, such as one that launches a dialog box in the DLL, this template is actually stored in the DLL module. You need to switch the module state for the correct handle to be used. Otherwise the program crashes as it tries to access some resource resides in a different module. You can do this module switching by adding the following code to the beginning of the function. That is just before using the resource from the other module.


AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope. When the execution flow goes out of the current scope, the module state gets switched back to that of the current application.

Thursday, May 7, 2009

Exporting a Global Variable from a DLL

Lets say you want to export a global variable of type int and use it in another application. You need to do it as follows.

1. In the header file where you are going to declare your variable do this.
   extern DLL_EXTERN int i;

2. In the .c or .cpp file, you need to define it like this.
   DLL_EXTERN int i = 10;

You should make your pre-processor settings such that DLL_EXTERN is equal to __declspec(dllexport).

3. Now in your application, where you are going to consume this variable, you need to set your pre-processor options such that DLL_EXTERN is equal to __declspec(dllimport).

NOTE:
For functions, not having __declspec(dllimport) does not give any compile/link errors. But of course it helps generate better code as with this the compiler knows that the function resides in a DLL and hence it can avoid the level of indirection that is there when a function call is done across the boundary of a DLL. But its a MUST that you have __declspec(dllimport) in front of variables as without that you will get the famous "unresolved external symbol" errors.

Thursday, April 23, 2009

%cd% vs. %~dp0

Until today, I also did not have an idea about the difference between these two.

%cd% available to a batch file or at the command prompt and expands to the drive letter and the path of the current directory.

%~dp0 is available to a batch file only. This expands to the drive letter and the path in which the batch file is located.

If you take the following command for an example where the batch file prints the two variables, the output would be as shown.

C:\danushka\runtime>D:\my_scripts\test_script.bat

%cd% = C:\danushka\runtime

%~dp0 = D:\my_scripts

Tuesday, December 23, 2008

Nanocar Wins Top Science Award

James Tour, a professor of chemistry at Rice University together with a team of postgraduate and postdoctoral researchers has built a nanocar with chassis, working suspension, wheels and a motor. Believe it or not, the width of this whole assembly is just 4 nanometers. To power up the car, you shine light on it and the motor spins in one direction and pushes the car like a peddle wheel on the surface. Prof. Tour won the Foresight Institute Feynman Prize for experimental nanotechnology. The following set of images shows how this incredible car really works.


                              The nanocar


              How light moves nanocar's engine


                        Suspension system

The most important question at this point is, what is the use of this?. Specially when it comes to real world applications, can it really help?.

Until now, the engineers have opted for top-down approaches when constructing most of the things. For example, trees are cut down to make tables, and as such, large silicon wafers are cut away to make transistors. But in the future, things will be built not from the top-down, but the bottom-up.

Prof. Tour points to hemoglobin as an example. Each heme group -- containing one iron atom -- carries only one molecule of oxygen, but billions of them go back and forth carrying oxygen from our lungs to the cells crying out for it. And on the way back out of the cells, the hemes detoxify by carrying out CO2. In the same way, nanovehicles could carry atoms to construct objects.

Friday, December 19, 2008

Setting up wxWidgets to work with Code::Blocks on Windows

You have to compile and install wxWidgets as explained here to get it to work with Code::Blocks on Windows.

Thursday, December 18, 2008

The Beauty of wxWidgets

I am playing around with the wxWidgets GUI toolkit these days.



The beauty of this cool toolkit is that you can write once and compile/run everywhere. This uses the native OS widgets and hence the same application runs on different platforms will have the native look-and-feel.


Wow!!!