Source file: /~heha/mb-iwp/CBT-Hooks/Kamerafehler.zip/src/Kamerafehler.cpp

// Quelltext für Visual C++ 6.0
// Funktion: Programm DSlrRemote.exe austricksen (Falschbilder anzeigen)
#include <windows.h>
#include <windowsx.h>
#include <shlwapi.h>
#define elemof(x) (sizeof(x)/sizeof(*(x)))
#define T(x) TEXT(x)

#pragma bss_seg(".shared")
HHOOK g_hHook;		// should be initially 0

#pragma bss_seg()
HHOOK g_HookProcess;	// the DLL that eventually holds the hook

HWND g_hStatic;		// window handle of interest
WNDPROC g_fOrigProc;
TCHAR ImageFilePath[MAX_PATH], *ImageFileName;
HBITMAP g_bm;

HHOOK g_KbdHook;
int LadeBildNr;

void _cdecl debug(const char *fmt, ...) {
 char buf[256];
 wvsprintf(buf,fmt,(va_list)(&fmt+1));
 OutputDebugStringA(buf);
}

static HBITMAP LadeFalscheDatei(int cx, int cy) {
 lstrcpy(ImageFileName,"*.bmp");
 WIN32_FIND_DATA fd;
 HANDLE h;
 h=FindFirstFile(ImageFilePath,&fd);
 if (h) {
  for (int i=1; i<LadeBildNr; i++) {
   if (!FindNextFile(h,&fd)) break;
  }
  if (fd.cFileName[0]) {
   lstrcpy(ImageFileName,fd.cFileName);
   debug("Lade %s",ImageFilePath);
   HBITMAP bm=(HBITMAP)LoadImage(0,ImageFilePath,IMAGE_BITMAP,cx,cy,LR_LOADFROMFILE);
   return bm;
  }
  FindClose(h);
 }
 return 0;
}

LRESULT CALLBACK StaticSubclassProc(HWND Wnd, UINT Msg,WPARAM wParam, LPARAM lParam) {
 switch (Msg) {
  case STM_SETIMAGE: {
   if (lParam && LadeBildNr) {
    HBITMAP bm=LadeFalscheDatei(0,0);
    if (bm) {
     if (g_bm) DeleteBitmap(g_bm);
     g_bm=bm;
     lParam=(LPARAM)bm;
    }
   }
  }break;
 }
 return CallWindowProc(g_fOrigProc,Wnd,Msg,wParam,lParam);
}

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
 if (code==HC_ACTION && (long)lParam>=0) {
  debug("Keykode %X %X",wParam, lParam);
  if (VK_NUMPAD0<=wParam && wParam<=VK_NUMPAD9) {
   LadeBildNr=wParam-VK_NUMPAD0;
   debug("Wähle Falschbild Nr. %d",LadeBildNr);
   return 1;
  }
 }
 return CallNextHookEx(g_KbdHook,code,wParam,lParam);
}

LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam) {
 switch (code) {
  case HCBT_CREATEWND: if (!g_hStatic) {
   CBT_CREATEWND *cw=(CBT_CREATEWND*)lParam;
   TCHAR buf[MAX_PATH];
   LPCTSTR p;
   GetModuleFileName(0,buf,elemof(buf));
   p=PathFindFileName(buf);
   if (!lstrcmpi(p,T("DSlrRemote.exe"))
   && (int)cw->lpcs->lpszClass==49177
   && cw->lpcs->style==0x5000000E
   && cw->lpcs->dwExStyle==4
   && (int)cw->lpcs->hMenu==0x03EE) {
    g_hStatic=(HWND)wParam;
    debug("created %X(xywh=%d %d %d %d), subclass!",g_hStatic,
      cw->lpcs->x,cw->lpcs->y,cw->lpcs->cx,cw->lpcs->cy);
    g_fOrigProc=SubclassWindow(g_hStatic,StaticSubclassProc);
    g_KbdHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,0,GetCurrentThreadId());
   }
  }break;
  case HCBT_DESTROYWND: {
   if (g_hStatic==(HWND)wParam) {
    debug("destroyed");
    g_hStatic=0;
    UnhookWindowsHookEx(g_KbdHook);
   }
  }break;
 }
 return CallNextHookEx(g_hHook,code,wParam,lParam);
}

extern "C" BOOL WINAPI _DllMainCRTStartup(
 HINSTANCE hinstDLL,	// handle to DLL module
 DWORD fdwReason,	// reason for calling function
 LPVOID) {
 switch (fdwReason) {
  case DLL_PROCESS_ATTACH: {
   GetModuleFileName(hinstDLL,ImageFilePath,elemof(ImageFilePath));
   ImageFileName=PathFindFileName(ImageFilePath);
   DisableThreadLibraryCalls(hinstDLL);
   if (!g_hHook) {	// query the shared variable
    debug("hook");
    g_HookProcess=g_hHook=SetWindowsHookEx(WH_CBT,CBTProc,hinstDLL,0);
   }
  }break;
  case DLL_PROCESS_DETACH: {
   if (g_HookProcess) {
    debug("unhook");
    UnhookWindowsHookEx(g_HookProcess);
   }
  }
 }
 return TRUE;
}
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded