Tuesday, January 27, 2004

As you know, the program memory on Pocket PC and Smartphone devices supposed to be handled by the OS itself. Meaning that shell would start sending WM_CLOSE messages if there is less than 128 KB of free memory. In some cases when running you application before allocation of some big chunk of memory you'd need to make sure that there's enough memory available for that operation. There is the SHCloseApps API function in the aygshell library that will do just what you need. We could also utilize the GlobalMemoryStatus to verify if such a drastic measures are required:

public bool FreeProgramMemory(int memSought)

{                

      bool result = true;

      MEMORYSTATUS memStatus = new MEMORYSTATUS();

      GlobalMemoryStatus(ref memStatus);

      //check if we're out of memory first

      if( memStatus.dwAvailPhys < memSought)

      {

            if (SHCloseApps(memSought)!= 0)

                  result = false;

      }

      return result;

}

//Required P/Invoke declarations

[DllImport("aygshell.dll")]

public static extern int SHCloseApps(int  dwMemSought);

 

[DllImport("coredll.dll")]

public static extern  void GlobalMemoryStatus(

            ref MEMORYSTATUS lpBuffer );

 

public struct MEMORYSTATUS

{

      public int dwLength;

      public int dwMemoryLoad;

      public int dwTotalPhys;

      public int dwAvailPhys;

      public int dwTotalPageFile;

      public int dwAvailPageFile;

      public int dwTotalVirtual;

      public int dwAvailVirtual;

}

 

1/27/2004 2:20:36 PM (GMT Standard Time, UTC+00:00)  #     |