Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dboot.cpp : Defines the entry point for the application.
- //
- #include "stdafx.h"
- #include "dboot.h"
- #include "hook.h"
- #include "simpleini.h"
- static _SetWindowsHookExW SetWindowsHookEx;
- static _UnhookWindowsHookEx UnhookWindowsHookEx;
- static _CallNextHookEx CallNextHookEx;
- #define ID_TIMER_BOOTEND 101
- struct Config {
- TCHAR* winName;
- TCHAR* hAppPath;
- TCHAR* vAppPath;
- };
- // Global Variables:
- HINSTANCE g_hInst; // current instance
- BOOL g_bootEnd;
- HINSTANCE g_hCore;
- HHOOK g_hHook;
- POINT g_ptPos;
- BOOL g_pressed;
- Config* g_config;
- DWORD g_count;
- // Forward declarations of functions included in this code module:
- ATOM MyRegisterClass(HINSTANCE, LPTSTR);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void ProcConfig(DWORD index, WPARAM wParam, PMSLLHOOKSTRUCT pMouseHookStruct);
- LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
- BOOL ActivateMHook();
- void DeactivateMHook();
- BOOL LoadPathSt();
- BOOL FileExist(TCHAR *file);
- BOOL ShellExe(TCHAR *file, TCHAR* params);
- BOOL CreateDesktopShortcut(TCHAR* name, CHAR* target);
- BOOL StartWinCE();
- void ProcConfig(DWORD index, WPARAM wParam, POINT pt)
- {
- switch (wParam)
- {
- case WM_LBUTTONDOWN:
- g_pressed = TRUE;
- g_ptPos = pt;
- break;
- case WM_LBUTTONUP:
- g_pressed = FALSE;
- break;
- case WM_MOUSEMOVE:
- if (g_pressed) {
- if (g_config[index].hAppPath != NULL && abs(g_ptPos.x - pt.x) > 300) {
- g_pressed = FALSE;
- NKDbgPrintfW(L"Horizontal move detected\n");
- ShellExe(g_config[index].hAppPath, NULL);
- }
- if (g_config[index].vAppPath != NULL && abs(g_ptPos.y - pt.y) > 200) {
- g_pressed = FALSE;
- NKDbgPrintfW(L"Vertical move detected\n");
- ShellExe(g_config[index].vAppPath, NULL);
- }
- }
- break;
- }
- }
- LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- if ((nCode != HC_ACTION) || (!g_pressed && wParam != WM_LBUTTONDOWN))
- return CallNextHookEx(g_hHook, nCode, wParam, lParam);
- PMSLLHOOKSTRUCT pMouseHookStruct = (PMSLLHOOKSTRUCT)lParam;
- TCHAR name[MAX_PATH];
- if (GetClassName(GetForegroundWindow(), name, MAX_PATH) > 0) {
- for (DWORD i=0;i<g_count;i++) {
- if (wcsncmp(name, g_config[i].winName, wcslen(g_config[i].winName)) == 0) {
- ProcConfig(i, wParam, pMouseHookStruct->pt);
- break;
- }
- }
- }
- return CallNextHookEx(g_hHook, nCode, wParam, lParam);
- }
- void DeactivateMHook()
- {
- if (g_hHook != NULL) {
- UnhookWindowsHookEx(g_hHook);
- g_hHook = NULL;
- }
- if (g_config != NULL) {
- for (DWORD i=0;i<g_count;i++) {
- if (g_config[i].winName != NULL) {
- delete g_config[i].winName;
- g_config[i].winName = NULL;
- }
- if (g_config[i].hAppPath != NULL) {
- delete g_config[i].hAppPath;
- g_config[i].hAppPath = NULL;
- }
- if (g_config[i].vAppPath != NULL) {
- delete g_config[i].vAppPath;
- g_config[i].vAppPath = NULL;
- }
- }
- delete[] g_config;
- g_config = NULL;
- }
- if (g_hCore != NULL) {
- FreeLibrary(g_hCore);
- g_hCore = NULL;
- }
- }
- BOOL LoadPathSt()
- {
- g_count = 0;
- CSimpleIni ini(TRUE, FALSE, FALSE);
- SI_Error rc = ini.LoadFile(_T("\\Storage Card\\System\\dboot.ini"));
- if (rc < 0)
- return FALSE;
- CSimpleIni::TNamesDepend sections;
- ini.GetAllSections(sections);
- if (sections.size() < 1)
- return FALSE;
- g_config = new Config[sections.size()];
- const TCHAR* happ;
- const TCHAR* vapp;
- CSimpleIni::TNamesDepend::const_iterator i;
- for (i = sections.begin(); i != sections.end(); ++i) {
- happ = ini.GetValue(i->pItem, _T("happ"), NULL);
- vapp = ini.GetValue(i->pItem, _T("vapp"), NULL);
- if (happ == NULL && vapp == NULL)
- continue;
- g_config[g_count].winName = new TCHAR[wcslen(i->pItem) + 1];
- wcscpy_s(g_config[g_count].winName, wcslen(i->pItem) + 1, i->pItem);
- if (happ != NULL) {
- g_config[g_count].hAppPath = new TCHAR[wcslen(happ) + 1];
- wcscpy_s(g_config[g_count].hAppPath, wcslen(happ) + 1, happ);
- }
- if (vapp != NULL) {
- g_config[g_count].vAppPath = new TCHAR[wcslen(vapp) + 1];
- wcscpy_s(g_config[g_count].vAppPath, wcslen(vapp) + 1, vapp);
- }
- g_count++;
- }
- return TRUE;
- }
- BOOL ActivateMHook()
- {
- if (g_hHook != NULL)
- return TRUE;
- if (!LoadPathSt())
- return FALSE;
- if (g_hCore == NULL) {
- g_hCore = LoadLibrary(_T("coredll.dll"));
- if (g_hCore == NULL) {
- NKDbgPrintfW(L"[Mouse hook] LoadLibrary core.dll failed\n");
- return FALSE;
- }
- }
- SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(g_hCore, _T("SetWindowsHookExW"));
- if (SetWindowsHookEx == NULL) {
- NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx not found\n");
- return FALSE;
- }
- CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hCore, _T("CallNextHookEx"));
- if (CallNextHookEx == NULL) {
- NKDbgPrintfW(L"[Mouse hook] CallNextHookEx not found\n");
- return FALSE;
- }
- UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hCore, _T("UnhookWindowsHookEx"));
- if (UnhookWindowsHookEx == NULL) {
- NKDbgPrintfW(L"[Mouse hook] UnhookWindowsHookEx not found\n");
- return FALSE;
- }
- g_hHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
- if (g_hHook == NULL) {
- NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx WH_MOUSE_LL failed\n");
- return FALSE;
- }
- return TRUE;
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- HKEY regKey = NULL;
- DWORD Disposition;
- if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("ControlPanel\\Comm"), 0, 0, REG_OPTION_NON_VOLATILE, 0,
- NULL, ®Key, &Disposition) == ERROR_SUCCESS)
- {
- TCHAR* cnct = _T("`Default USB`");
- RegSetValueEx(regKey, TEXT("Cnct"), 0, REG_SZ, (const BYTE*)cnct, (wcslen(cnct) + 1) * sizeof( TCHAR));
- DWORD autocnct = 1;
- RegSetValueEx(regKey, TEXT("AutoCnct"), 0, REG_DWORD, (const BYTE*)&autocnct, sizeof(autocnct));
- RegCloseKey(regKey);
- }
- HANDLE hMgr;
- DWORD dwXfer;
- hMgr = CreateFile(L"MGR1:", 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hMgr != INVALID_HANDLE_VALUE) {
- BYTE gpio0Enable[2] = {0,1};
- DeviceIoControl(hMgr, 2, gpio0Enable, 2, NULL, 0, &dwXfer, NULL);
- CloseHandle(hMgr);
- }
- if (FileExist(_T("\\Storage Card3\\StartWinCE")) ||
- !ShellExe(_T("\\Storage Card\\System\\UpgradeManager.exe"), lpCmdLine))
- {
- StartWinCE();
- return 0;
- }
- MSG msg;
- // Perform application initialization:
- if (!InitInstance(hInstance, nCmdShow))
- return FALSE;
- // Main message loop:
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
- //
- // FUNCTION: MyRegisterClass()
- //
- // PURPOSE: Registers the window class.
- //
- // COMMENTS:
- //
- ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
- {
- WNDCLASS wc;
- memset(&wc, 0, sizeof(WNDCLASS));
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WndProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = szWindowClass;
- return RegisterClass(&wc);
- }
- //
- // FUNCTION: InitInstance(HINSTANCE, int)
- //
- // PURPOSE: Saves instance handle and creates main window
- //
- // COMMENTS:
- //
- // In this function, we save the instance handle in a global variable and
- // create and display the main program window.
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- g_hInst = hInstance; // Store instance handle in our global variable
- if (!MyRegisterClass(hInstance, _T("dboot")))
- return FALSE;
- hWnd = CreateWindow(_T("dboot"), _T("dboot"), WS_VISIBLE,
- CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- return FALSE;
- RECT rc;
- GetWindowRect(hWnd, &rc);
- SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 800, 480, 0);
- SetForegroundWindow((HWND)(((ULONG) hWnd) | 0x01));
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- g_pressed = FALSE;
- g_bootEnd = FALSE;
- g_hCore = NULL;
- g_hHook = NULL;
- g_config = NULL;
- SetWindowsHookEx = NULL;
- CallNextHookEx = NULL;
- UnhookWindowsHookEx = NULL;
- SetTimer(hWnd, ID_TIMER_BOOTEND, 5000, NULL);
- return TRUE;
- }
- //
- // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
- //
- // PURPOSE: Processes messages for the main window.
- //
- // WM_COMMAND - process the application menu
- // WM_PAINT - Paint the main window
- // WM_DESTROY - post a quit message and return
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- RECT rec;
- switch (message)
- {
- case WM_LBUTTONDOWN:
- g_pressed = TRUE;
- g_ptPos.x = LOWORD(lParam);
- break;
- case WM_LBUTTONUP:
- g_pressed = FALSE;
- break;
- case WM_MOUSEMOVE:
- if (!g_bootEnd && g_pressed) {
- if (abs(g_ptPos.x - LOWORD(lParam)) > 300) {
- g_pressed = FALSE;
- ShellExe(_T("\\Storage Card\\System\\dmenu.exe"), NULL);
- NKDbgPrintfW(L"Start dmenu\n");
- }
- }
- break;
- case WM_PAINT:
- if (!g_bootEnd) {
- hdc = BeginPaint(hWnd, &ps);
- SetTextColor(hdc, RGB(245,245,245));
- SetBkMode(hdc, TRANSPARENT);
- SetRect(&rec,20,450,200,470);
- DrawText(hdc, TEXT("DBOOT 2.0 - 2017"),strlen("DBOOT 2.0 - 2017"), &rec, DT_TOP|DT_LEFT);
- EndPaint(hWnd, &ps);
- }
- break;
- case WM_DESTROY:
- DeactivateMHook();
- PostQuitMessage(0);
- break;
- case WM_TIMER:
- if (wParam == ID_TIMER_BOOTEND) {
- g_bootEnd = TRUE;
- SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, 0);
- ShowWindow(hWnd, SW_HIDE);
- KillTimer(hWnd, ID_TIMER_BOOTEND);
- if (!ActivateMHook()) {
- DeactivateMHook();
- PostQuitMessage(0);
- }
- }
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- BOOL FileExist(TCHAR *file)
- {
- BOOL rc = 0;
- DWORD attribs = GetFileAttributes(file);
- if (attribs != -1) {
- if ( (attribs & FILE_ATTRIBUTE_DIRECTORY) == 0)
- rc = 1;
- }
- return rc;
- }
- BOOL ShellExe(TCHAR* file, TCHAR* params)
- {
- if (file == NULL || !FileExist(file))
- return FALSE;
- SHELLEXECUTEINFO sei;
- memset(&sei, 0, sizeof(sei));
- sei.cbSize = sizeof(sei);
- sei.nShow = SW_SHOWNORMAL;
- sei.hwnd = NULL;
- sei.lpParameters = params;
- sei.lpFile = file;
- return ShellExecuteEx(&sei);
- }
- BOOL CreateDesktopShortcut(TCHAR* name, CHAR* target)
- {
- if (name == NULL || target == NULL)
- return FALSE;
- TCHAR fullPath[MAX_PATH];
- swprintf_s(fullPath, MAX_PATH, _T("\\Windows\\Desktop\\%s.lnk"), name);
- HANDLE hFile;
- hFile = CreateFile(fullPath,
- GENERIC_WRITE, // Open for writing
- 0, // Do not share
- NULL, // No security
- OPEN_ALWAYS, // Open or create
- FILE_ATTRIBUTE_NORMAL, // Normal file
- NULL); // No template file
- if (hFile == INVALID_HANDLE_VALUE)
- return FALSE;
- CHAR targetPath[MAX_PATH];
- sprintf_s(targetPath, MAX_PATH, "%d#\"%s\"", (strlen(target)+2), target);
- DWORD nbrWr;
- WriteFile(hFile, targetPath, strlen(targetPath), &nbrWr, 0);
- CloseHandle(hFile);
- return TRUE;
- }
- BOOL StartWinCE()
- {
- DeleteFile(_T("\\Storage Card3\\StartWinCE"));
- CreateDesktopShortcut(_T("Redemarrer"), "\\Storage Card\\System\\cereboot.exe");
- CreateDesktopShortcut(_T("Redemarrer WinCE"), "\\Storage Card\\System\\dmenu.exe");
- ShellExe(_T("\\Windows\\explorer.exe"), NULL);
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement