home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / rwl025.zip / RWLD.C < prev    next >
C/C++ Source or Header  |  2000-06-10  |  6KB  |  180 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*  RWL - A demonstration program placed in the Public Domain         */
  4. /*        by it author, R L Walsh, June 10, 2000                      */
  5. /*                                                                    */
  6. /**********************************************************************/
  7. #if 0
  8.  
  9.     For a description of how RWL works, please see "rwlx.c"
  10.  
  11.     The version of rwl.dll included in the archive was staticly
  12.     linked to my compiler`s subsystem libraries.  These have no C
  13.     runtime environment, reducing both functionality and headaches.
  14.  
  15.     FWIW...  In this demo, the functionality is tied to the hook
  16.     code.  In a more sophisticated implementation, the hook code
  17.     would only be used once:  to initialize some independent
  18.     "presence" in the target process.  Depending on your needs,
  19.     you might spin off a thread, create an object window, open
  20.     a pipe, or whatever else seems appropriate.
  21.  
  22.     If you use this method, you`ll need to ensure that your dll
  23.     remains loaded in the target process.  Leaving the hook in place
  24.     will accomplish this.  AFAIK, having the dll load itself in the
  25.     target process using its fully-qualified name will also work.
  26.  
  27. #endif
  28. /**********************************************************************/
  29.  
  30. //  RWLD.C
  31.  
  32. /**********************************************************************/
  33.  
  34. #include "rwl.h"        // most stuff (shared with rwlx.c)
  35.  
  36. /****************************************************************************/
  37.  
  38. // exported functions declared in rwl.h
  39.  
  40. //void _System   RWLDInitDll( HMODULE hDll, ULONG x2d, ULONG d2x);
  41. //int  _System   RWLDInputHook( HAB hab, PQMSG pqmsg, ULONG fs);
  42.  
  43. // function that does the work
  44. int     RWLDGetSet( PRWL prwl, HWND hReply);
  45.  
  46. /****************************************************************************/
  47.  
  48. // this data is in a single shared segment accessible to any
  49. // process rwl.dll is loaded into
  50.  
  51. ULONG   ulRWLM_X2D = RWLM_X2D;      // msg IDs;  these initial values
  52. ULONG   ulRWLM_D2X = RWLM_D2X;      // should never end up being used
  53.  
  54. HMODULE hmodRWLD = 0;               // currently set but not used
  55.  
  56. /****************************************************************************/
  57.  
  58. // called by rwl.exe at startup;  operates only in the RWL process
  59.  
  60. void _System    RWLDInitDll( HMODULE hDll, ULONG x2d, ULONG d2x)
  61.  
  62. {
  63. // save the ID for the msg the exe posts to the dll
  64.     if (x2d)
  65.         ulRWLM_X2D = x2d;
  66. // save the ID for the msg the dll posts to the exe
  67.     if (d2x)
  68.         ulRWLM_D2X = d2x;
  69. // save the dll's module handle;  this version doesn't use it
  70.     hmodRWLD = hDll;
  71.  
  72.     return;
  73. }
  74.  
  75. /****************************************************************************/
  76.  
  77. // this hook function operates in the target process's context
  78.  
  79. int _System    RWLDInputHook( HAB hab, PQMSG pqmsg, ULONG fs)
  80.  
  81. {
  82. // we only handle one message and it has to be constructed correctly;
  83. // if so, we always return TRUE, so there will be no further handling;
  84. // otherwise, we always pass msgs on by returning FALSE.
  85.  
  86.     if (pqmsg->msg == ulRWLM_X2D && // our unique msg ID
  87.         pqmsg->hwnd == 0 &&         // this should be a queue msg
  88.         fs == PM_REMOVE &&          // no false starts
  89.         pqmsg->mp1 &&               // ptr to RWL struct in shared mem
  90.         pqmsg->mp2 &&               // hwnd that posted this msg
  91.         WinIsWindow( hab, (HWND)pqmsg->mp2))
  92.             return (RWLDGetSet( (PRWL)pqmsg->mp1, (HWND)pqmsg->mp2));
  93.  
  94.     return (FALSE);
  95. }
  96.  
  97. /****************************************************************************/
  98.  
  99. // this does the work:  accesses the RWL struct, acts according
  100. // to its flag settings, then posts a reply msg to the source
  101.  
  102. int     RWLDGetSet( PRWL prwl, HWND hReply)
  103.  
  104. {
  105.     BOOL    fFalse = 0;
  106.     PPIB    pPib;
  107.     APIRET  rc;
  108.  
  109. do
  110. {
  111. // round prwl down to the nearest segment boundary, then get it
  112.     rc = DosGetSharedMem( (PVOID)((ULONG)prwl & 0xFFFF0000),
  113.                           PAG_READ | PAG_WRITE);
  114.     if (rc)
  115.         break;
  116.  
  117. // use the flags to determine what to do
  118.     switch (prwl->flags & ~RWLF_NOFREE)
  119.     {
  120.  
  121.     // retrieve BeginLIBPATH
  122.         case (RWLF_GET | RWLF_BEG):
  123.             *(prwl->szPath) = '\0';
  124.             rc = DosQueryExtLIBPATH( prwl->szPath, BEGIN_LIBPATH);
  125.             break;
  126.  
  127.     // retrieve EndLIBPATH
  128.         case (RWLF_GET | RWLF_END):
  129.             *(prwl->szPath) = '\0';
  130.             rc = DosQueryExtLIBPATH( prwl->szPath, END_LIBPATH);
  131.             break;
  132.  
  133.     // retrieve this process's f/q exe name
  134.         case (RWLF_GET | RWLF_PROC):
  135.             *(prwl->szPath) = '\0';
  136.             DosGetInfoBlocks( NULL, &pPib);
  137.             rc = DosQueryModuleName( pPib->pib_hmte, sizeof( prwl->szPath),
  138.                                      prwl->szPath);
  139.             break;
  140.  
  141.     // set BeginLIBPATH
  142.         case (RWLF_SET | RWLF_BEG):
  143.             rc = DosSetExtLIBPATH( prwl->szPath, BEGIN_LIBPATH);
  144.             break;
  145.  
  146.     // set EndLIBPATH
  147.         case (RWLF_SET | RWLF_END):
  148.             rc = DosSetExtLIBPATH( prwl->szPath, END_LIBPATH);
  149.             break;
  150.  
  151.         default:
  152.             rc = 1;
  153.     }
  154.  
  155. // set our success/failure flags
  156.     if (rc)
  157.         prwl->flags |= RWLF_BAD;
  158.     else
  159.         prwl->flags |= RWLF_OK;
  160.  
  161. // RWLF_NOFREE will be on if we've hooked rwl.exe's msg queue;
  162. // this prevents us from accidently freeing the shared mem
  163.     if (prwl->flags & RWLF_NOFREE)
  164.         prwl->flags &= ~RWLF_NOFREE;
  165.     else
  166.         if (DosFreeMem( (PVOID)((ULONG)prwl & 0xFFFF0000)))
  167.             DosBeep( 1220, 65);
  168.  
  169. } while (fFalse);
  170.  
  171. // post our reply - rwl.exe is waiting for it
  172.     if (WinPostMsg( hReply, ulRWLM_D2X, (MP)prwl, (MP)rc) == FALSE)
  173.         DosBeep( 220, 65);
  174.  
  175.     return (TRUE);
  176. }
  177.  
  178. /****************************************************************************/
  179.  
  180.