Advertisement
Guest User

dboot.cpp

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