//main.cpp with WINAPI using the WINAPI loop A a1; a1.Initialize();
//A void Initialize() { Alpha alpha1; alpha1.Create(&SHandler); } static void SHandler(string info) { A a1; a1.Handler(info) } void Handler(string info) {printf(info);}
//Alpha void (*_AddressAt)(string info); void Create(void (*AddressAt)(string info)) { _AddressAt = AddressAt; Timer Timer1; Timer1.AddHandler(&Shandler); Timer1.Interval = 1000; Timer1.Start(); } static void SHandler() { Alpha alpha1; alpha1.Handler(); } void Handler() { _AddressAt("CallBack!"); //LINE THAT CRASHES APP }
//Timer.h #pragma once class Timer { public: DWORD Interval = 1000; bool IsRunning(); void AddHandler(void (*AddressOf)(void)); void Start(); void Stop(); private: void (*_AddressOf)(void); void Looper(); bool _IsRunning = false; }; //Timer.cpp #include <windows.h> #include <thread> #include "Timer.h" using namespace std; void Timer::AddHandler(void (*AddressOf)(void)) { _AddressOf = AddressOf; } void Timer::Start() { _IsRunning = true; std::thread first(Looper, this); first.join(); } void Timer::Stop() { _IsRunning = false; } void Timer::Looper() { while (_IsRunning) { _AddressOf(); // make callback Sleep(Interval); } } bool Timer::IsRunning() { return _IsRunning; }
alpha1
a1.Initialize
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)