home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / WIN_BOSS.LIB < prev    next >
Text File  |  1995-06-01  |  8KB  |  242 lines

  1. // WIN_Boss.lib - Functions for controlling a Windows session.  This library
  2. // ver.2          works hand-in-hand with a session running ServeOS2.cmm via
  3. //                CEnvi2 for Windows.
  4. //
  5. //
  6.  
  7. #define WCF_TEXT   1 // format for clipboard text in Windows clipboard calls
  8.  
  9. #define WVK_LBUTTON     0x01
  10. #define WVK_RBUTTON     0x02
  11. #define WVK_CANCEL      0x03
  12. #define WVK_MBUTTON     0x04
  13. #define WVK_BACK        0x08
  14. #define WVK_TAB         0x09
  15. #define WVK_CLEAR       0x0C
  16. #define WVK_RETURN      0x0D
  17. #define WVK_SHIFT       0x10
  18. #define WVK_CONTROL     0x11
  19. #define WVK_MENU        0x12
  20. #define WVK_ALT         WVK_MENU
  21. #define WVK_PAUSE       0x13
  22. #define WVK_CAPITAL     0x14
  23. #define WVK_ESCAPE      0x1B
  24. #define WVK_SPACE       0x20
  25. #define WVK_PRIOR       0x21
  26. #define WVK_NEXT        0x22
  27. #define WVK_END         0x23
  28. #define WVK_HOME        0x24
  29. #define WVK_LEFT        0x25
  30. #define WVK_UP          0x26
  31. #define WVK_RIGHT       0x27
  32. #define WVK_DOWN        0x28
  33. #define WVK_SELECT      0x29
  34. #define WVK_PRINT       0x2A
  35. #define WVK_EXECUTE     0x2B
  36. #define WVK_SNAPSHOT    0x2C
  37. #define WVK_INSERT      0x2D
  38. #define WVK_DELETE      0x2E
  39. #define WVK_HELP        0x2F
  40. /* WVK_A thru WVK_Z are the same as their ASCII equivalents: 'A' thru 'Z' */
  41. /* WVK_0 thru WVK_9 are the same as their ASCII equivalents: '0' thru '9' */
  42. #define WVK_NUMPAD0     0x60
  43. #define WVK_NUMPAD1     0x61
  44. #define WVK_NUMPAD2     0x62
  45. #define WVK_NUMPAD3     0x63
  46. #define WVK_NUMPAD4     0x64
  47. #define WVK_NUMPAD5     0x65
  48. #define WVK_NUMPAD6     0x66
  49. #define WVK_NUMPAD7     0x67
  50. #define WVK_NUMPAD8     0x68
  51. #define WVK_NUMPAD9     0x69
  52. #define WVK_MULTIPLY    0x6A
  53. #define WVK_ADD         0x6B
  54. #define WVK_SEPARATOR   0x6C
  55. #define WVK_SUBTRACT    0x6D
  56. #define WVK_DECIMAL     0x6E
  57. #define WVK_DIVIDE      0x6F
  58. #define WVK_F1          0x70
  59. #define WVK_F2          0x71
  60. #define WVK_F3          0x72
  61. #define WVK_F4          0x73
  62. #define WVK_F5          0x74
  63. #define WVK_F6          0x75
  64. #define WVK_F7          0x76
  65. #define WVK_F8          0x77
  66. #define WVK_F9          0x78
  67. #define WVK_F10         0x79
  68. #define WVK_F11         0x7A
  69. #define WVK_F12         0x7B
  70. #define WVK_F13         0x7C
  71. #define WVK_F14         0x7D
  72. #define WVK_F15         0x7E
  73. #define WVK_F16         0x7F
  74. #define WVK_NUMLOCK     0x90
  75.  
  76. #include <PMdll.lib>
  77. #include <FileIO.lib>
  78. #include <NamePipe.lib>
  79.  
  80. #define  CDECL_DLL_CALL    55
  81. #define  PASCAL_DLL_CALL   77
  82.  
  83. WinBossQuit()
  84. {
  85.    return WinVoidFunction("ServeOS2Exit");
  86. }
  87.  
  88. WinBossSelect(pPipeName) // True if exists, else false
  89. {
  90.    sprintf(lFindName,"ServeOS2: %s",pPipeName);
  91.    // create list of switchable windows
  92.    lSwitchList = WinQuerySwitchList(Info().hab);
  93.    assert( lSwitchList != NULL );
  94.    lSwitchCount = 1 + GetArraySpan(lSwitchList);
  95.    for ( li = 0; li < lSwitchCount; li++ ) {
  96.       if ( 0 == WinQuerySwitchEntry(lSwitchList[li],lSwEntry)
  97.         && lSwEntry.title  &&  !stricmp(lSwEntry.title,lFindName) ) {
  98.          strcpy(gPipeName,pPipeName);
  99.          return(True);
  100.       }
  101.    }
  102.    return(False);
  103. }
  104.  
  105.  
  106. gPipeName = "ServeOS2"; // initialize to default
  107. #define NO_WAIT_FOR_RETURN    0
  108. #define WAIT_FOR_VOID_RETURN  1
  109. #define WAIT_FOR_RETURN       2
  110.  
  111. WinFunction(pFunctionName,pReturnVar/*,Arg1,Arg2,Arg3...*/)
  112. {
  113.    va_start(lVaList,pReturnVar);
  114.    lRetBLOb = WinBossExecuteFunction(pFunctionName,WAIT_FOR_RETURN,lVaList,va_arg()-2);
  115.    va_end(lVaList);
  116.  
  117.    if ( lRetBLOb ) {
  118.       // convert the return from BLOb to real variable.  The first SWORD32 is
  119.       // the variable type, the next SWORD32 is a dimension, then length
  120.       // (only good for buffers) and then the actual variable, data types are
  121.       switch ( lTypeOrLen = BLObGet(lRetBLOb,0,SWORD32) ) {
  122.          case (-CMM_BYTE): pReturnVar = BLObGet(lRetBLOb,4,UWORD8);     break;
  123.          case (-CMM_INT):  pReturnVar = BLObGet(lRetBLOb,4,SWORD32);    break;
  124.          case (-CMM_FLOAT):pReturnVar = BLObGet(lRetBLOb,4,FLOAT64);    break;
  125.          default:          pReturnVar = BLObGet(lRetBLOb,4,lTypeOrLen); break;
  126.       }
  127.    }
  128.  
  129.    return ( lRetBlob ? True : False );
  130. }
  131.  
  132. WinVoidFunction(pFunctionName/*,Arg1,Arg2,Arg3...*/)
  133. {
  134.    va_start(lVaList,pFunctionName);
  135.    lRetBLOb = WinBossExecuteFunction(pFunctionName,WAIT_FOR_VOID_RETURN,lVaList,va_arg()-1);
  136.    va_end(lVaList);
  137.    return ( lRetBlob ? True : False );
  138. }
  139.  
  140. WinNoWaitFunction(pFunctionName/*,Arg1,Arg2,Arg3...*/)
  141. {
  142.    va_start(lVaList,pFunctionName);
  143.    lRetBLOb = WinBossExecuteFunction(pFunctionName,NO_WAIT_FOR_RETURN,lVaList,va_arg()-1);
  144.    va_end(lVaList);
  145.    return ( lRetBlob ? True : False );
  146. }
  147.  
  148. WinBossExecuteFunction(pFunctionName,pExpectReturnValue,pVaList,pVaListVarCount)
  149. {
  150.    // first item is 0 for no wait for return, 1 to wait but void return, else 2 to wait for return
  151.    BLObPut(lSendBLOb,pExpectReturnValue,SWORD32);
  152.  
  153.    // next item in BLOb is name of the function to call, put length first
  154.    BLObPut(lSendBLOb,1+strlen(pFunctionName),UWORD32);
  155.    BLObPut(lSendBLOb,pFunctionName,1+strlen(pFunctionName));
  156.  
  157.    // next item is variable count
  158.    BLObPut(lSendBLOb,pVaListVarCount,UWORD32);
  159.  
  160.    // finally, for each variable, send the var type then the variable
  161.    for ( lVarCount = 0; lVarCount < pVaListVarCount; lVarCount++ ) {
  162.       lVar = va_arg(pVaList,lVarCount);
  163.       if ( 1 == DataDimension(lVar) ) {
  164.          if ( CMM_BYTE != DataType(lVar) ) {
  165.             printf("\nWinBoss arrays may only be byte-type and 1 dimensional.\a\n");
  166.             abort();
  167.          }
  168.          lVarLen = 1+GetArraySpan(lVar);
  169.          BLObPut(lSendBLOb,lVarLen,SWORD32);
  170.          BLObPut(lSendBLOb,lVar,lVarLen);
  171.       } else {
  172.          lDataType = DataType(lVar);
  173.          BLObPut(lSendBLOb,-lDataType,SWORD32);
  174.          switch ( lDataType ) {
  175.             case CMM_BYTE:    BLObPut(lSendBLOb,lVar,UWORD8);  break;
  176.             case CMM_INT:     BLObPut(lSendBLOb,lVar,SWORD32); break;
  177.             case CMM_FLOAT:   BLObPut(lSendBLOb,lVar,FLOAT64); break;
  178.             default:
  179.                printf("\nWinBoss data type must be byte, integer, or float.\n\a");
  180.                abort();
  181.          }
  182.       }
  183.    }
  184.  
  185.    // send blob is created. pass on return blob
  186.    return WinBossTransactBLObs(lSendBLOb);
  187.  
  188. }
  189.  
  190. WinBossTransactBLObs(pSendBLOb)
  191. {
  192.    bool lSuccess = False;   // assume failure
  193.    sprintf(lFullPipeName,"\\PIPE\\%s",gPipeName);
  194.  
  195.    if ( !DosCreateNPipe(lFullPipeName,lPipeHandle,
  196.                         NP_ACCESS_DUPLEX | NP_NOINHERIT,
  197.                         NP_NOWAIT | NP_TYPE_BYTE | NP_UNLIMITED_INSTANCES | NP_READMODE_BYTE,
  198.                         4096, 4096, 0) ) {
  199.       // Give the Windows program up to 10 seconds to open the file
  200.       for ( lRepeat =  10 * 10; lRepeat--; ) {
  201.          suspend(100);
  202.          if ( !DosConnectNPipe(lPipeHandle) ) {
  203.             lSuccess = True;
  204.             break;
  205.          }
  206.       }
  207.       if ( lSuccess ) {
  208.          lSuccess = False;
  209.  
  210.          // change pipe to blocking state to make sure reads and writes are
  211.          // finished
  212.          DosSetNPHState(lPipeHandle,NP_WAIT | NP_READMODE_BYTE);
  213.  
  214.          // write the output blob length as four bytes
  215.          BLObPut(lLenBLOb,BLObSize(pSendBLOb),UWORD32);
  216.          if ( !DosWrite(lPipeHandle,lLenBLOb,BLObSize(lLenBLOb),lBytesSent)
  217.            && lBytesSent == BLObSize(lLenBLOb) ) {
  218.             // transact, which writes this command and waits for two-byte response size
  219.             if ( !DosWrite(lPipeHandle,pSendBLOb,BLObSize(pSendBLOb),lBytesSent)
  220.               && BLObSize(pSendBLOb) == lBytesSent ) {
  221.                // Read return blob size
  222.                BLObPut(lReadCount,0,UWORD32);
  223.                if ( !DosRead(lPipeHandle,lReadCount,BLObSize(lReadCount),lBytesRead)
  224.                  && BLObSize(lReadCount) == lBytesRead ) {
  225.                   // read in the result string
  226.                   lResultLen = BLObGet(lReadCount,0,UWORD32);
  227.                   BLObSize(lReceiveBLOb,lResultLen);
  228.                   if ( !DosRead(lPipeHandle,lReceiveBLOb,lResultLen,lBytesRead)
  229.                     && lResultLen == lBytesRead ) {
  230.                      lSuccess = True;
  231.                   }
  232.                }
  233.             }
  234.          }
  235.          DosDisconnectNPipe(lPipeHandle);
  236.       }
  237.       DosClose(lPipeHandle);
  238.    }
  239.    return( lSuccess ? lReceiveBLOb : NULL );
  240. }
  241.  
  242.