[KSPROXY]
[reactos.git] / reactos / dll / cpl / sysdm / virtmem.c
index fb1603b..6dcd9ad 100644 (file)
@@ -9,7 +9,7 @@
 
 #include "precomp.h"
 
-static BOOL OnSelChange(PVIRTMEM pVirtMem);
+static BOOL OnSelChange(HWND hwndDlg, PVIRTMEM pVirtMem);
 static LPCTSTR lpKey = _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
 
 static BOOL
@@ -20,22 +20,22 @@ ReadPageFileSettings(PVIRTMEM pVirtMem)
     DWORD dwDataSize;
     BOOL bRet = FALSE;
 
-    if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
-                      lpKey,
-                      0,
-                      NULL,
-                      REG_OPTION_NON_VOLATILE,
-                      KEY_QUERY_VALUE,
-                      NULL,
-                      &hkey,
-                      NULL) == ERROR_SUCCESS)
+    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
+                       lpKey,
+                       0,
+                       NULL,
+                       REG_OPTION_NON_VOLATILE,
+                       KEY_QUERY_VALUE,
+                       NULL,
+                       &hkey,
+                       NULL) == ERROR_SUCCESS)
     {
-        if(RegQueryValueEx(hkey,
-                           _T("PagingFiles"),
-                           NULL,
-                           &dwType,
-                           NULL,
-                           &dwDataSize) == ERROR_SUCCESS)
+        if (RegQueryValueEx(hkey,
+                            _T("PagingFiles"),
+                            NULL,
+                            &dwType,
+                            NULL,
+                            &dwDataSize) == ERROR_SUCCESS)
         {
             pVirtMem->szPagingFiles = (LPTSTR)HeapAlloc(GetProcessHeap(),
                                                         0,
@@ -44,12 +44,12 @@ ReadPageFileSettings(PVIRTMEM pVirtMem)
             {
                 ZeroMemory(pVirtMem->szPagingFiles,
                            dwDataSize);
-                if(RegQueryValueEx(hkey,
-                                   _T("PagingFiles"),
-                                   NULL,
-                                   &dwType,
-                                   (PBYTE)pVirtMem->szPagingFiles,
-                                   &dwDataSize) == ERROR_SUCCESS)
+                if (RegQueryValueEx(hkey,
+                                    _T("PagingFiles"),
+                                    NULL,
+                                    &dwType,
+                                    (PBYTE)pVirtMem->szPagingFiles,
+                                    &dwDataSize) == ERROR_SUCCESS)
                 {
                     bRet = TRUE;
                 }
@@ -67,21 +67,37 @@ ReadPageFileSettings(PVIRTMEM pVirtMem)
 }
 
 
-static INT
-GetPageFileSizes(LPTSTR lpPageFiles)
+static VOID
+GetPageFileSizes(LPTSTR lpPageFiles,
+                 LPINT lpInitialSize,
+                 LPINT lpMaximumSize)
 {
+    INT i = 0;
+
+    *lpInitialSize = -1;
+    *lpMaximumSize = -1;
+
     while (*lpPageFiles != _T('\0'))
     {
         if (*lpPageFiles == _T(' '))
         {
             lpPageFiles++;
-            return (INT)_ttoi(lpPageFiles);
+
+            switch (i)
+            {
+                case 0:
+                    *lpInitialSize = (INT)_ttoi(lpPageFiles);
+                    i = 1;
+                    break;
+
+                case 1:
+                    *lpMaximumSize = (INT)_ttoi(lpPageFiles);
+                    return;
+            }
         }
 
         lpPageFiles++;
     }
-
-    return -1;
 }
 
 
@@ -98,7 +114,6 @@ ParseMemSettings(PVIRTMEM pVirtMem)
     INT DriveLen;
     INT PgCnt = 0;
 
-    ZeroMemory(szDrives, sizeof(szDrives));
     DriveLen = GetLogicalDriveStrings(1023,
                                       szDrives);
 
@@ -118,14 +133,15 @@ ParseMemSettings(PVIRTMEM pVirtMem)
         /* copy the 'X:' portion */
         lstrcpyn(szDrive, DrivePtr, sizeof(szDrive) / sizeof(TCHAR));
 
-        if(GetDriveType(DrivePtr) == DRIVE_FIXED)
+        if (GetDriveType(DrivePtr) == DRIVE_FIXED)
         {
             /* does drive match the one in the registry ? */
-            if(!_tcsncmp(pVirtMem->szPagingFiles, szDrive, 2))
+            if (!_tcsncmp(pVirtMem->szPagingFiles, szDrive, 2))
             {
                 /* FIXME: we only check the first available pagefile in the reg */
-                InitialSize = GetPageFileSizes(pVirtMem->szPagingFiles);
-                MaxSize = GetPageFileSizes(pVirtMem->szPagingFiles);
+                GetPageFileSizes(pVirtMem->szPagingFiles,
+                                 &InitialSize,
+                                 &MaxSize);
 
                 pVirtMem->Pagefile[PgCnt].InitialValue = InitialSize;
                 pVirtMem->Pagefile[PgCnt].MaxValue = MaxSize;
@@ -180,7 +196,7 @@ ParseMemSettings(PVIRTMEM pVirtMem)
     SendMessage(pVirtMem->hListBox, LB_SETCURSEL, (WPARAM)0, (LPARAM)0);
     HeapFree(GetProcessHeap(), 0, szDisplayString);
     pVirtMem->Count = PgCnt;
-    OnSelChange(pVirtMem);
+    OnSelChange(pVirtMem->hSelf, pVirtMem);
 }
 
 
@@ -189,16 +205,15 @@ WritePageFileSettings(PVIRTMEM pVirtMem)
 {
     HKEY hk = NULL;
     TCHAR szPagingFiles[2048];
+    TCHAR szText[256];
     INT i;
     INT nPos = 0;
     BOOL bErr = TRUE;
 
-    for(i = 0; i < pVirtMem->Count; ++i)
+    for (i = 0; i < pVirtMem->Count; ++i)
     {
-        if(pVirtMem->Pagefile[i].bUsed)
+        if (pVirtMem->Pagefile[i].bUsed)
         {
-            TCHAR szText[256];
-
             _stprintf(szText, _T("%s\\pagefile.sys %i %i"),
                       pVirtMem->Pagefile[i].szDrive,
                       pVirtMem->Pagefile[i].InitialValue,
@@ -216,15 +231,15 @@ WritePageFileSettings(PVIRTMEM pVirtMem)
         }
     }
 
-    if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
-                      lpKey,
-                      0,
-                      NULL,
-                      REG_OPTION_NON_VOLATILE,
-                      KEY_WRITE,
-                      NULL,
-                      &hk,
-                      NULL) == ERROR_SUCCESS)
+    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
+                       lpKey,
+                       0,
+                       NULL,
+                       REG_OPTION_NON_VOLATILE,
+                       KEY_WRITE,
+                       NULL,
+                       &hk,
+                       NULL) == ERROR_SUCCESS)
     {
         if (RegSetValueEx(hk,
                           _T("PagingFiles"),
@@ -247,7 +262,8 @@ WritePageFileSettings(PVIRTMEM pVirtMem)
 static VOID
 SetListBoxColumns(HWND hwndListBox)
 {
-    const INT tabs[2] = {30, 170};
+    const INT tabs[2] = {30, 120};
+
     SendMessage(hwndListBox, LB_SETTABSTOPS, (WPARAM)2, (LPARAM)&tabs[0]);
 }
 
@@ -283,6 +299,8 @@ static VOID
 OnSet(PVIRTMEM pVirtMem)
 {
     INT Index;
+    UINT Value;
+    BOOL bTranslated;
 
     pVirtMem->bSave = TRUE;
 
@@ -291,31 +309,47 @@ OnSet(PVIRTMEM pVirtMem)
                                      LB_GETCURSEL,
                                      0,
                                      0);
-
-    if(Index < pVirtMem->Count)
+    if (Index < pVirtMem->Count)
     {
-        TCHAR szText[255];
-
         /* check if custom settings are checked */
-        if(SendDlgItemMessage(pVirtMem->hSelf,
-                              IDC_CUSTOM,
-                              BM_GETCHECK,
-                              0,
-                              0) == BST_CHECKED)
+        if (IsDlgButtonChecked(pVirtMem->hSelf,
+                               IDC_CUSTOM) == BST_CHECKED)
         {
-            SendDlgItemMessage(pVirtMem->hSelf,
-                               IDC_INITIALSIZE,
-                               WM_GETTEXT,
-                               254,
-                               (LPARAM)szText);
-            pVirtMem->Pagefile[Index].InitialValue = _ttoi(szText);
-
-            SendDlgItemMessage(pVirtMem->hSelf,
-                               IDC_MAXSIZE,
-                               WM_GETTEXT,
-                               254,
-                               (LPARAM)szText);
-            pVirtMem->Pagefile[Index].MaxValue = _ttoi(szText);
+            Value = GetDlgItemInt(pVirtMem->hSelf,
+                                  IDC_INITIALSIZE,
+                                  &bTranslated,
+                                  FALSE);
+            if (!bTranslated)
+            {
+                /* FIXME: Show error message instead of setting the edit
+                          field to the previous value */
+                SetDlgItemInt(pVirtMem->hSelf,
+                              IDC_INITIALSIZE,
+                              pVirtMem->Pagefile[Index].InitialValue,
+                              FALSE);
+            }
+            else
+            {
+                pVirtMem->Pagefile[Index].InitialValue = Value;
+            }
+
+            Value = GetDlgItemInt(pVirtMem->hSelf,
+                                  IDC_MAXSIZE,
+                                  &bTranslated,
+                                  FALSE);
+            if (!bTranslated)
+            {
+                /* FIXME: Show error message instead of setting the edit
+                          field to the previous value */
+                SetDlgItemInt(pVirtMem->hSelf,
+                              IDC_MAXSIZE,
+                              pVirtMem->Pagefile[Index].MaxValue,
+                              FALSE);
+            }
+            else
+            {
+                pVirtMem->Pagefile[Index].MaxValue = Value;
+            }
         }
         else
         {
@@ -323,11 +357,8 @@ OnSet(PVIRTMEM pVirtMem)
             pVirtMem->Pagefile[Index].InitialValue = pVirtMem->Pagefile[Index].MaxValue = 0;
 
             // check to see if this drive is used for a paging file
-            if (SendDlgItemMessage(pVirtMem->hSelf,
-                                   IDC_NOPAGEFILE,
-                                   BM_GETCHECK,
-                                   0,
-                                   0) == BST_UNCHECKED)
+            if (IsDlgButtonChecked(pVirtMem->hSelf,
+                                   IDC_NOPAGEFILE) == BST_UNCHECKED)
             {
                 pVirtMem->Pagefile[Index].bUsed = TRUE;
             }
@@ -341,46 +372,55 @@ OnSet(PVIRTMEM pVirtMem)
 
 
 static BOOL
-OnSelChange(PVIRTMEM pVirtMem)
+OnSelChange(HWND hwndDlg, PVIRTMEM pVirtMem)
 {
-    TCHAR szCustVals[255];
+    TCHAR szBuffer[64];
+    MEMORYSTATUSEX MemoryStatus;
+    ULARGE_INTEGER FreeBytes;
+    DWORDLONG FreeMemory;
     INT Index;
+    INT i;
+    INT FileSize;
 
-    Index = (INT)SendDlgItemMessage(pVirtMem->hSelf,
+    Index = (INT)SendDlgItemMessage(hwndDlg,
                                     IDC_PAGEFILELIST,
                                     LB_GETCURSEL,
                                     0,
                                     0);
-
-    if(Index < pVirtMem->Count)
+    if (Index < pVirtMem->Count)
     {
+        /* Set drive letter */
+        SetDlgItemText(hwndDlg, IDC_DRIVE,
+                       pVirtMem->Pagefile[Index].szDrive);
+
+        /* Set available disk space */
+        if (GetDiskFreeSpaceEx(pVirtMem->Pagefile[Index].szDrive,
+                               NULL, NULL, &FreeBytes))
+        {
+            _stprintf(szBuffer, _T("%I64u MB"), FreeBytes.QuadPart / (1024 * 1024));
+            SetDlgItemText(hwndDlg, IDC_SPACEAVAIL, szBuffer);
+        }
 
-        if(pVirtMem->Pagefile[Index].InitialValue  != 0 &&
-           pVirtMem->Pagefile[Index].MaxValue != 0)
+        if (pVirtMem->Pagefile[Index].InitialValue  != 0 &&
+            pVirtMem->Pagefile[Index].MaxValue != 0)
         {
             /* enable and fill the custom values */
             EnableWindow(GetDlgItem(pVirtMem->hSelf, IDC_MAXSIZE), TRUE);
             EnableWindow(GetDlgItem(pVirtMem->hSelf, IDC_INITIALSIZE), TRUE);
 
-            _itot(pVirtMem->Pagefile[Index].InitialValue , szCustVals, 10);
-            SendDlgItemMessage(pVirtMem->hSelf,
-                               IDC_INITIALSIZE,
-                               WM_SETTEXT,
-                               0,
-                               (LPARAM)szCustVals);
-
-            _itot(pVirtMem->Pagefile[Index].MaxValue, szCustVals, 10);
-            SendDlgItemMessage(pVirtMem->hSelf,
-                               IDC_MAXSIZE,
-                               WM_SETTEXT,
-                               0,
-                               (LPARAM)szCustVals);
-
-            SendDlgItemMessage(pVirtMem->hSelf,
-                               IDC_CUSTOM,
-                               BM_SETCHECK,
-                               1,
-                               0);
+            SetDlgItemInt(pVirtMem->hSelf,
+                          IDC_INITIALSIZE,
+                          pVirtMem->Pagefile[Index].InitialValue,
+                          FALSE);
+
+            SetDlgItemInt(pVirtMem->hSelf,
+                          IDC_MAXSIZE,
+                          pVirtMem->Pagefile[Index].MaxValue,
+                          FALSE);
+
+            CheckDlgButton(pVirtMem->hSelf,
+                           IDC_CUSTOM,
+                           BST_CHECKED);
         }
         else
         {
@@ -389,23 +429,40 @@ OnSelChange(PVIRTMEM pVirtMem)
             EnableWindow(GetDlgItem(pVirtMem->hSelf, IDC_INITIALSIZE), FALSE);
 
             /* is it system managed */
-            if(pVirtMem->Pagefile[Index].bUsed)
+            if (pVirtMem->Pagefile[Index].bUsed)
             {
-                SendDlgItemMessage(pVirtMem->hSelf,
-                                   IDC_SYSMANSIZE,
-                                   BM_SETCHECK,
-                                   1,
-                                   0);
+                CheckDlgButton(pVirtMem->hSelf,
+                               IDC_SYSMANSIZE,
+                               BST_CHECKED);
             }
             else
             {
-                SendDlgItemMessage(pVirtMem->hSelf,
-                                   IDC_NOPAGEFILE,
-                                   BM_SETCHECK,
-                                   1,
-                                   0);
+                CheckDlgButton(pVirtMem->hSelf,
+                               IDC_NOPAGEFILE,
+                               BST_CHECKED);
             }
         }
+
+        /* Set minimum pagefile size */
+        SetDlgItemText(hwndDlg, IDC_MINIMUM, _T("2 MB"));
+
+        /* Set recommended pagefile size */
+        MemoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
+        if (GlobalMemoryStatusEx(&MemoryStatus))
+        {
+            FreeMemory = MemoryStatus.ullTotalPhys / (1024 * 1024);
+            _stprintf(szBuffer, _T("%I64u MB"), FreeMemory + (FreeMemory / 2));
+            SetDlgItemText(hwndDlg, IDC_RECOMMENDED, szBuffer);
+        }
+
+        /* Set current pagefile size */
+        FileSize = 0;
+        for (i = 0; i < 26; i++)
+        {
+            FileSize += pVirtMem->Pagefile[i].InitialValue;
+        }
+        _stprintf(szBuffer, _T("%u MB"), FileSize);
+        SetDlgItemText(hwndDlg, IDC_CURRENT, szBuffer);
     }
 
     return TRUE;
@@ -415,47 +472,16 @@ OnSelChange(PVIRTMEM pVirtMem)
 static VOID
 OnOk(PVIRTMEM pVirtMem)
 {
-    if(pVirtMem->bSave == TRUE)
+    if (pVirtMem->bSave == TRUE)
     {
         WritePageFileSettings(pVirtMem);
     }
-
-    if (pVirtMem->szPagingFiles)
-        HeapFree(GetProcessHeap(),
-                 0,
-                 pVirtMem->szPagingFiles);
-
-    HeapFree(GetProcessHeap(),
-             0,
-             pVirtMem);
 }
 
 
 static VOID
-OnCancel(PVIRTMEM pVirtMem)
-{
-    if (pVirtMem->szPagingFiles)
-        HeapFree(GetProcessHeap(),
-                 0,
-                 pVirtMem->szPagingFiles);
-
-    HeapFree(GetProcessHeap(),
-             0,
-             pVirtMem);
-}
-
-
-static PVIRTMEM
-OnInitDialog(HWND hwnd)
+OnInitDialog(HWND hwnd, PVIRTMEM pVirtMem)
 {
-    PVIRTMEM pVirtMem = (PVIRTMEM)HeapAlloc(GetProcessHeap(),
-                                            HEAP_ZERO_MEMORY,
-                                            sizeof(VIRTMEM));
-    if (pVirtMem == NULL)
-    {
-        EndDialog(hwnd, 0);
-    }
-
     pVirtMem->hSelf = hwnd;
     pVirtMem->hListBox = GetDlgItem(hwnd, IDC_PAGEFILELIST);
     pVirtMem->bSave = FALSE;
@@ -468,8 +494,6 @@ OnInitDialog(HWND hwnd)
         /* Parse our settings and set up dialog */
         ParseMemSettings(pVirtMem);
     }
-
-    return pVirtMem;
 }
 
 
@@ -479,23 +503,38 @@ VirtMemDlgProc(HWND hwndDlg,
                WPARAM wParam,
                LPARAM lParam)
 {
-    /* there can only be one instance of this dialog */
-    static PVIRTMEM pVirtMem = NULL;
+    PVIRTMEM pVirtMem;
 
     UNREFERENCED_PARAMETER(lParam);
 
+    pVirtMem = (PVIRTMEM)GetWindowLongPtr(hwndDlg, DWLP_USER);
+
     switch (uMsg)
     {
         case WM_INITDIALOG:
-            pVirtMem = OnInitDialog(hwndDlg);
+            pVirtMem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(VIRTMEM));
+            if (pVirtMem == NULL)
+            {
+                EndDialog(hwndDlg, 0);
+                return FALSE;
+            }
+
+            SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pVirtMem);
+
+            OnInitDialog(hwndDlg, pVirtMem);
+            break;
+
+        case WM_DESTROY:
+            if (pVirtMem->szPagingFiles)
+                HeapFree(GetProcessHeap(), 0,
+                         pVirtMem->szPagingFiles);
+            HeapFree(GetProcessHeap(), 0, pVirtMem);
             break;
 
         case WM_COMMAND:
-        {
             switch (LOWORD(wParam))
             {
                 case IDCANCEL:
-                    OnCancel(pVirtMem);
                     EndDialog(hwndDlg, 0);
                     return TRUE;
 
@@ -524,13 +563,12 @@ VirtMemDlgProc(HWND hwndDlg,
                     switch HIWORD(wParam)
                     {
                         case LBN_SELCHANGE:
-                            OnSelChange(pVirtMem);
+                            OnSelChange(hwndDlg, pVirtMem);
                             return TRUE;
                     }
                     break;
             }
-        }
-        break;
+            break;
     }
 
     return FALSE;