home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / BIN2PTR.PRG < prev    next >
Text File  |  1995-01-12  |  2KB  |  75 lines

  1. /////////////////////////
  2. //
  3. //    bin2ptr.prg - simple access to C pointers
  4. //
  5. //    Written by:    John M. Skelton, 12-Jan-95.
  6. //
  7. //    Copyright (C) 1995 Skelton Software, Kendal Cottage, Hillam, Leeds LS25 5HP, UK.
  8. //    All Rights Reserved.
  9. //
  10. /////////////////////////
  11.  
  12.  
  13. #define    GMEM_DDESHARE    8192        // magic number for Windows
  14. #define    GMEM_MOVEABLE    2        // magic number for Windows
  15.  
  16.  
  17. static    anPtr := {}
  18. static    ahData := {}
  19.  
  20.  
  21. // Convert a Clipper string to a C-style pointer
  22. // (that won't move, regardless of Clipper VM activity)
  23. //
  24. // NOTE: You must call FreePtr() when you've finished with the pointer!
  25.  
  26. function Str2Ptr(cStr)            // --> nPtr
  27. return Bin2Ptr(cStr + chr(0))
  28.  
  29.  
  30. // Convert a Clipper binary data string to a C-style pointer
  31. // (that won't move, regardless of Clipper VM activity)
  32. //
  33. // NOTE: You must call FreePtr() when you've finished with the pointer!
  34.  
  35. function Bin2Ptr(cData)        // --> nPtr
  36. local    hData
  37. local    nPtr, i
  38.  
  39. if cData == nil .or. (valtype(cData) == "N" .and. cData == 0)
  40.     return 0        // NULL pointer
  41. //else cData better be valtype() "C"!
  42. endif
  43.  
  44. hData = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, len(cData))
  45. nPtr = GlobalLock(hData)
  46. C4W_Poke(nPtr, cData)
  47. if (i := ascan(ahData, nil)) == 0
  48.     aadd(ahData, hData)
  49.     aadd(anPtr, nPtr)
  50. else
  51.     ahData[i] = hData
  52.     anPtr[i] = nPtr
  53. endif
  54. return nPtr
  55.  
  56.  
  57.  
  58. // To get data back, use C4W_Peek()
  59.  
  60.  
  61.  
  62. // Free a pointer
  63.  
  64. function FreePtr(nPtr)
  65. local    i
  66.  
  67. if nPtr != nil .and. nPtr != 0 .and. (i := ascan(anPtr, nPtr)) != 0
  68.     GlobalUnlock(ahData[i])
  69.     GlobalFree(ahData[i])
  70.     ahData[i] := anPtr[i] := nil
  71. endif
  72. nPtr = 0            // if passed as @nPtr, it'll be NULLed
  73. return nil
  74.  
  75.