Sunday, August 26, 2007

Type casting using Assembly code win32


We know that C++ will not allows assiging variable if they are on different typee. In That situations type casting is needed..But In some cases we can't use the c++ inbuilt type casting system as well as C style casting also..

 I am taking an exmaple of CreateThread thread proc function.(Beacuse many people uses static functions as the
thread proc for CreateThread.)

 For example if you look the function CreateThread API,the thread proc type is
DWORD WINAPI ThreadProc(void *lParam)
{

}

So this insist you to create some global functions or static functions.

Hence you can't pass your class member functions to CreateThread. So usually people will create some static functions and pass that function to CreateThread.And from inside that static function the appropriate class function is called..

This is fine. But using a littile asm code this can be avoided.
For example CCoffe is your class, and the MakeCoffe function we want to pass to CreateThread.  This can be done as below.

//sample codes
class CCoffe
{
  DWORD MakeCoffe(void *lParam)
  {
     return 0;
  }
}

//Initialization  function.
CCoffe::Init()
{  
   //temp is a function pointer of ThreadProc Type.
   DWORD (WINAPI*temp)(void*lp);
   __asm //Here we can pass any  address  to temp.      
   {


       mov EAX,CCoffe::MakeCoffe
       mov temp,EAX
   }
   //Call CreateThread with temp
   DWORD  out;
   CreateThread(0,0,temp,0,0,&out);
}

The Point is we can pass any address to temp,using asm block.Passing wrong function addresses 
may cause invalid stack structure,Might result in a crash.Hopes next time you will avoid static functions..

Note: I Don't know is this a good practice or not, If you have any opinion please let me know..


Friday, August 24, 2007

NULL pointer and Some thoughts C++


Its me again , now came with a NULL pointer in hand... You may think what so special about NULL pointer , right ? .

it is very special . for example even if multiply two NULL pointers ,you will get a the result as a NULL pointer.. is it interesting ? .. :) I was just joking.. 

I wrote this beacause mostly people checks in destructor something like this

if(p) delete p;

 This is not needed , you can just avoid that checking, because delete NULL , will not cause any probles.
so if you have more pointers in your class to delete , avoid that checking is too good to see.

I think Now it is good to mention about this function  IsBadWritePtr . This function can be used to check
whether one address is good to write. In MFC there is function AfxIsValidAddress which will call this function to check.

The next thing about NULL pointer is that , You can still call member functions with null pointer, Like

CCLemon *p = NULL;

p->Drink() ;

Still this will works . but only one condition , Drink should not access any member variables of class CCLemon , and Drink shouldn't be a virtual function. If Drink is a virtual function result is Bhooommm!!!





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..