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 LPCTSTR lpKey
= _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management");
15 ReadPageFileSettings(PVIRTMEM pVirtMem
)
22 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
26 REG_OPTION_NON_VOLATILE
,
30 NULL
) == ERROR_SUCCESS
)
32 if(RegQueryValueEx(hkey
,
37 &dwDataSize
) == ERROR_SUCCESS
)
39 pVirtMem
->szPagingFiles
= (LPTSTR
)HeapAlloc(GetProcessHeap(),
42 if (pVirtMem
->szPagingFiles
!= NULL
)
44 ZeroMemory(pVirtMem
->szPagingFiles
,
46 if(RegQueryValueEx(hkey
,
50 (PBYTE
)pVirtMem
->szPagingFiles
,
51 &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
[MAX_PATH
]; // single drive
94 TCHAR szVolume
[MAX_PATH
];
100 DriveLen
= GetLogicalDriveStrings(1023,
103 while (DriveLen
!= 0)
108 Len
= lstrlen(DrivePtr
) + 1;
111 DrivePtr
= _tcsupr(DrivePtr
);
113 /* copy the 'X:' portion */
114 lstrcpyn(szDrive
, DrivePtr
, 3);
116 if(GetDriveType(DrivePtr
) == DRIVE_FIXED
)
118 /* does drive match the one in the registry ? */
119 if(!_tcsncmp(pVirtMem
->szPagingFiles
, szDrive
, 2))
121 /* FIXME: we only check the first available pagefile in the reg */
122 InitialSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
123 MaxSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
125 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= InitialSize
;
126 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= MaxSize
;
127 pVirtMem
->Pagefile
[PgCnt
].bUsed
= TRUE
;
128 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
132 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= 0;
133 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= 0;
134 pVirtMem
->Pagefile
[PgCnt
].bUsed
= FALSE
;
135 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
138 /* fill out the listview */
139 ZeroMemory(&Item
, sizeof(Item
));
140 Item
.mask
= LVIF_TEXT
;
141 Item
.iItem
= ListView_GetItemCount(pVirtMem
->hListView
);
142 Item
.pszText
= szDrive
;
143 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
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];
159 _stprintf(szVol
, _T("[%s]"), szVolume
);
162 Item
.pszText
= szVol
;
163 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
167 if ((InitialSize
!= 0) || (MaxSize
!= 0))
171 _stprintf(szSize
, _T("%i - %i"), InitialSize
, MaxSize
);
174 Item
.pszText
= szSize
;
175 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
184 pVirtMem
->Count
= PgCnt
;
189 WritePageFileSettings(PVIRTMEM pVirtMem
)
192 TCHAR szPagingFiles
[2048];
197 for(i
= 0; i
< pVirtMem
->Count
; ++i
)
199 if(pVirtMem
->Pagefile
[i
].bUsed
)
203 _stprintf(szText
, _T("%s\\pagefile.sys %i %i"),
204 pVirtMem
->Pagefile
[i
].szDrive
,
205 pVirtMem
->Pagefile
[i
].InitialValue
,
206 pVirtMem
->Pagefile
[i
].MaxValue
);
208 /* Add it to our overall registry string */
209 lstrcat(szPagingFiles
+ nPos
, szText
);
211 /* Record the position where the next string will start */
212 nPos
+= (INT
)lstrlen(szText
) + 1;
214 /* add another NULL for REG_MULTI_SZ */
215 szPagingFiles
[nPos
] = _T('\0');
220 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
224 REG_OPTION_NON_VOLATILE
,
228 NULL
) == ERROR_SUCCESS
)
230 if (RegSetValueEx(hk
,
234 (LPBYTE
) szPagingFiles
,
235 (DWORD
) nPos
* sizeof(TCHAR
)))
244 ShowLastWin32Error(pVirtMem
->hSelf
);
249 SetListViewColumns(HWND hwndListView
)
254 GetClientRect(hwndListView
, &rect
);
256 (void)ListView_SetExtendedListViewStyle(hwndListView
,
257 LVS_EX_FULLROWSELECT
);
259 ZeroMemory(&lvc
, sizeof(lvc
));
260 lvc
.mask
= LVCF_SUBITEM
| LVCF_WIDTH
| LVCF_FMT
;
261 lvc
.fmt
= LVCFMT_LEFT
;
263 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.1);
265 (void)ListView_InsertColumn(hwndListView
, 0, &lvc
);
267 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.3);
269 (void)ListView_InsertColumn(hwndListView
, 1, &lvc
);
271 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.6);
273 (void)ListView_InsertColumn(hwndListView
, 2, &lvc
);
278 OnNoPagingFile(PVIRTMEM pVirtMem
)
280 /* Disable the page file custom size boxes */
281 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
282 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
287 OnSysManSize(PVIRTMEM pVirtMem
)
289 /* Disable the page file custom size boxes */
290 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
291 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
296 OnCustom(PVIRTMEM pVirtMem
)
298 /* Enable the page file custom size boxes */
299 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
300 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
305 OnSet(PVIRTMEM pVirtMem
)
309 pVirtMem
->bSave
= TRUE
;
311 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
317 if(Index
< pVirtMem
->Count
)
321 /* check if custom settings are checked */
322 if(SendDlgItemMessage(pVirtMem
->hSelf
,
328 SendDlgItemMessage(pVirtMem
->hSelf
,
333 pVirtMem
->Pagefile
[Index
].InitialValue
= _ttoi(szText
);
335 SendDlgItemMessage(pVirtMem
->hSelf
,
340 pVirtMem
->Pagefile
[Index
].MaxValue
= _ttoi(szText
);
345 pVirtMem
->Pagefile
[Index
].InitialValue
= pVirtMem
->Pagefile
[Index
].MaxValue
= 0;
347 // check to see if this drive is used for a paging file
348 if (SendDlgItemMessage(pVirtMem
->hSelf
,
354 pVirtMem
->Pagefile
[Index
].bUsed
= TRUE
;
358 pVirtMem
->Pagefile
[Index
].bUsed
= FALSE
;
366 OnSelChange(PVIRTMEM pVirtMem
,
369 TCHAR szCustVals
[255];
372 UNREFERENCED_PARAMETER(pnmv
);
374 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
380 if(Index
< pVirtMem
->Count
)
383 if(pVirtMem
->Pagefile
[Index
].InitialValue
!= 0 &&
384 pVirtMem
->Pagefile
[Index
].MaxValue
!= 0)
386 /* enable and fill the custom values */
387 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
388 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
390 _itot(pVirtMem
->Pagefile
[Index
].InitialValue
, szCustVals
, 10);
391 SendDlgItemMessage(pVirtMem
->hSelf
,
397 _itot(pVirtMem
->Pagefile
[Index
].MaxValue
, szCustVals
, 10);
398 SendDlgItemMessage(pVirtMem
->hSelf
,
404 SendDlgItemMessage(pVirtMem
->hSelf
,
412 /* It's not a custom value */
413 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
414 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
416 /* is it system managed */
417 if(pVirtMem
->Pagefile
[Index
].bUsed
)
419 SendDlgItemMessage(pVirtMem
->hSelf
,
427 SendDlgItemMessage(pVirtMem
->hSelf
,
441 OnOk(PVIRTMEM pVirtMem
)
443 if(pVirtMem
->bSave
== TRUE
)
445 WritePageFileSettings(pVirtMem
);
448 if (pVirtMem
->szPagingFiles
)
449 HeapFree(GetProcessHeap(),
451 pVirtMem
->szPagingFiles
);
453 HeapFree(GetProcessHeap(),
460 OnCancel(PVIRTMEM pVirtMem
)
462 if (pVirtMem
->szPagingFiles
)
463 HeapFree(GetProcessHeap(),
465 pVirtMem
->szPagingFiles
);
467 HeapFree(GetProcessHeap(),
474 OnInitDialog(HWND hwnd
)
476 PVIRTMEM pVirtMem
= (PVIRTMEM
)HeapAlloc(GetProcessHeap(),
479 if (pVirtMem
== NULL
)
484 pVirtMem
->hSelf
= hwnd
;
485 pVirtMem
->hListView
= GetDlgItem(hwnd
, IDC_PAGEFILELIST
);
486 pVirtMem
->bSave
= FALSE
;
488 SetListViewColumns(pVirtMem
->hListView
);
490 /* Load the pagefile systems from the reg */
491 if (ReadPageFileSettings(pVirtMem
))
493 /* Parse our settings and set up dialog */
494 ParseMemSettings(pVirtMem
);
502 VirtMemDlgProc(HWND hwndDlg
,
507 /* there can only be one instance of this dialog */
508 static PVIRTMEM pVirtMem
= NULL
;
510 UNREFERENCED_PARAMETER(lParam
);
515 pVirtMem
= OnInitDialog(hwndDlg
);
520 switch (LOWORD(wParam
))
524 EndDialog(hwndDlg
, 0);
529 EndDialog(hwndDlg
, 0);
533 OnNoPagingFile(pVirtMem
);
537 OnSysManSize(pVirtMem
);
553 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
555 switch (pnmhdr
->code
)
557 case LVN_ITEMCHANGED
:
559 LPNMLISTVIEW pnmv
= (LPNMLISTVIEW
) lParam
;
561 OnSelChange(pVirtMem
, pnmv
);