Advertisement
bueddl

dtor hook

Jun 6th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. //
  2. // Hooking & wrapping of virtual class methods in C++ (gcc/clang/intel ABI only)
  3. //
  4. // (C) 2016 by Bueddl <sebastian.buettner@powerserverplus.net>
  5. //
  6.  
  7. #include "hook.hpp"
  8. #include <iostream>
  9.  
  10. class SomeClass
  11. {
  12. public:
  13.     virtual ~SomeClass() = default;
  14.  
  15.     virtual int test_fn(int param)
  16.     {
  17.         std::cout << "Original SomeClass::test_fn()" << std::endl
  18.                             << "  this = " << this << std::endl
  19.                             << "  param = " << param << std::endl
  20.                             << "  -> " << (param * 7) << std::endl;
  21.  
  22.         return param * 7;
  23.     }
  24. };
  25.  
  26. void some_class_dtor(SomeClass const *_this)
  27. {
  28.     // Get some stuff done before
  29.     std::cout << "Pre call to SomeClass::~SomeClass()" << std::endl
  30.                         << "  this = " << _this << std::endl;
  31.  
  32.     // Get and call orig
  33.     Hook<SomeClass>::call_once<1>(_this);
  34.  
  35.     // Get some stuff done after
  36.     std::cout << "Post call to SomeClass::~SomeClass()" << std::endl;
  37. }
  38.  
  39. int main()
  40. {
  41.     // Create object instance
  42.     SomeClass *some_object = new SomeClass;
  43.  
  44.     // Hook
  45.     Hook<SomeClass>::install<1>(some_object, some_class_dtor);
  46.  
  47.     // Delete object instance
  48.     delete some_object;
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement