Monday, August 20, 2007

Quick way to disable global keys (like ALT + TAB )


Some times it needed to disable the functonality of the global keys like ALT + TAB ,CTRL + ESC . For example when playing some games , pressing ALT + TAB will not cause to switch the tasks. Personaly i don't like this ,but programmers do these ,otherwise they might needed to handle something more..


So if you want to do these , One way is to use Hooks , using SetWindowHookEx api. But there is much simpler way to do this , using hot keys. There is function named RegisterHotKey , this will define a system wide hot key. So the effect is same as Hook.


After defining a hotkey ,when the hotkey is pressed by the user WM_HOTKEY message will be posted to message queue of that window .


So if you want to disable ALT+TAB , install a hotkey using
                                 RegisterHotKey(hwnd,id,MOD_ALT,VK_TAB).


  Done!!!! . Very simple than hooks.you can also handle the WM_HOTKEY message to do your own tasks..


Thursday, August 9, 2007

operator= & Inheritance c++


One interesting thing about operator = is that it can't be inherited.. So even if your base class has operator=  , no use for derived class. For example CString has operator= , but if you derive a class from CString you will not get = operator functionality in your derived class.

But there is a neat trick that will explicitly put this operator= in to the derived class. Here it is

class CString
{
public :
  CString& operator=(char *)
  {
     // some codes
     return *this;
  }
};
class CMyString : public CString
{
public:
  //We explicitly inserted the operator.
   using CString::operator=; 
};

Now CMyString gets the operator from CString.

Wednesday, August 8, 2007

Checking the presence of Virtual table in a class c++

The Last day when i am debugging one program , i saw the __vfptr symbol in VC++ . This represents the the vtable of a class. When you debuging your program try obj1.__vfptr in watch window. It will show the virtual functions of that class.

On that time I thought about writing such a function to check the vtable presence in a class , it is not a bad Idea..

So This post is about checking the whether a class contains virtual function or not. I want to write a function like IsVTablePresent(MYclass) , the function will return true if there is a vtable ,Otherwise false.

I don't know is this type of functins are really useful or not , but i think these are really smart Smile.

 Here is my solution using templates , It is very easy.

template <typename T>
class tDervVir : public T
{
 virtual void VirtualFun(){} //dummy
};


template <typename T>
class tDervNormal : public T
{
 // No virtual functions inside.
};


template <typename T>
class VFinder                  
{
 public :
 int IsVirtualContains() //this is our function
 {


     tDervNormal<T> obj1;
     tDervVir<T>    obj2;
     return sizeof(obj1) == sizeof(obj2);
 }


};

By using the above classes it can be done. For example consider a class CSample as the class for testing.


class CSample
{
   virtual void PoorMan(){}
};



We can check CSample as the following way

VFinder<CSample> tester;
(tester.IsVirtualContains() ? cout << "Virtual present" : cout << "Virtual not present" ;


Thats all for now..