Advertisement
Guest User

hooking dll so far3

a guest
Dec 13th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // dllmain.h : Defines the entry point for the DLL application.
  2. #pragma once
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <Windows.h>
  7. using namespace std;
  8.  
  9.  HINSTANCE currentProcessHandle;
  10.  HOOKPROC hkprcSysMsg;
  11.  HHOOK hookID;
  12.  
  13. BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
  14. {
  15.     //std::ofstream outfile("test.txt");
  16.  
  17.  
  18.     switch (ul_reason_for_call)
  19.     {
  20.     case DLL_PROCESS_ATTACH:
  21.         currentProcessHandle = hModule;
  22.         break;
  23.     case DLL_THREAD_ATTACH:
  24.     case DLL_THREAD_DETACH:
  25.     case DLL_PROCESS_DETACH:
  26.         break;
  27.     }
  28.     return TRUE;
  29. }
  30.  
  31. LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
  32. {
  33.     std::ofstream outfile("test.txt");
  34.     if (nCode >= 0)
  35.     {
  36.         switch (nCode)
  37.         {
  38.         case HCBT_CREATEWND:
  39.             outfile << L"Created!~";
  40.             cout << "Created!~" << endl;
  41.             break;
  42.         case HCBT_DESTROYWND:
  43.             outfile << L"Destroied!~";
  44.             cout << "Destroied!~" << endl;
  45.             break;
  46.         default:
  47.             cout << "sth else" << endl;
  48.             break;
  49.         }
  50.     }
  51.     else
  52.     {
  53.         return CallNextHookEx(hookID, nCode, wparam, lparam);
  54.     }
  55.     outfile.close();
  56. }
  57.  
  58. __declspec(dllimport) void InstallHook();
  59. __declspec(dllimport) void UnistallHook();
  60.  
  61.  
  62. ################################################################################################
  63. //dllmain.cpp
  64. #pragma once
  65. #include "dllmain.h"
  66. void InstallHook()
  67. {
  68.     hookID = SetWindowsHookEx(WH_CBT, HookProcedure, currentProcessHandle, 0);
  69. }
  70.  
  71. void UnistallHook()
  72. {
  73.     UnhookWindowsHookEx(hookID);
  74. }
  75.  
  76. ###########################################################################
  77. // Hook Executer.cpp : Defines the entry point for the console application.
  78. //
  79.  
  80. #include "stdafx.h"
  81. #include "..\Dll\dllmain.cpp"
  82. #include <iostream>
  83. using namespace std;
  84.  
  85. int _tmain(int argc, _TCHAR* argv[])
  86. {
  87.     int num = -1;
  88.     cout << "1.Install Hook"<<endl
  89.         << "2.Unistall Hook"<<endl
  90.         << "0.Exit";
  91.     do{
  92.         cin >> num;
  93.         if (num ==1)
  94.         {
  95.             InstallHook();
  96.  
  97.         }
  98.         else
  99.         {
  100.             UnistallHook();
  101.         }
  102.         getchar();
  103.         system("cls");
  104.         cout << "1.Install Hook" << endl
  105.             << "2.Unistall Hook" << endl
  106.             << "0.Exit";
  107.     } while (num != 0 && num < 3);
  108.  
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement