## Printer Device Driver

[prev][next][parent][TOC]

### Example Code

Creating and Allocating From a Per-DC Heap

Following is an example of creating and allocating from a per-DC heap during a printer-driver enable via the OS2_PM_DRV_ENABLE entry point:

    ULONG APIENTRY OS2_PM_DRV_ENABLE( ULONG ulSubFunc, ULONG p1, ULONG p2 )

    {

      switch( ulSubFunc )             // Determine which enable/disable subfunction

      {

        case FILL_PHYSICAL:           // Enable subfunction

        {

           // Create a per-DC memory area (heap)

           hmcbHeap = GplMemoryCreateInstance(

                               LEN_DCHEAP,            // Size of DC Heap (80K)

                               0L,                    // Default extent

                               0L,                    // Default threshold

                               PROCESS_MEMORY );      // Type of heap (define in GPLMEM.H)

           // If GplMemoryCreateInstance succeeded

           if(hmcbHeap != NULL);

           {

             // Allocate PDEVICEBLOCK

             pddc->pdb = ( PDEVICEBLOCK ) GplMemoryAlloc( hmcbHeap, sizeof( DEVICEBLOCK ) );

           } /* end if */

         } /* end case */

         break;

      } /* end switch */

    } /* end OS2_PM_DRV_ENABLE */

Note: System memory must be used when passing data buffers to PrtWrite() calls.

Creating a Global Heap

Following is an example of creating a global heap:

    ULONG APIENTRY  _DLL_InitTerm( ULONG hThisModule, ULONG ulFlag )

    {

      switch( ulFlag )        // Flag to determine which load/unload module subfunction

      {

        case 0:               // Load module

        {

          // Create a global heap

          globals.pvHeap = GplMemoryCreateInstance(

                                     LEN_SHAREDHEAP,   // Initial size (256K)

                                     0,                // Default extent

                                     0,                // Default threshold

                                     SHARED_MEMORY);   // Type of heap (defined in GPLMEM.H)

        } /* end case */

        break;

      } /* end switch */

    } /* end _DLL_InitTerm */

Allocating From a Global Heap

Following is an example of allocating from a global heap:

    switch( ulSubFunc )                       // Determine which enable/disable subfunction

    {

      case FILL_LOGICAL:                      // Enable subfunction

      {

        // For every string in our driver's string resource table

        for( i=0; i < STRING_TABLE_SIZE; i++ )

        {

          // Load each string from resource table (returns length)

          sLen = WinLoadString (szTemp);

          // Allocate memory to hold string just loaded from global heap

          globals.pbStringTable[i] = GplMemoryAlloc( globals.pvSharedHeap, sLen+1 );

          // Copy string from temporary buffer to our driver's global string table

          strcpy( globals.pbStringTable[i], szTemp );

         } /* end for */

         break;

       } /* end case */

    } /* end switch */

Deleting a Per-DC Heap

Following is an example of deleting a per-DC heap:

    ULONG APIENTRY OS2_PM_DRV_ENABLE( ULONG ulSubFunc, ULONG p1, ULONG p2 )

    {

      switch( ulSubFunc )

      {

        case DISABLE_PHYSICAL:

        {

          // Free per-DC heap after freeing per-DC memory objects

          GplMemoryDeleteInstance( hmcbHeap );

        } /* end case */

        break;

       } /* end switch */

    } /* end OS2_PM_DRV_ENABLE */

Deleting a Global Heap

Following is an example of deleting a global heap:

    ULONG APIENTRY  _DLL_InitTerm( ULONG hThisModule, ULONG ulFlag )

    {

      switch( ulFlag )

      {

        case 1:

        {

          // Free global heap after freeing global memory objects

          GplMemoryDeleteInstance( globals.pvSharedHeap );

        } /* end case */

        break;

      } /* end switch */

    } /* end _DLL_InitTerm */