home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / DDJ9403A.ZIP / ASPI.ZIP / ASPIDLL.C < prev    next >
C/C++ Source or Header  |  1993-11-11  |  5KB  |  204 lines

  1. // ----------------------------------------------------------------------
  2. // Module ASPIDLL.C
  3. // Dynamic Link Library code for ASPI under Windows.
  4. //
  5. // Copyright (C) 1993, Brian Sawert.
  6. // All rights reserved.
  7. //
  8. // Notes:
  9. //    Compile with MEDIUM model for DLL.
  10. //    Compiling as DLL defines __DLL__ and defines _FAR as far.
  11. //
  12. // ----------------------------------------------------------------------
  13.  
  14. #include <windows.h>
  15.  
  16. #include <fcntl.h>
  17. #include <alloc.h>
  18. #include <mem.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <string.h>
  22. #include <errno.h>
  23.  
  24. #include "aspi.h"                        // ASPI definitions and constants
  25. #include "scsi.h"                        // SCSI definitions and constants
  26.  
  27.  
  28. // -------------------- defines and macros -------------------
  29.  
  30. #define BUSY_WAIT    0x1000;                // repeat count for busy check
  31. #define IOCTL_READ    2                    // IOCTL read function
  32. #define ASPI_NAME    "SCSIMGR$"            // SCSI manager driver name
  33.  
  34. #define asize(x)    sizeof((x)) / sizeof(*(x))
  35.  
  36.  
  37. // -------------------- global variables -------------------
  38.  
  39. DWORD dwPtr[3];                            // returns from GlobalDOSAlloc
  40.  
  41.  
  42. // -------------------- external variables -------------------
  43.  
  44.  
  45. // -------------------- local functions -------------------
  46.  
  47. void far *MaptoReal(void far *pptr);    // map to real mode address
  48. void far *MaptoProt(void far *rptr);    // map to protected mode address
  49. DWORD AllocRealBuff(DWORD bytes);        // allocate real mode buffer
  50. DWORD FreeRealBuff(DWORD SelSeg);        // free real mode buffer
  51.  
  52.  
  53. // -------------------- external functions -------------------
  54.  
  55.  
  56. // -------------------- function definitions -------------------
  57.  
  58.  
  59. #pragma argsused
  60. #pragma option -w-aus
  61.  
  62. // ------------------------------------------------------------
  63. // Windows DLL entry procedure.
  64. // ------------------------------------------------------------
  65.  
  66. int FAR PASCAL _export LibMain(HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
  67.     LPSTR lpszCmdLine)                    // library initialization routine
  68.     {
  69.     if (wHeapSize != 0)                    // just unlock data segment
  70.         UnlockData(0);
  71.  
  72.     return 1;
  73.     }
  74.  
  75.  
  76. #pragma argsused
  77. #pragma option -w-aus
  78.  
  79. // ------------------------------------------------------------
  80. // Windows DLL exit procedure.
  81. // ------------------------------------------------------------
  82.  
  83. int FAR PASCAL _export WEP(int bSystemExit)    // Windows exit procedure
  84.     {
  85.     return 1;                            // just return success code
  86.     }
  87.  
  88.  
  89. // ----------------------------------------------------------------------
  90. // Routine to map protected mode pointer to real mode.
  91. //
  92. // Usage:    void far *MaptoReal(void far *pptr);
  93. //    
  94. // Returns real mode pointer on success, NULL on error.
  95. // ----------------------------------------------------------------------
  96.  
  97. void far *MaptoReal(void far *pptr)
  98.     {
  99.     WORD sel;                            // protected mode selector
  100.     void far *ptr = NULL;                // real mode pointer
  101.     int count;
  102.  
  103.     sel = FP_SEG(pptr);                    // get selector value
  104.  
  105.     for (count = 0; count < asize(dwPtr); count++)
  106.         {                                // loop through allocations
  107.         if (sel == LOWORD(dwPtr[count]))
  108.             {                            // found matching selector
  109.             ptr = MAKELP(HIWORD(dwPtr[count]), FP_OFF(pptr));
  110.                                         // build real mode pointer
  111.             break;
  112.             }
  113.         }
  114.  
  115.     return(ptr);
  116.     }
  117.  
  118.  
  119. // ----------------------------------------------------------------------
  120. // Routine to map real mode pointer to protected  mode.
  121. //
  122. // Usage:    void far *MaptoProt(void far *rptr);
  123. //    
  124. // Returns protected mode pointer on success, NULL on error.
  125. // ----------------------------------------------------------------------
  126.  
  127. void far *MaptoProt(void far *rptr)
  128.     {
  129.     WORD seg;                            // real mode segment
  130.     void far *ptr = NULL;                // protected mode pointer
  131.     int count;
  132.  
  133.     seg = FP_SEG(rptr);                    // get selector value
  134.  
  135.     for (count = 0; count < asize(dwPtr); count++)
  136.         {                                // loop through allocations
  137.         if (seg == HIWORD(dwPtr[count]))
  138.             {                            // found matching segment
  139.             ptr = MAKELP(LOWORD(dwPtr[count]), FP_OFF(rptr));
  140.                                         // build protected mode pointer
  141.             break;
  142.             }
  143.         }
  144.  
  145.     return(ptr);
  146.     }
  147.  
  148.  
  149. // ----------------------------------------------------------------------
  150. // Routine to allocate real mode buffer.
  151. //
  152. // Usage:    DWORD AllocRealBuff(DWORD bytes);
  153. //    
  154. // Returns combined selector and segment on success, NULL on error.
  155. // ----------------------------------------------------------------------
  156.  
  157. DWORD AllocRealBuff(DWORD bytes)
  158.     {
  159.     DWORD SelSeg;
  160.     WORD sel;
  161.  
  162.     SelSeg = GlobalDosAlloc(bytes);        // allocate lower memory
  163.  
  164.     if (SelSeg != NULL)
  165.         {                                // allocated succeeded
  166.         sel = LOWORD(SelSeg);            // extract selector
  167.         if (GlobalPageLock(sel) == 0)
  168.             {                            // memory lock failed
  169.             GlobalDosFree(sel);            // free memory
  170.             SelSeg = NULL;                // clear return value
  171.             }
  172.         }
  173.  
  174.     return(SelSeg);                        // return selector and segment
  175.     }
  176.  
  177.  
  178. // ----------------------------------------------------------------------
  179. // Routine to free real mode buffer.
  180. //
  181. // Usage:    void FreeRealBuff(DWORD SelSeg);
  182. //    
  183. // Returns combined selector and segment.
  184. // ----------------------------------------------------------------------
  185.  
  186. DWORD FreeRealBuff(DWORD SelSeg)
  187.     {
  188.     WORD sel;
  189.     DWORD retval = SelSeg;
  190.  
  191.     sel = LOWORD(SelSeg);                // extract selector
  192.  
  193.     if (sel != NULL)
  194.         {
  195.         GlobalPageUnlock(sel);            // unlock memory
  196.         GlobalDosFree(sel);                // free memory
  197.         retval = 0L;
  198.         }
  199.  
  200.     return(retval);
  201.     }
  202.  
  203.  
  204.