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 if(RegQueryValueEx(hkey
,
48 (PBYTE
)pVirtMem
->szPagingFiles
,
49 &dwDataSize
) == ERROR_SUCCESS
)
59 ShowLastWin32Error(pVirtMem
->hSelf
);
69 GetPageFileSizes(LPTSTR lpPageFiles
)
71 while (*lpPageFiles
!= _T('\0'))
73 if (*lpPageFiles
== _T(' '))
76 return (INT
)_ttoi(lpPageFiles
);
87 ParseMemSettings(PVIRTMEM pVirtMem
)
89 TCHAR szDrives
[1024]; // all drives
90 LPTSTR DrivePtr
= szDrives
;
91 TCHAR szDrive
[MAX_PATH
]; // single drive
92 TCHAR szVolume
[MAX_PATH
];
98 DriveLen
= GetLogicalDriveStrings(1023,
101 while (DriveLen
!= 0)
106 Len
= lstrlen(DrivePtr
) + 1;
109 DrivePtr
= _tcsupr(DrivePtr
);
111 /* copy the 'X:' portion */
112 lstrcpyn(szDrive
, DrivePtr
, 3);
114 if(GetDriveType(DrivePtr
) == DRIVE_FIXED
)
116 /* does drive match the one in the registry ? */
117 if(!_tcsncmp(pVirtMem
->szPagingFiles
, szDrive
, 2))
119 /* FIXME: we only check the first available pagefile in the reg */
120 InitialSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
121 MaxSize
= GetPageFileSizes(pVirtMem
->szPagingFiles
);
123 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= InitialSize
;
124 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= MaxSize
;
125 pVirtMem
->Pagefile
[PgCnt
].bUsed
= TRUE
;
126 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
130 pVirtMem
->Pagefile
[PgCnt
].InitialValue
= 0;
131 pVirtMem
->Pagefile
[PgCnt
].MaxValue
= 0;
132 pVirtMem
->Pagefile
[PgCnt
].bUsed
= FALSE
;
133 lstrcpy(pVirtMem
->Pagefile
[PgCnt
].szDrive
, szDrive
);
136 /* fill out the listview */
137 ZeroMemory(&Item
, sizeof(Item
));
138 Item
.mask
= LVIF_TEXT
;
139 Item
.iItem
= ListView_GetItemCount(pVirtMem
->hListView
);
140 Item
.pszText
= szDrive
;
141 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
143 /* set a volume label if there is one */
144 if (GetVolumeInformation(DrivePtr
,
153 if (szVolume
!= _T('\0'))
155 TCHAR szVol
[MAX_PATH
+ 2];
157 wsprintf(szVol
, _T("[%s]"), szVolume
);
160 Item
.pszText
= szVol
;
161 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
165 if ((InitialSize
!= 0) || (MaxSize
!= 0))
169 wsprintf(szSize
, _T("%i - %i"), InitialSize
, MaxSize
);
172 Item
.pszText
= szSize
;
173 (void)ListView_InsertItem(pVirtMem
->hListView
, &Item
);
182 pVirtMem
->Count
= PgCnt
;
187 WritePageFileSettings(PVIRTMEM pVirtMem
)
190 TCHAR szPagingFiles
[2048];
195 for(i
= 0; i
< pVirtMem
->Count
; ++i
)
197 if(pVirtMem
->Pagefile
[i
].bUsed
)
201 wsprintf(szText
, _T("%s\\pagefile.sys %i %i"),
202 pVirtMem
->Pagefile
[i
].szDrive
,
203 pVirtMem
->Pagefile
[i
].InitialValue
,
204 pVirtMem
->Pagefile
[i
].MaxValue
);
206 /* Add it to our overall registry string */
207 lstrcat(szPagingFiles
+ nPos
, szText
);
209 /* Record the position where the next string will start */
210 nPos
+= (INT
)lstrlen(szText
) + 1;
212 /* add another NULL for REG_MULTI_SZ */
213 szPagingFiles
[nPos
] = _T('\0');
218 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE
,
222 REG_OPTION_NON_VOLATILE
,
226 NULL
) == ERROR_SUCCESS
)
228 if (RegSetValueEx(hk
,
232 (LPBYTE
) szPagingFiles
,
233 (DWORD
) nPos
* sizeof(TCHAR
)))
242 ShowLastWin32Error(pVirtMem
->hSelf
);
247 SetListViewColumns(HWND hwndListView
)
252 GetClientRect(hwndListView
, &rect
);
254 (void)ListView_SetExtendedListViewStyle(hwndListView
,
255 LVS_EX_FULLROWSELECT
);
257 ZeroMemory(&lvc
, sizeof(lvc
));
258 lvc
.mask
= LVCF_SUBITEM
| LVCF_WIDTH
| LVCF_FMT
;
259 lvc
.fmt
= LVCFMT_LEFT
;
261 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.1);
263 (void)ListView_InsertColumn(hwndListView
, 0, &lvc
);
265 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.3);
267 (void)ListView_InsertColumn(hwndListView
, 1, &lvc
);
269 lvc
.cx
= (INT
)((rect
.right
- rect
.left
) * 0.6);
271 (void)ListView_InsertColumn(hwndListView
, 2, &lvc
);
276 OnNoPagingFile(PVIRTMEM pVirtMem
)
278 /* Disable the page file custom size boxes */
279 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
280 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
285 OnSysManSize(PVIRTMEM pVirtMem
)
287 /* Disable the page file custom size boxes */
288 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
289 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
294 OnCustom(PVIRTMEM pVirtMem
)
296 /* Enable the page file custom size boxes */
297 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
298 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
303 OnSet(PVIRTMEM pVirtMem
)
307 pVirtMem
->bSave
= TRUE
;
309 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
315 if(Index
< pVirtMem
->Count
)
319 /* check if custom settings are checked */
320 if(SendDlgItemMessage(pVirtMem
->hSelf
,
326 SendDlgItemMessage(pVirtMem
->hSelf
,
331 pVirtMem
->Pagefile
[Index
].InitialValue
= _ttoi(szText
);
333 SendDlgItemMessage(pVirtMem
->hSelf
,
338 pVirtMem
->Pagefile
[Index
].MaxValue
= _ttoi(szText
);
343 pVirtMem
->Pagefile
[Index
].InitialValue
= pVirtMem
->Pagefile
[Index
].MaxValue
= 0;
345 // check to see if this drive is used for a paging file
346 if (SendDlgItemMessage(pVirtMem
->hSelf
,
352 pVirtMem
->Pagefile
[Index
].bUsed
= TRUE
;
356 pVirtMem
->Pagefile
[Index
].bUsed
= FALSE
;
364 OnSelChange(PVIRTMEM pVirtMem
,
367 TCHAR szCustVals
[255];
370 UNREFERENCED_PARAMETER(pnmv
);
372 Index
= (INT
)SendDlgItemMessage(pVirtMem
->hSelf
,
378 if(Index
< pVirtMem
->Count
)
381 if(pVirtMem
->Pagefile
[Index
].InitialValue
!= 0 &&
382 pVirtMem
->Pagefile
[Index
].MaxValue
!= 0)
384 /* enable and fill the custom values */
385 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), TRUE
);
386 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), TRUE
);
388 _itot(pVirtMem
->Pagefile
[Index
].InitialValue
, szCustVals
, 10);
389 SendDlgItemMessage(pVirtMem
->hSelf
,
395 _itot(pVirtMem
->Pagefile
[Index
].MaxValue
, szCustVals
, 10);
396 SendDlgItemMessage(pVirtMem
->hSelf
,
402 SendDlgItemMessage(pVirtMem
->hSelf
,
410 /* It's not a custom value */
411 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_MAXSIZE
), FALSE
);
412 EnableWindow(GetDlgItem(pVirtMem
->hSelf
, IDC_INITIALSIZE
), FALSE
);
414 /* is it system managed */
415 if(pVirtMem
->Pagefile
[Index
].bUsed
)
417 SendDlgItemMessage(pVirtMem
->hSelf
,
425 SendDlgItemMessage(pVirtMem
->hSelf
,
439 OnOk(PVIRTMEM pVirtMem
)
441 if(pVirtMem
->bSave
== TRUE
)
443 WritePageFileSettings(pVirtMem
);
446 if (pVirtMem
->szPagingFiles
)
447 HeapFree(GetProcessHeap(),
449 pVirtMem
->szPagingFiles
);
451 HeapFree(GetProcessHeap(),
458 OnCancel(PVIRTMEM pVirtMem
)
460 if (pVirtMem
->szPagingFiles
)
461 HeapFree(GetProcessHeap(),
463 pVirtMem
->szPagingFiles
);
465 HeapFree(GetProcessHeap(),
472 OnInitDialog(HWND hwnd
)
474 PVIRTMEM pVirtMem
= (PVIRTMEM
)HeapAlloc(GetProcessHeap(),
477 if (pVirtMem
== NULL
)
482 pVirtMem
->hSelf
= hwnd
;
483 pVirtMem
->hListView
= GetDlgItem(hwnd
, IDC_PAGEFILELIST
);
484 pVirtMem
->bSave
= FALSE
;
486 SetListViewColumns(pVirtMem
->hListView
);
488 /* Load the pagefile systems from the reg */
489 if (ReadPageFileSettings(pVirtMem
))
491 /* Parse our settings and set up dialog */
492 ParseMemSettings(pVirtMem
);
500 VirtMemDlgProc(HWND hwndDlg
,
505 /* there can only be one instance of this dialog */
506 static PVIRTMEM pVirtMem
= NULL
;
508 UNREFERENCED_PARAMETER(lParam
);
513 pVirtMem
= OnInitDialog(hwndDlg
);
518 switch (LOWORD(wParam
))
522 EndDialog(hwndDlg
, 0);
527 EndDialog(hwndDlg
, 0);
531 OnNoPagingFile(pVirtMem
);
535 OnSysManSize(pVirtMem
);
551 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
553 switch (pnmhdr
->code
)
555 case LVN_ITEMCHANGED
:
557 LPNMLISTVIEW pnmv
= (LPNMLISTVIEW
) lParam
;
559 OnSelChange(pVirtMem
, pnmv
);