Advertisement
Guest User

dboot.cpp

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