2 * PROJECT: ReactOS system properties, control panel applet
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/sysdm/virtual.c
5 * PURPOSE: Virtual memory control dialog
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
12 static BOOL
OnSelChange(PVIRTMEM pVirtMem
);
13 static LPCTSTR lpKey
= _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
16 ReadPageFileSettings(PVIRTMEM pVirtMem
)
23 if (RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
27 REG_OPTION_NON_VOLATILE
,
31 NULL
) == ERROR_SUCCESS
)
33 if (RegQueryValueEx(hkey
,
38 &dwDataSize
) == ERROR_SUCCESS
)
40 pVirtMem
->szPagingFiles
= (LPTSTR
)HeapAlloc(GetProcessHeap(),
43 if (pVirtMem
->szPagingFiles
!= NULL
)
45 ZeroMemory(pVirtMem
->szPagingFiles
,
47 if (RegQueryValueEx(hkey
,
51 (PBYTE
)pVirtMem
->szPagingFiles
,
52 &dwDataSize
) == ERROR_SUCCESS
)
61 ShowLastWin32Error(pVirtMem
->hSelf
);
71 GetPageFileSizes(LPTSTR lpPageFiles
)
73 while (*lpPageFiles
!= _T('\0'))
75 if (*lpPageFiles
== _T(' '))
78 return (INT
)_ttoi(lpPageFiles
);
89 ParseMemSettings(PVIRTMEM pVirtMem
)
91 TCHAR szDrives
[1024]; // all drives
92 LPTSTR DrivePtr
= szDrives
;
93 TCHAR szDrive
[3]; // single drive
94 TCHAR szVolume
[MAX_PATH
];
95 TCHAR
*szDisplayString
;
101 DriveLen
= GetLogicalDriveStrings(1023,
104 szDisplayString
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (MAX_PATH
* 2 + 69) * sizeof(TCHAR
));
105 if (szDisplayString
== NULL
)
108 while (DriveLen
!= 0)
112 Len
= lstrlen(DrivePtr
) + 1;
115 DrivePtr
= _tcsupr(DrivePtr
);
117 /* copy the 'X:' portion */
118 lstrcpyn(szDrive
, DrivePtr
, sizeof(szDrive
) / sizeof(TCHAR
));
120 if (GetDriveType(DrivePtr
) == DRIVE_FIXED
)
122 /* does drive match the one in the registry ? */
123 if (!_tcsncmp(pVirtMem
->szPagingFiles
, szDrive
, 2))
125 /* FIXME: we only check the first available pagefile in the reg */
126 InitialSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
127 MaxSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
129 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= InitialSize
;
130 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= MaxSize
;
131 pVirtMem
->Pagefile
[PgCnt
].bUsed
= TRUE
;
132 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
136 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= 0;
137 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= 0;
138 pVirtMem
->Pagefile
[PgCnt
].bUsed
= FALSE
;
139 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
142 _tcscpy(szDisplayString
, szDrive
);
143 _tcscat(szDisplayString
, _T("\t"));
145 /* set a volume label if there is one */
146 if (GetVolumeInformation(DrivePtr
,
155 if (szVolume
[0] != _T('\0'))
157 TCHAR szVol
[MAX_PATH
+ 2];
158 _stprintf(szVol
, _T("[%s]"), szVolume
);
159 _tcscat(szDisplayString
, szVol
);
163 if ((InitialSize
!= 0) || (MaxSize
!= 0))
167 _stprintf(szSize
, _T("%i - %i"), InitialSize
, MaxSize
);
168 _tcscat(szDisplayString
, _T("\t"));
169 _tcscat(szDisplayString
, szSize
);
172 SendMessage(pVirtMem
->hListBox
, LB_ADDSTRING
, (WPARAM
)0, (LPARAM
)szDisplayString
);
179 SendMessage(pVirtMem
->hListBox
, LB_SETCURSEL
, (WPARAM
)0, (LPARAM
)0);
180 HeapFree(GetProcessHeap(), 0, szDisplayString
);
181 pVirtMem
->Count
= PgCnt
;
182 OnSelChange(pVirtMem
);
187 WritePageFileSettings(PVIRTMEM pVirtMem
)
190 TCHAR szPagingFiles
[2048];
196 for (i
= 0; i
< pVirtMem
->Count
; ++i
)
198 if (pVirtMem
->Pagefile
[i
].bUsed
)
200 _stprintf(szText
, _T("%s\\pagefile.sys %i %i"),
201 pVirtMem
->Pagefile
[i
].szDrive
,
202 pVirtMem
->Pagefile
[i
].InitialValue
,
203 pVirtMem
->Pagefile
[i
].MaxValue
);
205 /* Add it to our overall registry string */
206 lstrcat(szPagingFiles
+ nPos
, szText
);
208 /* Record the position where the next string will start */
209 nPos
+= (INT
)lstrlen(szText
) + 1;
211 /* add another NULL for REG_MULTI_SZ */
212 szPagingFiles
[nPos
] = _T('\0');
217 if (RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
221 REG_OPTION_NON_VOLATILE
,
225 NULL
) == ERROR_SUCCESS
)
227 if (RegSetValueEx(hk
,
231 (LPBYTE
) szPagingFiles
,
232 (DWORD
) nPos
* sizeof(TCHAR
)) == ERROR_SUCCESS
)
241 ShowLastWin32Error(pVirtMem
->hSelf
);
246 SetListBoxColumns(HWND hwndListBox
)
248 const INT tabs
[2] = {30, 170};
250 SendMessage(hwndListBox
, LB_SETTABSTOPS
, (WPARAM
)2, (LPARAM
)&tabs
[0]);
255 OnNoPagingFile(PVIRTMEM pVirtMem
)
257 /* Disable the page file custom size boxes */
258 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
259 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
264 OnSysManSize(PVIRTMEM pVirtMem
)
266 /* Disable the page file custom size boxes */
267 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
268 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
273 OnCustom(PVIRTMEM pVirtMem
)
275 /* Enable the page file custom size boxes */
276 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
277 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
282 OnSet(PVIRTMEM pVirtMem
)
288 pVirtMem
->bSave
= TRUE
;
290 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
295 if (Index
< pVirtMem
->Count
)
297 /* check if custom settings are checked */
298 if (IsDlgButtonChecked(pVirtMem
->hSelf
,
299 IDC_CUSTOM
) == BST_CHECKED
)
301 Value
= GetDlgItemInt(pVirtMem
->hSelf
,
307 /* FIXME: Show error message instead of setting the edit
308 field to the previous value */
309 SetDlgItemInt(pVirtMem
->hSelf
,
311 pVirtMem
->Pagefile
[Index
].InitialValue
,
316 pVirtMem
->Pagefile
[Index
].InitialValue
= Value
;
319 Value
= GetDlgItemInt(pVirtMem
->hSelf
,
325 /* FIXME: Show error message instead of setting the edit
326 field to the previous value */
327 SetDlgItemInt(pVirtMem
->hSelf
,
329 pVirtMem
->Pagefile
[Index
].MaxValue
,
334 pVirtMem
->Pagefile
[Index
].MaxValue
= Value
;
340 pVirtMem
->Pagefile
[Index
].InitialValue
= pVirtMem
->Pagefile
[Index
].MaxValue
= 0;
342 // check to see if this drive is used for a paging file
343 if (IsDlgButtonChecked(pVirtMem
->hSelf
,
344 IDC_NOPAGEFILE
) == BST_UNCHECKED
)
346 pVirtMem
->Pagefile
[Index
].bUsed
= TRUE
;
350 pVirtMem
->Pagefile
[Index
].bUsed
= FALSE
;
358 OnSelChange(PVIRTMEM pVirtMem
)
362 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
368 if (Index
< pVirtMem
->Count
)
371 if (pVirtMem
->Pagefile
[Index
].InitialValue
!= 0 &&
372 pVirtMem
->Pagefile
[Index
].MaxValue
!= 0)
374 /* enable and fill the custom values */
375 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
376 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
378 SetDlgItemInt(pVirtMem
->hSelf
,
380 pVirtMem
->Pagefile
[Index
].InitialValue
,
383 SetDlgItemInt(pVirtMem
->hSelf
,
385 pVirtMem
->Pagefile
[Index
].MaxValue
,
388 CheckDlgButton(pVirtMem
->hSelf
,
394 /* It's not a custom value */
395 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
396 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
398 /* is it system managed */
399 if (pVirtMem
->Pagefile
[Index
].bUsed
)
401 CheckDlgButton(pVirtMem
->hSelf
,
407 CheckDlgButton(pVirtMem
->hSelf
,
419 OnOk(PVIRTMEM pVirtMem
)
421 if (pVirtMem
->bSave
== TRUE
)
423 WritePageFileSettings(pVirtMem
);
426 if (pVirtMem
->szPagingFiles
)
427 HeapFree(GetProcessHeap(),
429 pVirtMem
->szPagingFiles
);
431 HeapFree(GetProcessHeap(),
438 OnCancel(PVIRTMEM pVirtMem
)
440 if (pVirtMem
->szPagingFiles
)
441 HeapFree(GetProcessHeap(),
443 pVirtMem
->szPagingFiles
);
445 HeapFree(GetProcessHeap(),
452 OnInitDialog(HWND hwnd
)
454 PVIRTMEM pVirtMem
= (PVIRTMEM
)HeapAlloc(GetProcessHeap(),
457 if (pVirtMem
== NULL
)
462 pVirtMem
->hSelf
= hwnd
;
463 pVirtMem
->hListBox
= GetDlgItem(hwnd
, IDC_PAGEFILELIST
);
464 pVirtMem
->bSave
= FALSE
;
466 SetListBoxColumns(pVirtMem
->hListBox
);
468 /* Load the pagefile systems from the reg */
469 if (ReadPageFileSettings(pVirtMem
))
471 /* Parse our settings and set up dialog */
472 ParseMemSettings(pVirtMem
);
480 VirtMemDlgProc(HWND hwndDlg
,
485 /* there can only be one instance of this dialog */
486 static PVIRTMEM pVirtMem
= NULL
;
488 UNREFERENCED_PARAMETER(lParam
);
493 pVirtMem
= OnInitDialog(hwndDlg
);
497 switch (LOWORD(wParam
))
501 EndDialog(hwndDlg
, 0);
506 EndDialog(hwndDlg
, 0);
510 OnNoPagingFile(pVirtMem
);
514 OnSysManSize(pVirtMem
);
525 case IDC_PAGEFILELIST
:
526 switch HIWORD(wParam
)
529 OnSelChange(pVirtMem
);