home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 2.ddi / XMSCPP.ZIP / XMSDEMO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-19  |  3.0 KB  |  116 lines

  1. //********************************
  2. // Beginning of sample program to
  3. // demonstrate the usage of
  4. // extended memory using MS-XMS
  5. //*******************************
  6.  
  7. #include <iostream.h>
  8. #include <string.h>
  9. #include "xms.h"
  10.  
  11. void print_msg( char *str );
  12.  
  13. // Print a message with little box lines on top and below
  14. // very dumb, just used to make output look somewhat decent
  15. void print_msg( char *str )
  16. {
  17.     int i, len;
  18.     len = strlen( str );
  19.     cout << "\n";
  20.     for( i=0; i < len; i++ )
  21.         cout << "-";
  22.     cout << "\n" << str << "\n";
  23.     for( i=0; i < len; i++ )
  24.         cout << "-";
  25.     cout << "\n";
  26. }
  27.  
  28. int main( void )
  29. {
  30.     XMSDriver XMM;
  31.     long AMem;
  32.     char Dude[ 10 ] = "Excellent";
  33.     char ReadIn[ 22 ];
  34.     char Block[22] = "DudeDudeDudeDudeDude!";
  35.  
  36.     unsigned EMBHandles[ 5 ], i;
  37.  
  38.     if( !XMM.Inst() || (XMM.GetAvail() < 64) )
  39.     {
  40.         cout << "Can't continue the XMS demo.\n";
  41.         cout << "No extended memory manager installed, or\n";
  42.         cout << "not enough extended memory available (needs 64k).\n";
  43.         cout << "Add HIMEM.SYS to your config.sys file.\n";
  44.         return( 1 );
  45.     }
  46.     
  47.     print_msg( "Status Info" );
  48.     AMem = XMM.GetAvail( );
  49.  
  50.     cout << "Available Extended Memory = ";
  51.     cout << AMem << "\n";
  52.     cout << "Total Installed = " << XMM.GetTotal( ) << "\n";
  53.  
  54.     print_msg( "High Memory Area Functions" );
  55.     cout << "Requesting HMA...\n";
  56.     if( XMM.ReqHMA( ) )
  57.         cout << "HMA gotten!\n";
  58.     else
  59.         cout << "HMA gotten - NOT!\n";
  60.  
  61.     cout << "Enabling A20 line...\n";
  62.     if( XMM.A20Enable( ) )
  63.         cout << "A20 Enabled\n";
  64.     else
  65.         cout << "Error enabling A20\n";
  66.  
  67.     cout << "Checking current A20 state...\n";
  68.     if( XMM.A20Query( ) )
  69.     {
  70.         cout << "A20 Enabled (disabling now).\n";
  71.         XMM.A20Disable( );
  72.     }
  73.     else
  74.         cout << "A20 Disabled.\n";
  75.  
  76.     if( XMM.HMAInst( ) )
  77.     {
  78.         XMM.A20Enable( );
  79.         // Size, Index, Length, Data
  80.         cout << "Writing " << Dude << " to HMA...\n";
  81.         XMM.HMAWrite( 1, 5, 10, Dude );
  82.         cout << "Attempting to read in...\n";
  83.         XMM.HMARead( 1, 5, 10, ReadIn );
  84.         cout << '\'' << ReadIn << "\' was read in from HMA.\n";
  85.         XMM.A20Disable( );
  86.         cout << "Freeing HMA...\n";
  87.         XMM.RelHMA( );
  88.     }
  89.     
  90.     print_msg( "Extended Memory Block Functions" );
  91.     for( i = 0; i < 4; i++ )
  92.         EMBHandles[ i ] = XMM.EMBAlloc( (int) AMem / 5 );
  93.     for( i = 0; i < 4; i++ )
  94.         cout << "EMBHandle #" << i << " = " << EMBHandles[ i ] << "\n";
  95.     // Free all but one extended memory block (EMBHandles[0])
  96.     for( i = 1; i < 4; i++ )
  97.         XMM.EMBFree( EMBHandles[ i ] );
  98.  
  99.     AMem = (long) AMem / 5 + 32;        // Reallocate 32k more
  100.     if( XMM.EMBRealloc( EMBHandles[0], AMem + 32 ) )
  101.         cout << "Reallocated to " << AMem + 32 << "k bytes...\n";
  102.     else
  103.         cout << "Cannot reallocate...\n";
  104.  
  105.     // Write to and read from extended memory
  106.     cout << "Writing " << Block << " to extended memory...\n";
  107.     XMM.EMBMoveToExt( EMBHandles[0], 24, Block, strlen( Block )+1 );
  108.     XMM.EMBMoveToCon( ReadIn, EMBHandles[0], 24, strlen( Block )+1 );
  109.  
  110.     cout << "Read in from EMB...-> " << ReadIn << '\n';
  111.     
  112.     cout << "Freeing block...\n";
  113.     XMM.EMBFree( EMBHandles[0] );
  114. }
  115.  
  116.