home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / XMS11.ZIP / XMSDEMO2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-31  |  4.3 KB  |  142 lines

  1. //********************************//
  2. // XMSDEMO2.CPP                   //
  3. // Beginning of sample program to //
  4. // demonstrate the usage of       //
  5. // extended memory using MS-XMS   //
  6. // This prg. demonstrates the     //
  7. // usage of the XMSHandle class.  //
  8. //******************************* //
  9.  
  10. #include <iostream.h>
  11. #include <string.h>
  12. #include <conio.h>
  13. #include <dos.h>
  14. #include "xms.h"
  15.  
  16. // ***************** Constants, defines, whatever...
  17. char far *vmem = (char far *)MK_FP( 0xB800, 0);    // text mode video memory
  18.                                                 // 0xB000 for mono
  19. // *** Global vars...
  20. XMSDriver XMM;                    // Declare an instance of XMSDriver
  21. extern XMSDriver *xmsptr;         // Pointer to XMM (used by XMSHandle)
  22.  
  23. // Function prototypes...
  24. void print_msg( char *str );
  25. void fill_emb ( char lttr, XMSHandle *h, unsigned len );
  26.  
  27. // Fill extended memory block with a letter
  28. void fill_emb( char lttr, XMSHandle *h, unsigned len )
  29. {
  30.     unsigned i;
  31.     
  32.     // Set the pointer to the beginning
  33.     h->SetPtr( 0 );
  34.     for( i = 0; i < len; i += 2 )   // <= the '+=2' skips the attribute byte
  35.     {
  36.          h->Store( lttr );           // Stick in the letter
  37.         h->Store( (char) 0x07 );    // Stick in the attribute byte
  38.     }
  39. }
  40.  
  41. // Print out a message with lines on the top and bottom of the string
  42. void print_msg( char *str )
  43. {
  44.     int i, len;
  45.     len = strlen( str );
  46.     cout << "\n";
  47.     for( i=0; i < len; i++ )
  48.         cout << "-";
  49.     cout << "\n" << str << "\n";
  50.     for( i=0; i < len; i++ )
  51.         cout << "-";
  52.     cout << "\n";
  53. }
  54.  
  55. int main( void )
  56. {
  57.     const numhandles = 3;       // The number of handles
  58.     char a;                     // Temporary loop counter var
  59.     int b;                        // a temp int
  60.     long c;                        // a temp long
  61.     unsigned hnum;                // Three extended memory block handles
  62.     XMSHandle *hptr[numhandles];// Pointer to the Handle
  63.     xmsptr = &XMM;              // xmsptr MUST be initialized to XMM
  64.  
  65.     // Determine the presence and availability of extended memory
  66.     if( !XMM.Inst( ) || (XMM.GetAvail() < 12) )
  67.     {
  68.         cout <<  "Cannot run the demo...\n";
  69.         cout <<  "Either HIMEM.SYS has not been properly installed,\n";
  70.         cout <<  "or there is not enough extended memory (12k).\n";
  71.         return( 1 );
  72.     }
  73.     // **************** The initialization of the program
  74.     for( a = 0; a < 3; a++ )
  75.     {
  76.         // First step is to allocate a block of ext mem
  77.         hnum = XMM.EMBAlloc( 4 );    // Allocate 4 KBytes (4096 bytes)
  78.         if( !hnum )
  79.         {
  80.             cout << "An error has occurred allocating an EMB.\n";
  81.             cout << "You may not have enough extended memory.\n";
  82.             return( 1 );
  83.         }
  84.  
  85.         // Second step is to request a class pointer to the block
  86.         hptr[a] = XMM.HndReqPtr( hnum );
  87.  
  88.         // Increase the cache size (in bytes) for optimal performance
  89.         // The default is 50 bytes, the minimum rec'd is 10 bytes.
  90.         hptr[a]->CRealloc( 256 );
  91.  
  92.         // Now you can fill the block with data
  93.         cout << "Please wait...Stuffing screen " << (int) a << "...\n";
  94.         fill_emb( a+'a', hptr[a], 4000 );
  95.     }
  96.  
  97.     // All three EMB's have been filled...It's time for the body
  98.     // of this very corny program...
  99.     cout << "Presenting the...(strike practically any key)\n";
  100.     getch( );
  101.  
  102.     // Copy the blocks from extended memory to conventional memory
  103.     for( a = 0; a < numhandles; a++ )
  104.     {
  105.         // Get a block of memory 4000 bytes long from the EMB
  106.         // at offset 0, and stuff it into 'vmem'
  107.         hptr[a]->Get( vmem, 4000, 0 );
  108.         getch( );
  109.     }
  110.     cout << "\n        abc's of the XMSHandle class!          \n";
  111.  
  112.     // Finally, free all but one of the extended memory blocks...
  113.     for( a=1; a < numhandles; a++ )
  114.         XMM.HndRelPtr( hptr[a]->GetHandle() );
  115.  
  116.     // Write a character, a long, and an integer into extended mem.
  117.     cout << "Enter a character -> ";
  118.     cin >> a;
  119.     cout << "Enter an integer -> ";
  120.     cin >> b;
  121.     cout << "Enter a long -> ";
  122.     cin >> c;
  123.  
  124.     hptr[0]->SetPtr( 12 );        // Set the 'Index' pointer
  125.     hptr[0]->Store( a );        // Write it!
  126.     hptr[0]->Store( b );
  127.     hptr[0]->Store( c );
  128.  
  129.     a = b = c = 0;                // Null vars so we can prove we read in
  130.     cout << "A character, and integer, and a long...\n";
  131.  
  132.     // Set the pointer to 12 again, and then get the three values
  133.     hptr[0]->SetPtr( 12 );
  134.     hptr[0]->Get( a ); hptr[0]->Get( b ); hptr[0]->Get(c);
  135.  
  136.     cout << a << ", " << b << ", and " << c;    // Print 'em!
  137.  
  138.     XMM.HndRelPtr( hptr[0]->GetHandle() );
  139.     return 0;
  140. }
  141. // END XMSDEMO2.CPP
  142.