Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to dereference a function pointer and read as data under MSVC  ?
  2. void dummy();
  3.  
  4. int _tmain(int argc, _TCHAR* argv[])
  5. {
  6.     int i;
  7.  
  8.     printf("nReading dummy...n");
  9.     for(i = 0; i < 25; i++)
  10.         printf("%.2X ", ((char *)((void *)dummy))[i]);
  11.     puts("");
  12.  
  13.     dummy();
  14.  
  15.     getchar();
  16.     return 0;
  17. }
  18.  
  19. void __declspec(naked) dummy()
  20. {
  21.     __asm
  22.     {
  23.         nop;
  24.         nop;
  25.         nop;
  26.         nop;
  27.  
  28.         ret;
  29.     }
  30. }
  31.        
  32. Reading dummy...
  33. 90 90 90 90 C3 //... rest is random
  34.        
  35. hh
  36.        
  37. printf("%02hhX ", ((char *)((void *)dummy))[i]);
  38.        
  39. X
  40.        
  41. printf("%.2X ", ((char *)((void *)dummy))[i]);
  42.        
  43. printf("%.2X ", ((unsigned char *)dummy)[i]);
  44.        
  45. #include <stdio.h>
  46.  
  47. void PrintHex(const char* input, const int len)
  48. {
  49.      char * tmp=new char[len*3+1];
  50.      for(int i=0;i<len;++i)
  51.           sprintf(tmp+i*3,"%02x ",*(input+i)&0xFF);
  52.  
  53.      printf("%sn",tmp);
  54. };