home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / Banner / Banner.c next >
C/C++ Source or Header  |  2003-11-12  |  4KB  |  175 lines

  1. #include <windows.h>
  2. #include "../exdll/exdll.h"
  3. #include "../../Source/exehead/resource.h"
  4.  
  5. // Turn a pair of chars into a word
  6. // Turn four chars into a dword
  7. #ifdef __BIG_ENDIAN__ // Not very likely, but, still...
  8. #define CHAR2_TO_WORD(a,b) (((WORD)(b))|((a)<<8))
  9. #define CHAR4_TO_DWORD(a,b,c,d)    (((DWORD)CHAR2_TO_WORD(c,d))|(CHAR2_TO_WORD(a,b)<<16))
  10. #else
  11. #define CHAR2_TO_WORD(a,b) (((WORD)(a))|((b)<<8))
  12. #define CHAR4_TO_DWORD(a,b,c,d)    (((DWORD)CHAR2_TO_WORD(a,b))|(CHAR2_TO_WORD(c,d)<<16))
  13. #endif
  14.  
  15. HINSTANCE hInstance;
  16. HWND hwBanner;
  17. HANDLE hThread;
  18.  
  19. char buf[1024];
  20.  
  21. unsigned int myatoi(char *s);
  22.  
  23. BOOL CALLBACK BannerProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  24. {
  25.   if (uMsg == WM_INITDIALOG)
  26.   {
  27.     int iMainStringSet = 0;
  28.  
  29.     popstring(buf);
  30.     while (*(int*)buf == CHAR4_TO_DWORD('/','s','e','t') && !buf[4])
  31.     {
  32.       unsigned int id;
  33.       popstring(buf);
  34.       id = myatoi(buf);
  35.       popstring(buf);
  36.       SetDlgItemText(hwndDlg, id, buf);
  37.       popstring(buf);
  38.  
  39.       if (id == IDC_STR)
  40.         iMainStringSet++;
  41.     }
  42.  
  43.     SetWindowText(hwndDlg, buf);
  44.     if (!iMainStringSet)
  45.       SetDlgItemText(hwndDlg, IDC_STR, buf);
  46.  
  47.     if (!*buf)
  48.       SetWindowLong(hwndDlg, GWL_EXSTYLE, GetWindowLong(hwndDlg, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
  49.   }
  50.   if (uMsg == WM_CLOSE)
  51.   {
  52.     DestroyWindow(hwndDlg);
  53.   }
  54.   return 0;
  55. }
  56.  
  57. DWORD WINAPI BannerThread(LPVOID lpParameter)
  58. {
  59.   HWND hwndParent = (HWND) lpParameter;
  60.   HWND lhwBanner;
  61.   MSG msg;
  62.   //BOOL bRet;
  63.  
  64.   lhwBanner = CreateDialog(
  65.     GetModuleHandle(0),
  66.     MAKEINTRESOURCE(IDD_VERIFY),
  67.     hwndParent,
  68.     BannerProc
  69.   );
  70.  
  71.   while (IsWindow(lhwBanner))
  72.   {
  73.     if (PeekMessage(&msg, lhwBanner, 0, 0, PM_REMOVE))
  74.     {
  75.       DispatchMessage(&msg);
  76.     }
  77.     else
  78.     {
  79.       hwBanner = lhwBanner;
  80.       WaitMessage();
  81.     }
  82.   }
  83.  
  84.   hwBanner = NULL;
  85.  
  86.   return 0;
  87. }
  88.  
  89. void __declspec(dllexport) show(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
  90. {
  91.   EXDLL_INIT();
  92.  
  93.   {
  94.     DWORD dwThreadId;
  95.  
  96.     hwBanner = NULL;
  97.  
  98.     hThread = CreateThread(0, 0, BannerThread, (LPVOID) hwndParent, 0, &dwThreadId);
  99.  
  100.     // wait for the window to initalize and for the stack operations to finish
  101.     while (hThread && !hwBanner)
  102.     {
  103.       Sleep(10);
  104.     }
  105.  
  106.     CloseHandle(hThread);
  107.  
  108.     ShowWindow(hwBanner, SW_SHOW);
  109.   }
  110. }
  111.  
  112. void __declspec(dllexport) destroy(HWND hwndParent, int string_size, char *variables, stack_t **stacktop)
  113. {
  114.   if (!hwBanner)
  115.     return;
  116.  
  117.   PostMessage(hwBanner, WM_CLOSE, 0, 0);
  118.  
  119.   if (!hwndParent)
  120.   {
  121.     // create a dummy window on the thread the NSIS window will be created on and set it
  122.     // as the foreground window so this thread will return to be the foreground window
  123.     HWND hwTemp;
  124.  
  125.     AttachThreadInput(GetWindowThreadProcessId(hwndParent, 0), GetCurrentThreadId(), TRUE);
  126.  
  127.     hwTemp = CreateWindowEx(
  128.       WS_EX_TOOLWINDOW,
  129.       "STATIC",
  130.       "",
  131.       WS_VISIBLE | WS_POPUP,
  132.       -1,
  133.       -1,
  134.       1,
  135.       1,
  136.       0,
  137.       0,
  138.       hInstance,
  139.       0
  140.     );
  141.     SetForegroundWindow(hwTemp);
  142.     DestroyWindow(hwTemp);
  143.  
  144.     AttachThreadInput(GetWindowThreadProcessId(hwndParent, 0), GetCurrentThreadId(), FALSE);
  145.   }
  146.  
  147.   // Wait for the thread to finish
  148.   while (hwBanner)
  149.     Sleep(25);
  150. }
  151.  
  152. BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
  153. {
  154.   hInstance = hInst;
  155.   if (hwBanner && ul_reason_for_call == DLL_PROCESS_DETACH)
  156.   {
  157.     destroy(0, 0, 0, 0);
  158.   }
  159.     return TRUE;
  160. }
  161.  
  162. unsigned int myatoi(char *s)
  163. {
  164.   unsigned int v=0;
  165.  
  166.   for (;;)
  167.   {
  168.     unsigned int c=*s++;
  169.     if (c >= '0' && c <= '9') c-='0';
  170.     else break;
  171.     v*=10;
  172.     v+=c;
  173.   }
  174.   return v;
  175. }