Guest User

Untitled

a guest
Oct 15th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. /* File:main.cpp      *
  2.  * Credits: w2ch Anon */
  3.  
  4.  
  5. #include "main.h"
  6. #include "resource.h"
  7.  
  8.  
  9. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  10. {
  11.     // Use HeapSetInformation to specify that the process should
  12.     // terminate if the heap manager detects an error in any heap used
  13.     // by the process.
  14.     // The return value is ignored, because we want to continue running in the
  15.     // unlikely event that HeapSetInformation fails.
  16.     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
  17.    
  18.     w2chTools app;
  19.    
  20.     app.LoadPlugins();
  21.     if (app.Initialize())
  22.     {
  23.         app.RunMessageLoop();
  24.     }
  25.    
  26.     app.OnExit();
  27.     app.UnloadPlugins();
  28.     return 0;
  29. }
  30.  
  31.  
  32. PluginElements make_PluginElements(HMODULE &rhMod, PLUGIN_PROCESS_DATA &rProcessData, TCHAR *rFileName)
  33. {
  34.     // take away the .dll at the end of file.
  35.     int i;
  36.     for (i = 0; *(rFileName + i) != '\0'; i++);
  37.     if (i > 4,
  38.         *(rFileName + (i - 1)) == 'l' && *(rFileName + (i - 2)) == 'l' &&
  39.         *(rFileName + (i - 3)) == 'd' && *(rFileName + (i - 4)) == '.')
  40.     {
  41.         i -= 4;
  42.         *(rFileName + i) = '\0';
  43.     }
  44.    
  45.     PluginElements myPluginElements = { rhMod, rProcessData, 0 }; // rFileName[0]
  46.     for (; myPluginElements.szFileName[i] = *(rFileName + i), i; i--);
  47.     return myPluginElements;
  48. }
  49.  
  50.  
  51. // EnigmaApp:======================================================================
  52. w2chTools::w2chTools() :
  53. m_hinstRichedLib(NULL),
  54. m_hwndInputEdit(NULL),
  55. m_hwndOutpuEdit(NULL),
  56. m_hDlg(NULL),
  57. m_ComboBox(NULL)
  58. {
  59. }
  60.  
  61.  
  62. // Creates the application dialog.
  63. bool w2chTools::Initialize()
  64. {
  65.     m_hinstRichedLib = LoadLibrary(TEXT("Riched20.dll"));
  66.     if (m_hinstRichedLib == NULL)
  67.     {
  68.         MessageBox(NULL, TEXT("LoadLibrary(Riched20.dll) error!"), TEXT("ERROR"), MB_OK);
  69.         return 0;
  70.     }
  71.    
  72.     m_hDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc, reinterpret_cast<LPARAM>(this));
  73.    
  74.     if (m_hDlg == NULL)
  75.     {
  76.         MessageBox(NULL, TEXT("CreateDialog error!"), TEXT("ERROR"), MB_OK);
  77.         return 0;
  78.     }
  79.    
  80.     return 1;
  81. }
  82. void w2chTools::RunMessageLoop()
  83. {
  84.     MSG msg;
  85.    
  86.     ShowWindow(m_hDlg, SW_SHOWNORMAL);
  87.     UpdateWindow(m_hDlg);
  88.    
  89.     while (GetMessage(&msg, NULL, 0, 0))
  90.     {
  91.         TranslateMessage(&msg);
  92.         DispatchMessage(&msg);
  93.     }
  94. }
  95. void w2chTools::OnExit()
  96. {
  97.     if (m_hinstRichedLib != NULL) FreeLibrary(m_hinstRichedLib);
  98. }
  99.  
  100.  
  101. void w2chTools::LoadPlugins()
  102. {
  103.     // ============================================================
  104.     TCHAR szDir[MAX_PATH];
  105.     int StartOfDir;
  106.    
  107.     GetModuleFileName(NULL, szDir, MAX_PATH);
  108.    
  109.     for (StartOfDir = 0; szDir[StartOfDir] != '\0'; StartOfDir++);
  110.     for (; szDir[StartOfDir] != '\\'; StartOfDir--);
  111.     szDir[++StartOfDir] = '*';
  112.    
  113.     if (StartOfDir + 1 >= MAX_PATH) // ERROR checking.
  114.     {
  115.         MessageBox(NULL, TEXT("Directory is to long where the exectuable is. Can not load Plugins"), TEXT("ERROR"), MB_OK);
  116.         return;
  117.     }
  118.    
  119.     szDir[StartOfDir + 1] = '\0';
  120.     // ============================================================
  121.     WIN32_FIND_DATA ffd;
  122.     HANDLE hFind = INVALID_HANDLE_VALUE;
  123.     DWORD dwError = ERROR_NO_MORE_FILES;
  124.    
  125.     // Find the first file in the directory.
  126.     hFind = FindFirstFile(szDir, &ffd);
  127.     if (INVALID_HANDLE_VALUE == hFind)
  128.     {
  129.         MessageBox(NULL, TEXT("FindFirstFile error!"), TEXT("ERROR"), MB_OK);
  130.         return;
  131.     }
  132.    
  133.     // List all the files in the directory with some info about them.
  134.     int FileLenght;
  135.     do
  136.     {
  137.         if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  138.         {
  139.             for (FileLenght = 0; // Add the file found to szDir(c string).
  140.                 StartOfDir + FileLenght < (MAX_PATH - 1),
  141.                 szDir[StartOfDir + FileLenght] = ffd.cFileName[FileLenght],
  142.                 ffd.cFileName[FileLenght] != '\0';
  143.             FileLenght++);
  144.             szDir[StartOfDir + FileLenght] = '\0';
  145.            
  146.             HMODULE hMod = ::LoadLibrary(szDir);
  147.             if (hMod != NULL)
  148.             {
  149.                 PLUGIN_PROCESS_DATA pProcessData = (PLUGIN_PROCESS_DATA) ::GetProcAddress(hMod, "ProcessData");
  150.                
  151.                 if (pProcessData != NULL)
  152.                 {
  153.                     // Yes, this DLL exposes the function we need, it is a plugin we can use.
  154.                     // Add it to Plugins vector.
  155.                     m_Plugins.push_back(make_PluginElements(hMod, pProcessData, &(szDir[StartOfDir])));
  156.                 }
  157.             }
  158.         }
  159.     } while (FindNextFile(hFind, &ffd) != 0);
  160.    
  161.     dwError = GetLastError();
  162.     if (dwError != ERROR_NO_MORE_FILES)
  163.     {
  164.         MessageBox(NULL, TEXT("FindFirstFile error!"), TEXT("ERROR"), MB_OK);
  165.     }
  166.    
  167.     // Close the handle to find data in directory.
  168.     FindClose(hFind);
  169. }
  170. void w2chTools::UnloadPlugins()
  171. {
  172.     for (std::vector<PluginElements>::size_type i = m_Plugins.size() - 1;
  173.         i != (std::vector<PluginElements>::size_type) - 1; i--)
  174.     {
  175.         FreeLibrary(m_Plugins[i].hMod);
  176.     }
  177. }
  178.  
  179.  
  180. INT_PTR CALLBACK w2chTools::DialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  181. {
  182.     if (Message == WM_INITDIALOG)
  183.     {
  184.         w2chTools *pw2chTools = reinterpret_cast<w2chTools *>(lParam);
  185.        
  186.         ::SetWindowLongPtrW(hwnd, GWLP_USERDATA, PtrToUlong(pw2chTools));
  187.        
  188.         pw2chTools->m_hwndInputEdit = GetDlgItem(hwnd, IDC_RICHEDIT22);
  189.         pw2chTools->m_hwndOutpuEdit = GetDlgItem(hwnd, IDC_RICHEDIT21);
  190.        
  191.         pw2chTools->m_ComboBox = GetDlgItem(hwnd, IDC_COMBO1);
  192.        
  193.         // Add all the plugin names to combo box list for user to choose what action to do.
  194.         for (std::vector<PluginElements>::size_type i = 0; i < pw2chTools->m_Plugins.size(); ++i)
  195.         {
  196.             SendMessage(pw2chTools->m_ComboBox, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pw2chTools->m_Plugins[i].szFileName));
  197.         }
  198.        
  199.         return TRUE;
  200.     }
  201.     else
  202.     {
  203.         w2chTools *pw2chTools = reinterpret_cast<w2chTools *>(static_cast<LONG_PTR>(
  204.             ::GetWindowLongPtrW(hwnd, GWLP_USERDATA)));
  205.        
  206.         if (pw2chTools)
  207.         {
  208.             switch (Message)
  209.             {
  210.             case WM_COMMAND:
  211.                 switch (LOWORD(wParam))
  212.                 {
  213.                 case IDC_BUTTON1: // Clear button.
  214.                     SetWindowText(pw2chTools->m_hwndOutpuEdit, TEXT(""));
  215.                     break;
  216.                
  217.                 case IDC_BUTTON2: // Ok button.
  218.                 {
  219.                     signed int ItemIndex = SendMessage(reinterpret_cast<HWND>(pw2chTools->m_ComboBox), static_cast<UINT>(CB_GETCURSEL), static_cast<WPARAM>(0), static_cast<LPARAM>(0));
  220.                     if (CB_ERR != ItemIndex) pw2chTools->m_Plugins[ItemIndex].ProcessData(pw2chTools->m_hwndInputEdit, pw2chTools->m_hwndOutpuEdit);
  221.                 }
  222.                     break;
  223.                 }
  224.                 return 0;
  225.             case WM_CLOSE:
  226.                 DestroyWindow(hwnd);
  227.                 return TRUE;
  228.             case WM_DESTROY:
  229.                 PostQuitMessage(0);
  230.                 return TRUE;
  231.             default:
  232.                 break;
  233.             }
  234.         }
  235.     }
  236.    
  237.     return FALSE;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment