QA: How can I change memory allocated between storage and program programmatically?

Vassili Philippov (vasja@spbteam.com), September 17, 2001.

Question

I need to change memory division between storage and RAM like in the Main applet of the Control Panel. How it can be done?

Answer

There is a SetSystemMemoryDivision function in coredll.dll library for that. This function sets number of memory pages for storage. Use GetSystemMemoryDivision function to get memory page size. Here are signatures of these two functions:

typedef BOOL (__stdcall *GetSystemMemoryDivisionProc)(LPDWORD,LPDWORD,LPDWORD); typedef DWORD (__stdcall *SetSystemMemoryDivisionProc)(DWORD);

SetSystemMemoryDivision

This function takes one argument - size of storage in memory pages. It returns result code that could be:

GetSystemMemoryDivision

This function returns 3 DWORD values and result code. The first parameter is a pointer to a variable that will contain number of memory pages allocated for storage. The second parameter is a pointer to a variable that will contain number of memory pages allocated for RAM. The third parameter is a pointer to a variable that will contain size of one memory page in bytes.

coredll.dll

To use these two functions you should load coredll.dll library using LoadLibrary function and get functions addresses using GetProcAddress function. Here is a sample code for that:

typedef BOOL (__stdcall *GetSystemMemoryDivisionProc)(LPDWORD,LPDWORD,LPDWORD); typedef DWORD (__stdcall *SetSystemMemoryDivisionProc)(DWORD); void ReadMemoryDivision() { HINSTANCE hCoreDll = LoadLibrary(_T("coredll.dll")); GetSystemMemoryDivisionProc procGet = (GetSystemMemoryDivisionProc)GetProcAddress( hCoreDll, _T("GetSystemMemoryDivision")); DWORD dwStoragePages; DWORD dwRamPages; DWORD dwPageSize; BOOL bResult = procGet(&dwStoragePages, &dwRamPages, &dwPageSize); FreeLibrary(hCoreDll); } void SetMemoryDivision() { int nStoragePages = GetNewStorageSize(); //Write your own source here HINSTANCE hCoreDll = LoadLibrary(_T("coredll.dll")); SetSystemMemoryDivisionProc procSet = (SetSystemMemoryDivisionProc)GetProcAddress( hCoreDll, _T("SetSystemMemoryDivision")); DWORD dwResult = procSet(nStoragePages); //Process result code FreeLibrary(hCoreDll); }

Emulator

Note: SetSystemMemoryDivision function does not work on Pocket PC Emulator.

Sample application

You can download a sample application (42 Kb) that shows working with SetSystemMemoryDivision and GetSystemMemoryDivision functions.

Related resources: