Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //VS 2019 Pro / 2013 Ultimate
- //The cake is a lie, patterns are a lie
- #include <iostream>
- #include <Windows.h>
- int main2();
- class SystemClass
- {
- private:
- int var2;
- SystemClass()
- {
- this->var = 0;
- this->var2 = 0;
- }
- public:
- int var;
- void SetVar(int param1)
- {
- this->var = param1;
- }
- void SetVar2(int param1)
- {
- this->var2 = param1;
- }
- void prn()
- {
- std::cout << "this works! " << this->var << std::endl;
- }
- static SystemClass& getInstance()
- {
- static SystemClass theInstance;
- return theInstance;
- }
- void prn2()
- {
- std::cout << "this works2! " << this->var2 << std::endl;
- }
- };
- class Singleton
- {
- private:
- /* Here will be the instance stored. */
- static Singleton* instance;
- int var;
- /* Private constructor to prevent instancing. */
- Singleton();
- public:
- int var2;
- void SetVar(int param1)
- {
- this->var = param1;
- }
- void SetVar2(int param1)
- {
- this->var2 = param1;
- }
- void prn()
- {
- std::cout << "this works! " << this->var << std::endl;
- }
- void prn2()
- {
- std::cout << "this works2! " << this->var2 << std::endl;
- }
- /* Static access method. */
- static Singleton* getInstance();
- };
- /* Null, because instance will be initialized on demand. */
- Singleton* Singleton::instance = 0;
- Singleton* Singleton::getInstance()
- {
- if (instance == 0)
- {
- instance = new Singleton();
- }
- return instance;
- }
- Singleton::Singleton()
- {
- this->var = 0;
- this->var2 = 0;
- }
- int main()
- {
- /*__asm
- {
- int 3
- }*/
- std::cout << "########### SINGLETON BY REFERENCE ###########" << std::endl;
- SystemClass& sys = SystemClass::getInstance();
- //SystemClass sys = SystemClass::getInstance();
- sys.SetVar(333);
- sys.SetVar2(666);
- sys.prn();
- sys.prn2();
- //Por referencia
- __asm
- {
- ; int 3
- mov eax, sys
- ; VISUAL STUDIO 2019
- and dword ptr[eax - 0x04], 0x0
- ; VISUAL STUDIO 2013
- ; and dword ptr[eax + 0x08], 0x0
- ; int 3
- }
- /*__asm
- {
- int 3
- mov eax, SystemClass::getInstance
- int 3
- }*/
- //std::cout << sizeof(SystemClass) << std::endl;
- //ZeroMemory(SystemClass, 4);
- sys = SystemClass::getInstance();
- sys.prn();
- sys.prn2();
- main2();
- std::cin.get();
- return 0;
- }
- int main2()
- {
- std::cout << std::endl;
- std::cout << "########### SINGLETON BY POINTER ###########" << std::endl;
- Singleton* s = Singleton::getInstance(); // Ok
- std::cout << s << std::endl;
- s->SetVar(333);
- s->SetVar2(666);
- s->prn();
- s->prn2();
- __asm
- {
- lea eax, [Singleton::instance]
- and dword ptr[eax], 0x0
- }
- Singleton* r = Singleton::getInstance();
- std::cout << r << std::endl;
- r->prn();
- r->prn2();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement