home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / MULTINST.PRG < prev    next >
Text File  |  1994-11-18  |  3KB  |  103 lines

  1. //==================================
  2. // MULTINST.C - Matt Pietrek 1993
  3. //==================================
  4. //
  5. // Modified by:    John Skelton, 18-Nov-94.
  6. //
  7. // Copyright (C) 1994 Skelton Software, Leeds, UK.
  8. // All Rights Reserved.
  9.  
  10.  
  11. #define    MAKELP(sel, off)    (off + (sel) * 65536)
  12. #define    SELECTOROF(ptr)        C4W_HiWord(ptr)
  13.  
  14. #ifdef    TESTING
  15. function main()
  16. local    hWnd := WinSetup("test", "test")
  17. ? "Check for .T.: ", MultipleInstance()
  18. wait
  19. return 0
  20. #endif    // TESTING
  21.  
  22.  
  23. // Central function that modifies a module table to trick the loader
  24. // into letting a second instance of a multiple data segment program run.
  25.  
  26. function MultipleInstance(hInst)
  27. /*
  28.     HMODULE hModuleSel
  29.     LPSTR moduleName, fileName
  30.     BYTE cbModuleName
  31. */
  32. local    hModuleSel
  33. local    moduleName, fileName
  34. local    cbModuleName
  35.  
  36. if hInst == nil
  37.     hInst = _GetInstance()
  38. endif
  39.     hModuleSel = SELECTOROF(   ; // Convert the HINSTANCE to an HMODULE
  40.         GlobalLock(GetModuleHandle(MAKELP(0,hInst))))
  41.  
  42. #ifdef    TESTING
  43. ? hModuleSel
  44. wait
  45. #endif    // TESTING
  46.  
  47.     if hModuleSel == 0          // Make sure we succeeded.
  48.         return .f.
  49.     endif
  50.  
  51.     // Make pointers to the resident names table and the OFSTRUCT
  52. //    moduleName = MAKELP( hModuleSel, *(WORD FAR *)MAKELP(hModuleSel, 0x26))
  53.     moduleName = MAKELP( hModuleSel, C4W_PeekW(MAKELP(hModuleSel, 38)))
  54. //    fileName = MAKELP( hModuleSel, *(WORD FAR *)MAKELP(hModuleSel, 0x0A))
  55.     fileName = MAKELP( hModuleSel, C4W_PeekW(MAKELP(hModuleSel, 10)))
  56.  
  57.     // Get the module name length, and advance to the actual string
  58. //    cbModuleName = *moduleName++   // First byte is a length byte
  59.     cbModuleName = C4W_PeekB(moduleName++)   // First byte is a length byte
  60.  
  61. #ifdef    TESTING
  62. ? moduleName
  63. ? fileName
  64. ? cbModuleName
  65. wait
  66. #endif    // TESTING
  67.  
  68.     // Convert the first uppercase letter of the modulename to lowercase
  69.     do while cbModuleName != 0
  70.         if isupper(C4W_Peek(moduleName, 1))
  71.             C4W_Poke(moduleName, lower(C4W_Peek(moduleName, 1)))
  72.             exit
  73.         endif
  74.         cbModuleName-- ; moduleName++
  75.     enddo
  76.  
  77.     if cbModuleName == 0    // Make sure we succeeded
  78.         return .f.
  79.     endif
  80.  
  81.     // Position to the end of the filename. First byte is a length byte
  82.     fileName += C4W_PeekB(fileName) - 1
  83.  
  84.     // Find the first uppercase letter in the filename. Convert to lowercase
  85.     do while .t.
  86.         // Stop when we come to a directory separator or colon
  87.         if C4W_Peek(fileName, 1) $ "\/:"
  88.             return .f.
  89.         endif
  90.  
  91.         if isupper(C4W_Peek(fileName, 1))
  92.             C4W_Poke(fileName, lower(C4W_Peek(fileName, 1)))
  93.             exit
  94.         endif
  95.         fileName--
  96.     enddo
  97. return .t.
  98.  
  99. static function GetModuleHandle(hInst)
  100. local    _c := GetProcAddress( , "GetModuleHandle", "P", "int", "dword")
  101. return CallDLL(_c, hInst)
  102.  
  103.