Thursday, July 5, 2012

Playing with the C stack


Parameters on the stack appear in memory with the leftmost item at the lowest memory location. Thus you can get a pointer to your functions stack frame, and walk the parameter list, or even cast your stackframe to a struct that can be passed to other functions.

The first sample demonstrates how to retrieve the pointer to any variable parameters passed to the function.

int __cdecl printf(char const* pszFmt,...)
 { 
    char buf[1024];
    return wvsprintf(buf,pszFmt,&pszFmt+1);
 }

This sample demonstrates how the functions passed to a function can be passed to another function.

BOOL InvalidateRect( 
 HWND hwnd, 
 LONG left, 
 LONG top, 
 LONG right, 
 LONG bottom, 
 BOOL fErase)
 {
   return InvalidateRect(hwnd,(LPRECT)&left,fErase); 
 }


No comments:

Post a Comment