c# - Global CBT Hook in Windows 7 fails eventually -


i using global cbt hook setup stop new windows stealing focus. injecting c++ dll c# app. functions fine while , stops working. thought might timing out, changed lowlevelhookstimeout in registry , rebooted. didn't help.

i not great c++. so, perhaps error in code. causing fail? also, there way deal name mangling , clean code bit?

windowmonitor.cpp

   #include <map>    #include "stdafx.h"    #include "header.h"    using namespace std;     #pragma data_seg(".test")    hhook hook=0;    #pragma data_seg()    #pragma comment(linker, "/section:.test,rws")     extern "c"     {    __declspec(dllexport) lresult callback cbtproc(int ncode, wparam wparam, lparam lparam)         {     if (ncode<=0)  return callnexthookex(hook, ncode, wparam, lparam);     if (ncode==hcbt_activate)     {         try {             cbtactivatestruct* stuff=(cbtactivatestruct*) lparam;              if (stuff!=0)             {             if (stuff->fmouse==true)             {                  return 0;             }             if (stuff->fmouse==false)             {                 return 1;             }             }         }         catch (exception& e)         {          }      }     return callnexthookex(hook, ncode, wparam, lparam); } }   bool apientry dllmain( hmodule hmodule,                   dword  ul_reason_for_call,                   lpvoid lpreserved                   ) { switch (ul_reason_for_call) { case dll_process_attach: case dll_thread_attach: case dll_thread_detach: case dll_process_detach:      break; } return true; }  bool injectcbthook() {  hmodule hdll; hookproc cbtprocaddr; dword proc_id;   hdll        = loadlibrarya("windowmonitor.dll"); cbtprocaddr = (hookproc)getprocaddress(hdll, "_cbtproc@12");   hook=setwindowshookex(wh_cbt, cbtprocaddr, hdll, 0);  if (hook==0)  {     int x=getlasterror();     return false; }  return true; }  bool ejectcbthooks() {  unhookwindowshookex(hook);  return true;  } 

windowmonitor.h

#ifndef wm #define wm extern "c" {  #define dllexport  __declspec(dllexport)   dllexport bool injectcbthook() ;   dllexport bool ejectcbthooks(); }  #endif  

c# p/invoke

[dllimport("windowmonitor.dll", callingconvention = callingconvention.cdecl)] static extern bool injectcbthook(); [dllimport("windowmonitor.dll", callingconvention = callingconvention.cdecl)] static extern bool ejectcbthooks(); 


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -