Advertisement
Guest User

dboot.cpp

a guest
Jan 8th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // dboot.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "dboot.h"
  6. #include "hook.h"
  7. #include "simpleini.h"
  8.  
  9. static _SetWindowsHookExW   SetWindowsHookEx;
  10. static _UnhookWindowsHookEx UnhookWindowsHookEx;
  11. static _CallNextHookEx      CallNextHookEx;
  12.  
  13. #define ID_TIMER_BOOTEND 101
  14.  
  15. struct Config {
  16.     TCHAR* winName;
  17.     TCHAR* hAppPath;
  18.     TCHAR* vAppPath;
  19. };
  20.  
  21. // Global Variables:
  22. HINSTANCE           g_hInst;            // current instance
  23. BOOL                g_bootEnd;
  24. HINSTANCE           g_hCore;
  25. HHOOK               g_hHook;
  26.  
  27. POINT               g_ptPos;
  28. BOOL                g_pressed;
  29. Config*             g_config;
  30. DWORD               g_count;
  31.  
  32. // Forward declarations of functions included in this code module:
  33. ATOM            MyRegisterClass(HINSTANCE, LPTSTR);
  34. BOOL            InitInstance(HINSTANCE, int);
  35. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  36.  
  37. void ProcConfig(DWORD index, WPARAM wParam, PMSLLHOOKSTRUCT pMouseHookStruct);
  38. LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
  39. BOOL ActivateMHook();
  40. void DeactivateMHook();
  41. BOOL LoadPathSt();
  42.  
  43. BOOL FileExist(TCHAR *file);
  44. BOOL ShellExe(TCHAR *file, TCHAR* params);
  45. BOOL CreateDesktopShortcut(TCHAR* name, CHAR* target);
  46. BOOL StartWinCE();
  47.  
  48. void ProcConfig(DWORD index, WPARAM wParam, POINT pt)
  49. {
  50.     switch (wParam)
  51.     {
  52.         case WM_LBUTTONDOWN:
  53.             g_pressed = TRUE;
  54.             g_ptPos = pt;
  55.             break;
  56.         case WM_LBUTTONUP:
  57.             g_pressed = FALSE;
  58.             break;
  59.         case WM_MOUSEMOVE:
  60.             if (g_pressed) {
  61.                 if (g_config[index].hAppPath != NULL && abs(g_ptPos.x - pt.x) > 300) {
  62.                     g_pressed = FALSE;
  63.                     NKDbgPrintfW(L"Horizontal move detected\n");
  64.                     ShellExe(g_config[index].hAppPath, NULL);
  65.                 }
  66.                
  67.                 if (g_config[index].vAppPath != NULL && abs(g_ptPos.y - pt.y) > 200) {
  68.                     g_pressed = FALSE;
  69.                     NKDbgPrintfW(L"Vertical move detected\n");
  70.                     ShellExe(g_config[index].vAppPath, NULL);
  71.                 }
  72.             }
  73.             break;
  74.     }
  75. }
  76.  
  77. LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
  78. {
  79.     if ((nCode != HC_ACTION) || (!g_pressed && wParam != WM_LBUTTONDOWN))
  80.         return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  81.  
  82.     PMSLLHOOKSTRUCT pMouseHookStruct = (PMSLLHOOKSTRUCT)lParam;
  83.  
  84.     TCHAR name[MAX_PATH];
  85.     if (GetClassName(GetForegroundWindow(), name, MAX_PATH) > 0) {
  86.         for (DWORD i=0;i<g_count;i++) {
  87.             if (wcsncmp(name, g_config[i].winName, wcslen(g_config[i].winName)) == 0) {
  88.                 ProcConfig(i, wParam, pMouseHookStruct->pt);
  89.                 break;
  90.             }
  91.         }
  92.     }
  93.  
  94.     return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  95. }
  96.  
  97. void DeactivateMHook()
  98. {
  99.     if (g_hHook != NULL) {
  100.         UnhookWindowsHookEx(g_hHook);
  101.         g_hHook = NULL;
  102.     }
  103.  
  104.     if (g_config != NULL) {
  105.         for (DWORD i=0;i<g_count;i++) {
  106.             if (g_config[i].winName != NULL) {
  107.                 delete g_config[i].winName;
  108.                 g_config[i].winName = NULL;
  109.             }
  110.             if (g_config[i].hAppPath != NULL) {
  111.                 delete g_config[i].hAppPath;
  112.                 g_config[i].hAppPath = NULL;
  113.             }
  114.             if (g_config[i].vAppPath != NULL) {
  115.                 delete g_config[i].vAppPath;
  116.                 g_config[i].vAppPath = NULL;
  117.             }
  118.         }
  119.  
  120.         delete[] g_config;
  121.         g_config = NULL;
  122.     }
  123.  
  124.     if (g_hCore != NULL) {
  125.         FreeLibrary(g_hCore);
  126.         g_hCore = NULL;
  127.     }
  128. }
  129.  
  130. BOOL LoadPathSt()
  131. {
  132.     g_count = 0;
  133.  
  134.     CSimpleIni ini(TRUE, FALSE, FALSE);
  135.     SI_Error rc = ini.LoadFile(_T("\\Storage Card\\System\\dboot.ini"));
  136.     if (rc < 0)
  137.         return FALSE;
  138.  
  139.     CSimpleIni::TNamesDepend sections;
  140.     ini.GetAllSections(sections);
  141.  
  142.     if (sections.size() < 1)
  143.         return FALSE;
  144.  
  145.     g_config = new Config[sections.size()];
  146.  
  147.     const TCHAR* happ;
  148.     const TCHAR* vapp;
  149.  
  150.     CSimpleIni::TNamesDepend::const_iterator i;
  151.     for (i = sections.begin(); i != sections.end(); ++i) {
  152.         happ = ini.GetValue(i->pItem, _T("happ"), NULL);
  153.         vapp = ini.GetValue(i->pItem, _T("vapp"), NULL);
  154.  
  155.         if (happ == NULL && vapp == NULL)
  156.             continue;
  157.  
  158.         g_config[g_count].winName = new TCHAR[wcslen(i->pItem) + 1];
  159.         wcscpy_s(g_config[g_count].winName, wcslen(i->pItem) + 1, i->pItem);
  160.  
  161.         if (happ != NULL) {
  162.             g_config[g_count].hAppPath = new TCHAR[wcslen(happ) + 1];
  163.             wcscpy_s(g_config[g_count].hAppPath, wcslen(happ) + 1, happ);
  164.         }
  165.  
  166.         if (vapp != NULL) {
  167.             g_config[g_count].vAppPath = new TCHAR[wcslen(vapp) + 1];
  168.             wcscpy_s(g_config[g_count].vAppPath, wcslen(vapp) + 1, vapp);
  169.         }
  170.  
  171.         g_count++;
  172.     }
  173.  
  174.     return TRUE;
  175. }
  176.  
  177. BOOL ActivateMHook()
  178. {
  179.     if (g_hHook != NULL)
  180.         return TRUE;
  181.  
  182.     if (!LoadPathSt())
  183.         return FALSE;
  184.  
  185.     if (g_hCore == NULL) {
  186.         g_hCore = LoadLibrary(_T("coredll.dll"));
  187.         if (g_hCore == NULL) {
  188.             NKDbgPrintfW(L"[Mouse hook] LoadLibrary core.dll failed\n");
  189.             return FALSE;
  190.         }
  191.     }
  192.  
  193.     SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(g_hCore, _T("SetWindowsHookExW"));
  194.     if (SetWindowsHookEx == NULL) {
  195.         NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx not found\n");
  196.         return FALSE;
  197.     }
  198.  
  199.     CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hCore, _T("CallNextHookEx"));
  200.     if (CallNextHookEx == NULL) {
  201.         NKDbgPrintfW(L"[Mouse hook] CallNextHookEx not found\n");
  202.         return FALSE;
  203.     }
  204.  
  205.     UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hCore, _T("UnhookWindowsHookEx"));
  206.     if (UnhookWindowsHookEx == NULL) {
  207.         NKDbgPrintfW(L"[Mouse hook] UnhookWindowsHookEx not found\n");
  208.         return FALSE;
  209.     }
  210.  
  211.     g_hHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  212.     if (g_hHook == NULL) {
  213.         NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx WH_MOUSE_LL failed\n");
  214.         return FALSE;
  215.     }
  216.  
  217.     return TRUE;
  218. }
  219.  
  220. int WINAPI WinMain(HINSTANCE hInstance,
  221.                    HINSTANCE hPrevInstance,
  222.                    LPTSTR    lpCmdLine,
  223.                    int       nCmdShow)
  224. {
  225.     HKEY regKey = NULL;
  226.     DWORD Disposition;
  227.     if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("ControlPanel\\Comm"), 0, 0, REG_OPTION_NON_VOLATILE, 0,
  228.         NULL, &regKey, &Disposition) == ERROR_SUCCESS)
  229.     {
  230.         TCHAR* cnct = _T("`Default USB`");
  231.         RegSetValueEx(regKey, TEXT("Cnct"), 0, REG_SZ, (const BYTE*)cnct, (wcslen(cnct) + 1) * sizeof( TCHAR));
  232.         DWORD autocnct = 1;
  233.         RegSetValueEx(regKey, TEXT("AutoCnct"), 0, REG_DWORD, (const BYTE*)&autocnct, sizeof(autocnct));
  234.         RegCloseKey(regKey);
  235.     }
  236.  
  237.     HANDLE hMgr;
  238.     DWORD dwXfer;
  239.     hMgr = CreateFile(L"MGR1:", 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  240.     if (hMgr != INVALID_HANDLE_VALUE) {
  241.         BYTE gpio0Enable[2] = {0,1};
  242.         DeviceIoControl(hMgr, 2, gpio0Enable, 2, NULL, 0, &dwXfer, NULL);
  243.         CloseHandle(hMgr);
  244.     }
  245.  
  246.     if (FileExist(_T("\\Storage Card3\\StartWinCE")) ||
  247.         !ShellExe(_T("\\Storage Card\\System\\UpgradeManager.exe"), lpCmdLine))
  248.     {
  249.         StartWinCE();
  250.         return 0;
  251.     }
  252.    
  253.     MSG msg;
  254.  
  255.     // Perform application initialization:
  256.     if (!InitInstance(hInstance, nCmdShow))
  257.         return FALSE;
  258.  
  259.     // Main message loop:
  260.     while (GetMessage(&msg, NULL, 0, 0)) {
  261.         TranslateMessage(&msg);
  262.         DispatchMessage(&msg);
  263.     }
  264.  
  265.     return (int) msg.wParam;
  266. }
  267.  
  268. //
  269. //  FUNCTION: MyRegisterClass()
  270. //
  271. //  PURPOSE: Registers the window class.
  272. //
  273. //  COMMENTS:
  274. //
  275. ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
  276. {
  277.     WNDCLASS wc;
  278.     memset(&wc, 0, sizeof(WNDCLASS));
  279.  
  280.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  281.     wc.lpfnWndProc   = WndProc;
  282.     wc.hInstance     = hInstance;
  283.     wc.lpszClassName = szWindowClass;
  284.  
  285.     return RegisterClass(&wc);
  286. }
  287.  
  288. //
  289. //   FUNCTION: InitInstance(HINSTANCE, int)
  290. //
  291. //   PURPOSE: Saves instance handle and creates main window
  292. //
  293. //   COMMENTS:
  294. //
  295. //        In this function, we save the instance handle in a global variable and
  296. //        create and display the main program window.
  297. //
  298. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  299. {
  300.     HWND hWnd;
  301.  
  302.     g_hInst = hInstance; // Store instance handle in our global variable
  303.  
  304.     if (!MyRegisterClass(hInstance, _T("dboot")))
  305.         return FALSE;
  306.  
  307.     hWnd = CreateWindow(_T("dboot"), _T("dboot"), WS_VISIBLE,
  308.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  309.  
  310.     if (!hWnd)
  311.         return FALSE;
  312.  
  313.     RECT rc;
  314.     GetWindowRect(hWnd, &rc);
  315.  
  316.     SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 800, 480, 0);
  317.     SetForegroundWindow((HWND)(((ULONG) hWnd) | 0x01));
  318.     ShowWindow(hWnd, nCmdShow);
  319.     UpdateWindow(hWnd);
  320.  
  321.     g_pressed               = FALSE;
  322.     g_bootEnd               = FALSE;
  323.     g_hCore                 = NULL;
  324.     g_hHook                 = NULL;
  325.     g_config                = NULL;
  326.     SetWindowsHookEx        = NULL;
  327.     CallNextHookEx          = NULL;
  328.     UnhookWindowsHookEx     = NULL;
  329.  
  330.     SetTimer(hWnd, ID_TIMER_BOOTEND, 5000, NULL);
  331.  
  332.     return TRUE;
  333. }
  334.  
  335. //
  336. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  337. //
  338. //  PURPOSE:  Processes messages for the main window.
  339. //
  340. //  WM_COMMAND  - process the application menu
  341. //  WM_PAINT    - Paint the main window
  342. //  WM_DESTROY  - post a quit message and return
  343. //
  344. //
  345.  
  346. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  347. {
  348.     PAINTSTRUCT ps;
  349.     HDC hdc;
  350.     RECT rec;
  351.    
  352.     switch (message)
  353.     {
  354.         case WM_LBUTTONDOWN:
  355.             g_pressed = TRUE;
  356.             g_ptPos.x = LOWORD(lParam);
  357.             break;
  358.         case WM_LBUTTONUP:
  359.             g_pressed = FALSE;
  360.             break;
  361.         case WM_MOUSEMOVE:
  362.             if (!g_bootEnd && g_pressed) {
  363.                 if (abs(g_ptPos.x - LOWORD(lParam)) > 300) {
  364.                     g_pressed = FALSE;
  365.                     ShellExe(_T("\\Storage Card\\System\\dmenu.exe"), NULL);
  366.                     NKDbgPrintfW(L"Start dmenu\n");
  367.                 }
  368.             }
  369.             break;
  370.         case WM_PAINT:
  371.             if (!g_bootEnd) {
  372.                 hdc = BeginPaint(hWnd, &ps);
  373.  
  374.                 SetTextColor(hdc, RGB(245,245,245));
  375.                 SetBkMode(hdc, TRANSPARENT);
  376.  
  377.                 SetRect(&rec,20,450,200,470);
  378.                 DrawText(hdc, TEXT("DBOOT 2.0 - 2017"),strlen("DBOOT 2.0 - 2017"), &rec, DT_TOP|DT_LEFT);
  379.                
  380.                 EndPaint(hWnd, &ps);
  381.             }
  382.             break;
  383.         case WM_DESTROY:
  384.             DeactivateMHook();
  385.             PostQuitMessage(0);
  386.             break;
  387.         case WM_TIMER:
  388.             if (wParam == ID_TIMER_BOOTEND) {
  389.                 g_bootEnd = TRUE;
  390.                 SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, 0);
  391.                 ShowWindow(hWnd, SW_HIDE);
  392.                 KillTimer(hWnd, ID_TIMER_BOOTEND);
  393.  
  394.                 if (!ActivateMHook()) {
  395.                     DeactivateMHook();
  396.                     PostQuitMessage(0);
  397.                 }
  398.             }
  399.             break;
  400.         default:
  401.             return DefWindowProc(hWnd, message, wParam, lParam);
  402.     }
  403.     return 0;
  404. }
  405.  
  406. BOOL FileExist(TCHAR *file)
  407. {
  408.     BOOL rc = 0;
  409.  
  410.     DWORD attribs = GetFileAttributes(file);
  411.     if (attribs != -1) {
  412.         if ( (attribs & FILE_ATTRIBUTE_DIRECTORY) == 0)
  413.             rc = 1;
  414.     }
  415.  
  416.     return rc;
  417. }
  418.  
  419. BOOL ShellExe(TCHAR* file, TCHAR* params)
  420. {
  421.     if (file == NULL || !FileExist(file))
  422.         return FALSE;
  423.  
  424.     SHELLEXECUTEINFO sei;
  425.     memset(&sei, 0, sizeof(sei));
  426.     sei.cbSize = sizeof(sei);
  427.     sei.nShow = SW_SHOWNORMAL;
  428.     sei.hwnd = NULL;
  429.     sei.lpParameters = params;
  430.     sei.lpFile = file;
  431.  
  432.     return ShellExecuteEx(&sei);
  433. }
  434.  
  435. BOOL CreateDesktopShortcut(TCHAR* name, CHAR* target)
  436. {
  437.     if (name == NULL || target == NULL)
  438.         return FALSE;
  439.  
  440.     TCHAR fullPath[MAX_PATH];
  441.     swprintf_s(fullPath, MAX_PATH, _T("\\Windows\\Desktop\\%s.lnk"), name);
  442.  
  443.     HANDLE hFile;
  444.     hFile = CreateFile(fullPath,
  445.                         GENERIC_WRITE,          // Open for writing
  446.                         0,                      // Do not share
  447.                         NULL,                   // No security
  448.                         OPEN_ALWAYS,            // Open or create
  449.                         FILE_ATTRIBUTE_NORMAL,  // Normal file
  450.                         NULL);                  // No template file
  451.  
  452.     if (hFile == INVALID_HANDLE_VALUE)
  453.         return FALSE;
  454.    
  455.     CHAR targetPath[MAX_PATH];
  456.     sprintf_s(targetPath, MAX_PATH, "%d#\"%s\"", (strlen(target)+2), target);
  457.  
  458.     DWORD nbrWr;
  459.     WriteFile(hFile, targetPath, strlen(targetPath), &nbrWr, 0);
  460.     CloseHandle(hFile);
  461.  
  462.     return TRUE;
  463. }
  464.  
  465. BOOL StartWinCE()
  466. {
  467.     DeleteFile(_T("\\Storage Card3\\StartWinCE"));
  468.  
  469.     CreateDesktopShortcut(_T("Redemarrer"), "\\Storage Card\\System\\cereboot.exe");
  470.     CreateDesktopShortcut(_T("Redemarrer WinCE"), "\\Storage Card\\System\\dmenu.exe");
  471.  
  472.     ShellExe(_T("\\Windows\\explorer.exe"), NULL);
  473.  
  474.     return TRUE;
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement