Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Callback(HWND hwnd, boost::function<void(HWND,LPARAM)> call, LPARAM lParam)
- {
- //Make our stack allocated function object into a heap allocated one
- boost::function<void(HWND,LPARAM)> *callH = new boost::function<void(HWND,LPARAM)>(call);
- //send a message with a pointer to our function object in the WPARAM
- PostMessage(hwnd, WM_CALLBACK, (unsigned)callH, lParam);
- }
- LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- //check for our custom message
- if(msg == WM_CALLBACK)
- {
- //retreive the function pointer from the WPARAM
- boost::function<void(HWND,LPARAM)> *callH = (boost::function<void(HWND,LPARAM)>*)wParam;
- //call it
- (*callH)(hwnd,lParam);
- //delete our heap allocated function object
- delete callH;
- return 0;
- }
- else
- //if there was nothing relevant to us, call the old message procedure
- return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam);
- }
- //std::map<HWND, WNDPROC> hooked;
- Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
- :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
- started(false), exited(false),
- window(_window), size(_size)
- {
- window->AddRef();
- HWND hwnd = *((HWND*)window->GetHandle());
- CallbackHook(hwnd);
- Callback(hwnd, boost::bind(&Graphics::create, this), 0);
- while(!started)Sleep(100);
- }
- void Graphics::create()
- {
- ...code that may throw various exceptions
- started = true;
- }
- class Context
- {
- public:
- Context(HWND hwnd, const boost::function<void(HWND,LPARAM)>& f, LPARAM lParam)
- {
- // TODO: reroute call through Wrapper function.
- }
- void wait()
- {
- mutex::scoped_lock l(m);
- while (!finished)
- {
- c.wait(l);
- }
- if (ex)
- rethrow_exception(ex);
- }
- private:
- void Wrapper()
- {
- try
- {
- f(/*params*/);
- }
- catch (...)
- {
- ex = current_exception();
- }
- mutex::scoped_lock l(m);
- finished = true;
- c.notify_all();
- }
- boost::function<void(HWND,LPARAM)> f;
- exception_ptr ex;
- bool finished;
- mutex m;
- condition c;
- };
- void Callback(HWND hwnd, const boost::function<void(HWND,LPARAM)>& f, LPARAM lParam)
- {
- Context ctx(hwnd, f, lParam);
- ctx.wait();
- }
- void Callback(HWND hwnd, boost::function<void(HWND,LPARAM)> call, LPARAM lParam)
- {
- //send a message with a pointer to our function object in the WPARAM
- if (SendMessage(hwnd, WM_CALLBACK, &call, lParam))
- {
- // error handling.
- }
- }
- LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- //check for our custom message
- if(msg == WM_CALLBACK)
- {
- //retreive the function pointer from the WPARAM
- boost::function<void(HWND,LPARAM)> *callH = (boost::function<void(HWND,LPARAM)>*)wParam;
- //call it, catching exceptions in the process.
- try
- {
- (*callH)(hwnd,lParam);
- return 0;
- }
- catch (...)
- {
- return 1;
- }
- }
- else
- //if there was nothing relevant to us, call the old message procedure
- return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam);
- }
- //std::map<HWND, WNDPROC> hooked;
- Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
- :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
- started(false), exited(false),
- window(_window), size(_size)
- {
- window->AddRef();
- HWND hwnd = *((HWND*)window->GetHandle());
- CallbackHook(hwnd);
- Callback(hwnd, boost::bind(&Graphics::create, this), 0);
- // No need to wait, as SendMessage is synchronous.
- }
- void Graphics::create()
- {
- ...code that may throw various exceptions
- started = true;
- }
- Graphics::Graphics(IWindow *_window, Size2<unsigned> _size)
- :lost(false), reset(false), refCnt(0), backCol(0xFF000000),
- started(false), exited(false),
- window(_window), size(_size)
- {
- window->AddRef();
- HWND hwnd = *((HWND*)window->GetHandle());
- semaphore = CreateSemaphore(NULL, 0, 1, NULL);
- if (semaphore == NULL) throw whatever;
- CallbackHook(hwnd);
- Callback(hwnd, boost::bind(&Graphics::create, this), 0);
- WaitForSingleObject(semaphore);
- create();
- CloseHandle(semaphore); /* you won't need it */
- }
- void Graphics::create()
- {
- ...code that may throw various exceptions
- }
- void Graphics::create_callback()
- {
- ReleaseSemaphore(semaphore, 1, NULL);
- }
Add Comment
Please, Sign In to add comment