Advertisement
Guest User

MinHook test

a guest
May 9th, 2013
319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <windows.h>
  2. #include "MinHook.h"
  3.  
  4. #if defined _M_X64
  5. #pragma comment(lib, "libMinHook.x64.lib")
  6. #elif defined _M_IX86
  7. #pragma comment(lib, "libMinHook.x86.lib")
  8. #endif
  9.  
  10. #pragma comment(lib, "winmm.lib") // for timeGetTime
  11.  
  12. // Random 100 functions
  13. void *pUser32Functions[] = {
  14.     ShowWindow,
  15.     SendMessage,
  16.     AnimateWindow,
  17.     GetClipboardData,
  18.     SetCursorPos,
  19.     GetForegroundWindow,
  20.     MessageBox,
  21.     BringWindowToTop,
  22.     DispatchMessage,
  23.     MessageBeep,
  24.     IsWindow,
  25.     CloseWindow,
  26.     mouse_event,
  27.     PrintWindow,
  28.     GetDC,
  29.     WaitMessage,
  30.     ShowCursor,
  31.     IsIconic,
  32.     CharUpper,
  33.     ReleaseCapture,
  34.     RemoveMenu,
  35.     MoveWindow,
  36.     GetMessage,
  37.     PostMessage,
  38.     GetWindowThreadProcessId,
  39.     SetWindowPos,
  40.     DestroyWindow,
  41.     OpenDesktop,
  42.     SetActiveWindow,
  43.     GetSystemMenu,
  44.     SetTimer,
  45.     ChangeDisplaySettings,
  46.     ReleaseDC,
  47.     FindWindow,
  48.     FlashWindow,
  49.     SetParent,
  50.     PeekMessage,
  51.     GetWindow,
  52.     SetWindowText,
  53.     GetCursor,
  54.     OpenClipboard,
  55.     GetClassName,
  56.     SetFocus,
  57.     EnumWindows,
  58.     EnumDisplaySettings,
  59.     BeginPaint,
  60.     GetAncestor,
  61.     CallWindowProc,
  62.     GetWindowInfo,
  63.     GetCursorInfo,
  64.     LoadString,
  65.     GetParent,
  66.     RedrawWindow,
  67.     GetSystemMetrics,
  68.     RegisterClassEx,
  69.     DefWindowProc,
  70.     ClientToScreen,
  71.     GetProp,
  72.     SwitchDesktop,
  73.     SetForegroundWindow,
  74.     BlockInput,
  75.     ShowScrollBar,
  76.     keybd_event,
  77.     GetMonitorInfo,
  78.     CloseDesktop,
  79.     CallNextHookEx,
  80.     SendMessageTimeout,
  81.     ExitWindowsEx,
  82.     OpenInputDesktop,
  83.     GetCursorPos,
  84.     CharNext,
  85.     KillTimer,
  86.     InsertMenu,
  87.     HideCaret,
  88.     ScreenToClient,
  89.     GetDlgItemText,
  90.     LockWindowUpdate,
  91.     GetClassInfo,
  92.     GetWindowPlacement,
  93.     UpdateWindow,
  94.     GetLastInputInfo,
  95.     GetMenuItemCount,
  96.     FillRect,
  97.     RegisterClass,
  98.     GetClientRect,
  99.     CreateDesktop,
  100.     GetSubMenu,
  101.     SetClipboardData,
  102.     RegisterDeviceNotification,
  103.     CharToOem,
  104.     RegisterHotKey,
  105.     GetWindowText,
  106.     LoadImage,
  107.     GetMenuString,
  108.     RegisterWindowMessage,
  109.     EnableMenuItem,
  110.     TileWindows,
  111.     GetScrollPos,
  112.     GetFocus,
  113.     TrackPopupMenu,
  114.     NULL
  115. };
  116.  
  117. // Foo detour function
  118. void WINAPI DetourFunc()
  119. {
  120.     __debugbreak();
  121. }
  122.  
  123. // Foo thread
  124. DWORD WINAPI ThreadProc(LPVOID lpParameter)
  125. {
  126.     Sleep(INFINITE);
  127.     return 0;
  128. }
  129.  
  130. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  131. {
  132.     int i;
  133.     void *pFoo;
  134.     DWORD dwTickCount;
  135.     DWORD dwt1, dwt2;
  136.     DWORD dwt3, dwt4;
  137.     char szMessage[256];
  138.  
  139.     // Initialize MinHook
  140.     if (MH_Initialize() != MH_OK)
  141.     {
  142.         return 1;
  143.     }
  144.  
  145.     // Create hooks
  146.     for(i=0; pUser32Functions[i] != NULL; i++)
  147.     {
  148.         if (MH_CreateHook(pUser32Functions[i], &DetourFunc,
  149.             (void**)(&pFoo)) != MH_OK)
  150.         {
  151.             return 1;
  152.         }
  153.     }
  154.  
  155.     //////////////////////////////////////////////////////////////////////////
  156.     // Test #1
  157.  
  158.     // Enable all using MH_EnableHook
  159.     dwTickCount = timeGetTime();
  160.  
  161.     for(i=0; pUser32Functions[i] != NULL; i++)
  162.     {
  163.         if (MH_EnableHook(pUser32Functions[i]) != MH_OK)
  164.         {
  165.             return 1;
  166.         }
  167.     }
  168.  
  169.     dwt1 = timeGetTime() - dwTickCount;
  170.  
  171.     // Disable all
  172.     MH_DisableAllHooks();
  173.  
  174.     // Enable all using MH_EnableAllHooks
  175.     dwTickCount = timeGetTime();
  176.  
  177.     MH_EnableAllHooks();
  178.  
  179.     dwt2 = timeGetTime() - dwTickCount;
  180.  
  181.     // Disable all
  182.     MH_DisableAllHooks();
  183.  
  184.     //////////////////////////////////////////////////////////////////////////
  185.     // Test #2
  186.  
  187.     // Create 100 threads
  188.     for(i=0; i<100; i++)
  189.         CloseHandle(CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL));
  190.  
  191.     // Enable all using MH_EnableHook
  192.     dwTickCount = timeGetTime();
  193.  
  194.     for(i=0; pUser32Functions[i] != NULL; i++)
  195.     {
  196.         if (MH_EnableHook(pUser32Functions[i]) != MH_OK)
  197.         {
  198.             return 1;
  199.         }
  200.     }
  201.  
  202.     dwt3 = timeGetTime() - dwTickCount;
  203.  
  204.     // Disable all
  205.     MH_DisableAllHooks();
  206.  
  207.     // Enable all using MH_EnableAllHooks
  208.     dwTickCount = timeGetTime();
  209.  
  210.     MH_EnableAllHooks();
  211.  
  212.     dwt4 = timeGetTime() - dwTickCount;
  213.  
  214.     // Disable all
  215.     MH_DisableAllHooks();
  216.  
  217.     //////////////////////////////////////////////////////////////////////////
  218.     // Done
  219.  
  220.     // Uninitialize MinHook
  221.     if (MH_Uninitialize() != MH_OK)
  222.     {
  223.         return 1;
  224.     }
  225.  
  226.     // And the winner is...
  227.     wsprintf(szMessage,
  228.         "No threads:\n"
  229.         "MH_EnableHook: %u ms\n"
  230.         "MH_EnableAllHooks: %u ms\n"
  231.         "\n"
  232.         "100 threads:\n"
  233.         "MH_EnableHook: %u ms\n"
  234.         "MH_EnableAllHooks: %u ms",
  235.         dwt1, dwt2, dwt3, dwt4);
  236.  
  237.     MessageBox(NULL, szMessage, "Timing", MB_OK);
  238.  
  239.     ExitProcess(0);
  240. }
Advertisement
RAW Paste Data Copied
Advertisement