Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* File:main.cpp *
- * Credits: w2ch Anon */
- #include "main.h"
- #include "resource.h"
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // Use HeapSetInformation to specify that the process should
- // terminate if the heap manager detects an error in any heap used
- // by the process.
- // The return value is ignored, because we want to continue running in the
- // unlikely event that HeapSetInformation fails.
- HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
- w2chTools app;
- app.LoadPlugins();
- if (app.Initialize())
- {
- app.RunMessageLoop();
- }
- app.OnExit();
- app.UnloadPlugins();
- return 0;
- }
- PluginElements make_PluginElements(HMODULE &rhMod, PLUGIN_PROCESS_DATA &rProcessData, TCHAR *rFileName)
- {
- // take away the .dll at the end of file.
- int i;
- for (i = 0; *(rFileName + i) != '\0'; i++);
- if (i > 4,
- *(rFileName + (i - 1)) == 'l' && *(rFileName + (i - 2)) == 'l' &&
- *(rFileName + (i - 3)) == 'd' && *(rFileName + (i - 4)) == '.')
- {
- i -= 4;
- *(rFileName + i) = '\0';
- }
- PluginElements myPluginElements = { rhMod, rProcessData, 0 }; // rFileName[0]
- for (; myPluginElements.szFileName[i] = *(rFileName + i), i; i--);
- return myPluginElements;
- }
- // EnigmaApp:======================================================================
- w2chTools::w2chTools() :
- m_hinstRichedLib(NULL),
- m_hwndInputEdit(NULL),
- m_hwndOutpuEdit(NULL),
- m_hDlg(NULL),
- m_ComboBox(NULL)
- {
- }
- // Creates the application dialog.
- bool w2chTools::Initialize()
- {
- m_hinstRichedLib = LoadLibrary(TEXT("Riched20.dll"));
- if (m_hinstRichedLib == NULL)
- {
- MessageBox(NULL, TEXT("LoadLibrary(Riched20.dll) error!"), TEXT("ERROR"), MB_OK);
- return 0;
- }
- m_hDlg = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc, reinterpret_cast<LPARAM>(this));
- if (m_hDlg == NULL)
- {
- MessageBox(NULL, TEXT("CreateDialog error!"), TEXT("ERROR"), MB_OK);
- return 0;
- }
- return 1;
- }
- void w2chTools::RunMessageLoop()
- {
- MSG msg;
- ShowWindow(m_hDlg, SW_SHOWNORMAL);
- UpdateWindow(m_hDlg);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- void w2chTools::OnExit()
- {
- if (m_hinstRichedLib != NULL) FreeLibrary(m_hinstRichedLib);
- }
- void w2chTools::LoadPlugins()
- {
- // ============================================================
- TCHAR szDir[MAX_PATH];
- int StartOfDir;
- GetModuleFileName(NULL, szDir, MAX_PATH);
- for (StartOfDir = 0; szDir[StartOfDir] != '\0'; StartOfDir++);
- for (; szDir[StartOfDir] != '\\'; StartOfDir--);
- szDir[++StartOfDir] = '*';
- if (StartOfDir + 1 >= MAX_PATH) // ERROR checking.
- {
- MessageBox(NULL, TEXT("Directory is to long where the exectuable is. Can not load Plugins"), TEXT("ERROR"), MB_OK);
- return;
- }
- szDir[StartOfDir + 1] = '\0';
- // ============================================================
- WIN32_FIND_DATA ffd;
- HANDLE hFind = INVALID_HANDLE_VALUE;
- DWORD dwError = ERROR_NO_MORE_FILES;
- // Find the first file in the directory.
- hFind = FindFirstFile(szDir, &ffd);
- if (INVALID_HANDLE_VALUE == hFind)
- {
- MessageBox(NULL, TEXT("FindFirstFile error!"), TEXT("ERROR"), MB_OK);
- return;
- }
- // List all the files in the directory with some info about them.
- int FileLenght;
- do
- {
- if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- {
- for (FileLenght = 0; // Add the file found to szDir(c string).
- StartOfDir + FileLenght < (MAX_PATH - 1),
- szDir[StartOfDir + FileLenght] = ffd.cFileName[FileLenght],
- ffd.cFileName[FileLenght] != '\0';
- FileLenght++);
- szDir[StartOfDir + FileLenght] = '\0';
- HMODULE hMod = ::LoadLibrary(szDir);
- if (hMod != NULL)
- {
- PLUGIN_PROCESS_DATA pProcessData = (PLUGIN_PROCESS_DATA) ::GetProcAddress(hMod, "ProcessData");
- if (pProcessData != NULL)
- {
- // Yes, this DLL exposes the function we need, it is a plugin we can use.
- // Add it to Plugins vector.
- m_Plugins.push_back(make_PluginElements(hMod, pProcessData, &(szDir[StartOfDir])));
- }
- }
- }
- } while (FindNextFile(hFind, &ffd) != 0);
- dwError = GetLastError();
- if (dwError != ERROR_NO_MORE_FILES)
- {
- MessageBox(NULL, TEXT("FindFirstFile error!"), TEXT("ERROR"), MB_OK);
- }
- // Close the handle to find data in directory.
- FindClose(hFind);
- }
- void w2chTools::UnloadPlugins()
- {
- for (std::vector<PluginElements>::size_type i = m_Plugins.size() - 1;
- i != (std::vector<PluginElements>::size_type) - 1; i--)
- {
- FreeLibrary(m_Plugins[i].hMod);
- }
- }
- INT_PTR CALLBACK w2chTools::DialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
- {
- if (Message == WM_INITDIALOG)
- {
- w2chTools *pw2chTools = reinterpret_cast<w2chTools *>(lParam);
- ::SetWindowLongPtrW(hwnd, GWLP_USERDATA, PtrToUlong(pw2chTools));
- pw2chTools->m_hwndInputEdit = GetDlgItem(hwnd, IDC_RICHEDIT22);
- pw2chTools->m_hwndOutpuEdit = GetDlgItem(hwnd, IDC_RICHEDIT21);
- pw2chTools->m_ComboBox = GetDlgItem(hwnd, IDC_COMBO1);
- // Add all the plugin names to combo box list for user to choose what action to do.
- for (std::vector<PluginElements>::size_type i = 0; i < pw2chTools->m_Plugins.size(); ++i)
- {
- SendMessage(pw2chTools->m_ComboBox, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(pw2chTools->m_Plugins[i].szFileName));
- }
- return TRUE;
- }
- else
- {
- w2chTools *pw2chTools = reinterpret_cast<w2chTools *>(static_cast<LONG_PTR>(
- ::GetWindowLongPtrW(hwnd, GWLP_USERDATA)));
- if (pw2chTools)
- {
- switch (Message)
- {
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case IDC_BUTTON1: // Clear button.
- SetWindowText(pw2chTools->m_hwndOutpuEdit, TEXT(""));
- break;
- case IDC_BUTTON2: // Ok button.
- {
- signed int ItemIndex = SendMessage(reinterpret_cast<HWND>(pw2chTools->m_ComboBox), static_cast<UINT>(CB_GETCURSEL), static_cast<WPARAM>(0), static_cast<LPARAM>(0));
- if (CB_ERR != ItemIndex) pw2chTools->m_Plugins[ItemIndex].ProcessData(pw2chTools->m_hwndInputEdit, pw2chTools->m_hwndOutpuEdit);
- }
- break;
- }
- return 0;
- case WM_CLOSE:
- DestroyWindow(hwnd);
- return TRUE;
- case WM_DESTROY:
- PostQuitMessage(0);
- return TRUE;
- default:
- break;
- }
- }
- }
- return FALSE;
- }
Advertisement
Add Comment
Please, Sign In to add comment