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 ZeroMemory(&szDrives
, sizeof(szDrives
) * sizeof(TCHAR
));
102 DriveLen
= GetLogicalDriveStrings(1023,
105 szDisplayString
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (MAX_PATH
* 2 + 69) * sizeof(TCHAR
));
106 if (szDisplayString
== NULL
)
109 while (DriveLen
!= 0)
113 Len
= lstrlen(DrivePtr
) + 1;
116 DrivePtr
= _tcsupr(DrivePtr
);
118 /* copy the 'X:' portion */
119 lstrcpyn(szDrive
, DrivePtr
, 3);
121 if(GetDriveType(DrivePtr
) == DRIVE_FIXED
)
123 /* does drive match the one in the registry ? */
124 if(!_tcsncmp(pVirtMem
->szPagingFiles
, szDrive
, 2))
126 /* FIXME: we only check the first available pagefile in the reg */
127 InitialSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
128 MaxSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
130 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= InitialSize
;
131 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= MaxSize
;
132 pVirtMem
->Pagefile
[PgCnt
].bUsed
= TRUE
;
133 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
137 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= 0;
138 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= 0;
139 pVirtMem
->Pagefile
[PgCnt
].bUsed
= FALSE
;
140 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
143 _tcscpy(szDisplayString
, szDrive
);
144 _tcscat(szDisplayString
, _T("\t"));
146 /* set a volume label if there is one */
147 if (GetVolumeInformation(DrivePtr
,
156 if (szVolume
[0] != _T('\0'))
158 TCHAR szVol
[MAX_PATH
+ 2];
159 _stprintf(szVol
, _T("[%s]"), szVolume
);
160 _tcscat(szDisplayString
, szVol
);
164 if ((InitialSize
!= 0) || (MaxSize
!= 0))
168 _stprintf(szSize
, _T("%i - %i"), InitialSize
, MaxSize
);
169 _tcscat(szDisplayString
, _T("\t"));
170 _tcscat(szDisplayString
, szSize
);
173 SendMessage(pVirtMem
->hListBox
, LB_ADDSTRING
, (WPARAM
)0, (LPARAM
)szDisplayString
);
180 SendMessage(pVirtMem
->hListBox
, LB_SETCURSEL
, (WPARAM
)0, (LPARAM
)0);
181 HeapFree(GetProcessHeap(), 0, szDisplayString
);
182 pVirtMem
->Count
= PgCnt
;
183 OnSelChange(pVirtMem
);
188 WritePageFileSettings(PVIRTMEM pVirtMem
)
191 TCHAR szPagingFiles
[2048];
196 for(i
= 0; i
< pVirtMem
->Count
; ++i
)
198 if(pVirtMem
->Pagefile
[i
].bUsed
)
202 _stprintf(szText
, _T("%s\\pagefile.sys %i %i"),
203 pVirtMem
->Pagefile
[i
].szDrive
,
204 pVirtMem
->Pagefile
[i
].InitialValue
,
205 pVirtMem
->Pagefile
[i
].MaxValue
);
207 /* Add it to our overall registry string */
208 lstrcat(szPagingFiles
+ nPos
, szText
);
210 /* Record the position where the next string will start */
211 nPos
+= (INT
)lstrlen(szText
) + 1;
213 /* add another NULL for REG_MULTI_SZ */
214 szPagingFiles
[nPos
] = _T('\0');
219 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
223 REG_OPTION_NON_VOLATILE
,
227 NULL
) == ERROR_SUCCESS
)
229 if (RegSetValueEx(hk
,
233 (LPBYTE
) szPagingFiles
,
234 (DWORD
) nPos
* sizeof(TCHAR
)) == ERROR_SUCCESS
)
243 ShowLastWin32Error(pVirtMem
->hSelf
);
248 SetListBoxColumns(HWND hwndListBox
)
250 INT tabs
[2] = {30, 170};
251 SendMessage(hwndListBox
, LB_SETTABSTOPS
, (WPARAM
)2, (LPARAM
)(LPINT
) &tabs
[0]);
256 OnNoPagingFile(PVIRTMEM pVirtMem
)
258 /* Disable the page file custom size boxes */
259 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
260 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
265 OnSysManSize(PVIRTMEM pVirtMem
)
267 /* Disable the page file custom size boxes */
268 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
269 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
274 OnCustom(PVIRTMEM pVirtMem
)
276 /* Enable the page file custom size boxes */
277 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
278 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
283 OnSet(PVIRTMEM pVirtMem
)
287 pVirtMem
->bSave
= TRUE
;
289 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
295 if(Index
< pVirtMem
->Count
)
299 /* check if custom settings are checked */
300 if(SendDlgItemMessage(pVirtMem
->hSelf
,
306 SendDlgItemMessage(pVirtMem
->hSelf
,
311 pVirtMem
->Pagefile
[Index
].InitialValue
= _ttoi(szText
);
313 SendDlgItemMessage(pVirtMem
->hSelf
,
318 pVirtMem
->Pagefile
[Index
].MaxValue
= _ttoi(szText
);
323 pVirtMem
->Pagefile
[Index
].InitialValue
= pVirtMem
->Pagefile
[Index
].MaxValue
= 0;
325 // check to see if this drive is used for a paging file
326 if (SendDlgItemMessage(pVirtMem
->hSelf
,
332 pVirtMem
->Pagefile
[Index
].bUsed
= TRUE
;
336 pVirtMem
->Pagefile
[Index
].bUsed
= FALSE
;
344 OnSelChange(PVIRTMEM pVirtMem
)
346 TCHAR szCustVals
[255];
349 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
355 if(Index
< pVirtMem
->Count
)
358 if(pVirtMem
->Pagefile
[Index
].InitialValue
!= 0 &&
359 pVirtMem
->Pagefile
[Index
].MaxValue
!= 0)
361 /* enable and fill the custom values */
362 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
363 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
365 _itot(pVirtMem
->Pagefile
[Index
].InitialValue
, szCustVals
, 10);
366 SendDlgItemMessage(pVirtMem
->hSelf
,
372 _itot(pVirtMem
->Pagefile
[Index
].MaxValue
, szCustVals
, 10);
373 SendDlgItemMessage(pVirtMem
->hSelf
,
379 SendDlgItemMessage(pVirtMem
->hSelf
,
387 /* It's not a custom value */
388 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
389 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
391 /* is it system managed */
392 if(pVirtMem
->Pagefile
[Index
].bUsed
)
394 SendDlgItemMessage(pVirtMem
->hSelf
,
402 SendDlgItemMessage(pVirtMem
->hSelf
,
416 OnOk(PVIRTMEM pVirtMem
)
418 if(pVirtMem
->bSave
== TRUE
)
420 WritePageFileSettings(pVirtMem
);
423 if (pVirtMem
->szPagingFiles
)
424 HeapFree(GetProcessHeap(),
426 pVirtMem
->szPagingFiles
);
428 HeapFree(GetProcessHeap(),
435 OnCancel(PVIRTMEM pVirtMem
)
437 if (pVirtMem
->szPagingFiles
)
438 HeapFree(GetProcessHeap(),
440 pVirtMem
->szPagingFiles
);
442 HeapFree(GetProcessHeap(),
449 OnInitDialog(HWND hwnd
)
451 PVIRTMEM pVirtMem
= (PVIRTMEM
)HeapAlloc(GetProcessHeap(),
454 if (pVirtMem
== NULL
)
459 pVirtMem
->hSelf
= hwnd
;
460 pVirtMem
->hListBox
= GetDlgItem(hwnd
, IDC_PAGEFILELIST
);
461 pVirtMem
->bSave
= FALSE
;
463 SetListBoxColumns(pVirtMem
->hListBox
);
465 /* Load the pagefile systems from the reg */
466 if (ReadPageFileSettings(pVirtMem
))
468 /* Parse our settings and set up dialog */
469 ParseMemSettings(pVirtMem
);
477 VirtMemDlgProc(HWND hwndDlg
,
482 /* there can only be one instance of this dialog */
483 static PVIRTMEM pVirtMem
= NULL
;
485 UNREFERENCED_PARAMETER(lParam
);
490 pVirtMem
= OnInitDialog(hwndDlg
);
495 switch (LOWORD(wParam
))
499 EndDialog(hwndDlg
, 0);
504 EndDialog(hwndDlg
, 0);
508 OnNoPagingFile(pVirtMem
);
512 OnSysManSize(pVirtMem
);
523 case IDC_PAGEFILELIST
:
524 switch HIWORD(wParam
)
527 OnSelChange(pVirtMem
);