home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MKHOOK1.ZIP / DOSWIN32.MAC < prev    next >
Text File  |  1992-08-29  |  13KB  |  422 lines

  1. ;--------------------------------------------------------------------
  2. ;doswin32.mac  Begun:Sat  08-22-1992  Revised:Fri  08-28-1992
  3. ;
  4. ;These Macros are more general purpose in definition and serve both
  5. ;DOS and WIN calls
  6. ;
  7. ;--------------------------------------------------------------------
  8.  
  9. ;equates related to message structure
  10. hwnd           equ   quemsg.qmsg_hwnd
  11. msg            equ   quemsg.qmsg_msg
  12. mp1            equ   quemsg.qmsg_mp1
  13. mp2            equ   quemsg.qmsg_mp2
  14.  
  15. HWND_DESKTOP   equ   1               ;needed for  WinErrorMessage macro
  16. msgBoxErrStyle equ   (MB_ERROR OR MB_CANCEL)
  17.  
  18. ;---------------- I/O DOS Calls Only---------------
  19. stdin          equ   0
  20. stdout         equ   1
  21. stderr         equ   2
  22.  
  23. ;---------------- Useful ---------------
  24. cr             equ   0dh
  25. lf             equ   0ah
  26. nl             equ   0dh,0ah   ;cr+lf
  27. BEL            equ   07h
  28. NULL           equ   0000h
  29.  
  30. ;External declarations for DOS (Control Program) Functions (Used So Far)
  31. EXTRN    DosAllocMem:near,DosAllocSharedMem:near,DosBeep:near,DosClose:near
  32. EXTRN    DosCreateThread:near,DosDevConfig:near,DosDevIOCtl:near
  33. EXTRN    DosExecPgm:near,DosExit:near,DosFreeMem:near,DosFreeModule:near
  34. EXTRN    DosGetInfoBlocks:near,DosGetNamedSharedMem:near,DosKillThread:near
  35. EXTRN    DosLoadModule:near,DosOpen:near,DosPutMessage:near
  36. EXTRN    DosQueryModuleHandle:near,DosQueryModuleName:near,DosQueryProcAddr:near
  37. EXTRN    DosQueryProcType:near,DosRead:near,DosSleep:near,DosSetFilePtr:near
  38. EXTRN    DosStartSession:near,DosWrite:near
  39.  
  40. ;External declarations for Window (PM) Functions (Used So Far)
  41. EXTRN    WinAlarm:near,WinBeginPaint:near,WinBroadcastMsg:near,WinChangeSwitchEntry:near
  42. EXTRN    WinCreateMsgQueue:near,WinCreateStdWindow:near,WinDefWindowProc:near
  43. EXTRN    WinDestroyMsgQueue:near,WinDestroyWindow:near,WinDispatchMsg:near
  44. EXTRN    WinDrawText:near,WinEndPaint:near,WinFillRect:near,WinFocusChange:near
  45. EXTRN    WinFreeErrorInfo:near
  46. EXTRN    WinGetCurrentTime:near,WinGetErrorInfo:near,WinGetKeyState:near
  47. EXTRN    WinGetMsg:near,WinGetLastError:near,WinInitialize:near,WinMessageBox:near
  48. EXTRN    WinOpenWindowDC:near,WinPostMsg:near
  49. EXTRN    WinQueryDesktopWindow:near,WinQueryFocus:near,WinQueryMsgTime:near
  50. EXTRN    WinQuerySwitchList:near,WinQueryWindowRect:near
  51. EXTRN    WinRegisterClass:near,WinReleaseHook:near,WinReleasePS:near
  52. EXTRN    WinSetFocus:near,WinSetHook:near,WinSendMsg:near,WinTerminate:near
  53.  
  54. ;----------------------------------------------------------
  55. ;DEFINE Macros for DATA SEgment
  56.  
  57. ;Define Buffers used for Storage and for Num conversion
  58. $DefineNumBufs  MACRO
  59.     B_Buf       BYTE  3 dup(?)
  60.     W_Buf       BYTE  5 dup(?)
  61.     DW_Buf      BYTE 10 dup(0)
  62.     Bit8Str     BYTE  8 dup(?)
  63.     Bit16Str    BYTE  16 dup(?)
  64.     Bit32Str    BYTE  32 dup(?)
  65.     ten        DWORD  10         ;used for AsciiToWord
  66.     lth        DWORD  ?          ;used for AsciiToWord
  67.     errId      DWORD  ?
  68. ENDM
  69.  
  70. ;some standard strings and other vars for Data segment
  71. $DOSErrorMessages  MACRO
  72.     DosErrorMsg     BYTE   " Error calling Dos CP Function "
  73.     DosErrOpts      BYTE   "Enter Q to Quit Program - Any other Key to Continue"
  74.     DosReadErr      BYTE   ?,?    ;required for DOS read
  75.     newline         BYTE   13,10
  76.     nwritten       DWORD   ?      ;required for DOS Write
  77. ENDM
  78.  
  79. ;This macro allows the calling of any function subject only to the fact
  80. ;that it has no more than 10 parameters and that all are either DWORD
  81. ;long or are 32 bit offsets - i.e. it is a Full PUSH
  82. ;Enter Arguments in same order as listed in OS/2 2.0 Technical Library
  83. $Call MACRO fname,arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9
  84.     ctr = 0
  85.     IFNB <arg9>
  86.        push  arg9
  87.        ctr = ctr + 4
  88.     ENDIF
  89.     IFNB <arg8>
  90.        push  arg8
  91.        ctr = ctr + 4
  92.     ENDIF
  93.     IFNB <arg7>
  94.        push  arg7
  95.        ctr = ctr + 4
  96.     ENDIF
  97.     IFNB <arg6>
  98.        push  arg6
  99.        ctr = ctr + 4
  100.     ENDIF
  101.     IFNB <arg5>
  102.        push  arg5
  103.        ctr = ctr + 4
  104.     ENDIF
  105.     IFNB <arg4>
  106.        push  arg4
  107.        ctr = ctr + 4
  108.     ENDIF
  109.     IFNB <arg3>
  110.        push  arg3
  111.        ctr = ctr + 4
  112.     ENDIF
  113.     IFNB <arg2>
  114.        push  arg2
  115.        ctr = ctr + 4
  116.     ENDIF
  117.     IFNB <arg1>
  118.        push  arg1
  119.        ctr = ctr + 4
  120.     ENDIF
  121.     IFNB <arg0>
  122.        push  arg0
  123.        ctr = ctr + 4
  124.     ENDIF
  125.     call   fname
  126.     add    esp,ctr
  127. ENDM
  128.  
  129. $Alarm  MACRO     ;Makes a nice up and down sound
  130.     $Call DosBeep,300,300
  131.     $Call DosBeep,600,300
  132.     $Call DosBeep,300,300
  133. ENDM
  134.  
  135. ; Concantenate String S2 to end of String S1 and place at S3
  136. ; S3 buffer must be long enough to hold S1+S2. S3 is 0 terminated
  137. ; copy S1 to buffer S3 and then add S2 at end
  138. $ConcantS1andS2toS3  MACRO  S1,S2,S3
  139.      pusha
  140.      mov     edi,offset S3
  141.      mov     esi,offset S1
  142.      mov     ecx,LENGTHOF S1
  143.      rep     movsb
  144.      mov     esi,offset S2
  145.      mov     ecx,LENGTHOF S2
  146.      rep     movsb
  147.      inc     edi
  148.      mov     byte ptr [edi],0    ;NULL Terminate
  149.      popa
  150. ENDM
  151.  
  152. ;This is useful for BEEPING
  153. $DosBeep equ  $Call DosBeep,500,500   ;frequency,duration
  154.  
  155. ;Useful for exiting
  156. $DosExit equ  $Call DosExit,1,0  ;return 0 and all threads
  157.  
  158. ;useful for Carriage ReturnLine Feeds - requires newline definition in .DATA
  159. $NewLine equ  $Call DosWrite,stdout,offset newline,LENGTHOF newline,offset nwritten
  160.  
  161. ;Used for Displaying Error Messages forDOS functions
  162. ;Called right after a $Call call of a DOS function if Error trapping desired
  163. ;The user passes the FuncName to be displayed
  164. ;It should be the Name of the Function used in $Call
  165. ;If error is detected, after displaying error number and function
  166. ;User is offered option of exiting or continuing
  167. $DosErrMsg  MACRO  FuncName   ;FuncName passed in calling macros
  168.     local  exitDEM,  msgFuncName
  169.     .IF  eax == 0
  170.         jmp  exitDEM
  171.     .ENDIF
  172.     IFNDEF  FuncName
  173.       .DATA
  174.        msgFuncName   BYTE FuncName
  175.       .CODE
  176.     ENDIF
  177.     $DWordToAscii eax,DW_Buf
  178.     $NewLine
  179.     $Call DosWrite,stdout,offset DW_Buf,LENGTHOF DW_BUF,offset nwritten
  180.     $Call DosWrite,stdout,offset DosErrorMsg,LENGTHOF DosErrorMsg,offset nwritten
  181.     $Call DosWrite,stdout,offset msgFuncName,LENGTHOF msgFuncName,offset nwritten
  182.     $DosBeep
  183.     $NewLine
  184.     $Call DosWrite,stdout,offset DosErrOpts,LENGTHOF DosErrOpts,offset nwritten
  185.     $Call DosRead,stdin,offset DosReadErr,1,offset nwritten
  186.     mov    al,DosReadErr
  187. .IF     al == 'q' || al == 'Q'
  188.      $DosExit
  189. .ENDIF
  190. exitDEM:
  191. ENDM
  192.  
  193. $WinDebugMessage MACRO arg      ;Display Message = arg # in eax before call
  194.     pusha
  195.     $DWordToAscii eax,DW_Buf
  196.     $Alarm
  197.     $ConcantS1andS2ToS3 DW_Buf,arg,Concanted
  198.     $Call WinMessageBox,HWND_DESKTOP,HWND_DESKTOP,offset Concanted,offset msgInfo,0,MB_ENTER    ;msgBoxErrStyle
  199.     popa
  200. ENDM
  201.  
  202. ;Error Message for PM applications - Pass the Function name as String in ""
  203. $WinErrMsg MACRO FuncName      ;sound alarm and display error box
  204.     local   msgFuncName
  205.     $Call WinGetLastError,hab    ;gets error number for code
  206.     IFNDEF  FuncName
  207.       .DATA
  208.        msgFuncName   BYTE FuncName
  209.     ENDIF
  210.    .CODE
  211.     $DWordToAscii eax,DW_Buf
  212.     $Alarm
  213.     $ConcantS1andS2ToS3 DW_Buf,msgFuncName,Concanted
  214.     $Call WinMessageBox,HWND_DESKTOP,HWND_DESKTOP,offset Concanted,NULL,0,msgBoxErrStyle
  215. ENDM
  216.  
  217. IFDEF NUMBUFS
  218. ;---------- Binary -> Ascii and Ascii -> Binary Conversions  ---------------
  219. ;Is Passed buffer name - tests to see if all characters in buffer are digits
  220. ;If not returns error message and aborts else returns number in eax
  221. ;skips over leading and trailing spaces
  222. $GetNumDigits MACRO  digbuf
  223.     push     esi
  224.     push     edi
  225.     mov      esi,offset digbuf
  226.     mov      edi,0
  227.     .WHILE 1
  228.         .WHILE byte ptr [esi] == 32    ;skip over leading spaces
  229.             inc esi
  230.         .ENDW
  231.         .IF  byte  ptr [esi] >= 30h
  232.             .IF byte ptr [esi] <= 39h
  233.                 inc edi
  234.                 inc esi
  235.              .ELSE
  236.                 .BREAK
  237.             .ENDIF
  238.          .ELSE
  239.             .BREAK
  240.          .ENDIF
  241.     .ENDW           ;on exit edi has number of bytes read
  242.     .IF edi == 0    ;no valid input
  243.          $DosBeep 500,500
  244.          IFDEF NOWIN    ;not a PM application
  245.              $NewLine
  246.              $Call DosWrite,"Invalid Input for ASCIIToDWord - Aborting"
  247.              $NewLine
  248.              $DosExit
  249.          ELSE
  250.              mov   eax,0
  251.              $WinDebugMessage
  252.              call ExitWin
  253.              $DosExit
  254.          ENDIF
  255.     .ENDIF
  256.     mov    eax,edi
  257.     pop    edi
  258.     pop    esi
  259. ENDM
  260.  
  261. ;Convert DWORD to ASCII string at 10 BYTE buffer at digits
  262. ;Number to convert moved into EAX, esi points to buffer
  263. $DWordToAscii MACRO  num,buf
  264.     pusha
  265.     mov      eax,num
  266.     mov      esi,offset buf
  267.     call     DWordToAscii
  268.     popa
  269. ENDM
  270.  
  271. ;Convert DWORD to Hex string at 10 BYTE buffer at digits
  272. ;Number to convert moved into EAX, esi points to buffer
  273. $DWordToHex MACRO  num,buf
  274.     pusha
  275.     mov      eax,num
  276.     mov      esi,offset buf
  277.     call     DWordToHex
  278.     popa
  279. ENDM
  280.  
  281. ;convert string representing BYTE in BUFF to numeric DWORD
  282. ;result returned in EAX)
  283. $AsciiToDWord  MACRO  Buff
  284.     $GetNumDigits Buff  ;checks on validity of characters in buffer as well
  285.     mov      lth,eax
  286.     push     ebx
  287.     push     ecx
  288.     push     edx
  289.     push     edi
  290.     push     esi
  291.     mov      esi,offset Buff
  292.     call     AsciitoDWord
  293.     pop      esi
  294.     pop      edi
  295.     pop      edx
  296.     pop      ecx
  297.     pop      ebx
  298. ENDM
  299.  
  300. ;Convert BYTE to Binary Bit String
  301. ;Byte to convert in AL
  302. $ByteToBitString MACRO
  303.     pusha
  304.     mov     edi,offset Bit8Str
  305.     call    ByteToBitString
  306.     popa
  307. ENDM
  308.  
  309. ;Byte to convert in AX
  310. $WordToBitString MACRO
  311.     pusha
  312.     mov     edi,offset Bit16Str
  313.     call    WordToBitString
  314.     popa
  315. ENDM
  316.  
  317. ;----------- Procedures here ---------------
  318.  
  319. .CODE
  320. ;--- Call with esi pointing to buffer to store digit string
  321. DWordToAScii  Proc
  322.     mov      edi,10
  323.     xor      edx,edx
  324.     mov      ecx,10
  325. d0: div      edi
  326.     add      edx,30h
  327.     push     edx          ;save on stack
  328.     xor      edx,edx
  329.     loop     d0           ;on exit top of stack has first digit, etc.
  330.     mov      ecx,10
  331. d1: pop      edx
  332.     mov      [esi],dl     ;esi has address of buffer
  333.     inc      esi
  334.     loop     d1
  335.     ; now get rid of leading 0's
  336.     sub      esi,10       ;back to start of string
  337.     mov      ecx,10
  338.     .WHILE   byte ptr [esi] == "0"
  339.          mov byte ptr [esi]," "
  340.          inc esi
  341.          dec ecx
  342.     .ENDW
  343.     .IF  cx == 0          ; all spaces
  344.          dec esi
  345.          mov byte ptr [esi] ,'0'
  346.     .ENDIF
  347.      ret
  348. DWordToAscii   endp
  349.  
  350. AsciiToDWord  proc
  351.     mov      eax,1
  352.     xor      edi,edi
  353.     xor      ebx,ebx
  354.     mov      ecx,lth
  355.     dec      lth
  356.     add      esi,lth       ;offset is lnth -1 from 0
  357. w0: push     eax
  358.     sub      byte ptr [esi],30h
  359.     mov      bl,[esi]
  360.     mul      bl            ;result in EAX
  361.     add      edi,eax
  362.     pop      eax
  363.     mul      ten        ;10 x previous value in AX now
  364.     dec      esi
  365.     loopd    w0
  366.     mov      eax,edi
  367.     ret
  368. AsciiToDWord  endp
  369.  
  370. ;--- Call with esi pointing to buffer to store digit string
  371. DWordToHex  Proc
  372.     mov      edi,16
  373.     xor      edx,edx
  374.     mov      ecx,8
  375. d0: div      edi
  376.     .IF      edx <= 9
  377.         add      edx,30h
  378.     .ELSE
  379.         add      edx,55   ;use caps
  380.     .ENDIF
  381.     push     edx          ;save on stack
  382.     xor      edx,edx
  383.     loop     d0           ;on exit top of stack has first digit, etc.
  384.     mov      ecx,8
  385. d1: pop      edx
  386.     mov      [esi],dl     ;esi has address of buffer
  387.     inc      esi
  388.     loop     d1
  389.     mov      byte ptr [esi],'H'
  390.      ret
  391. DWordToHex   endp
  392.  
  393. WordToBitString  proc  ;called with edi set to offset of string and # in ax
  394.     mov     cx,16
  395.     dec     edi
  396. wb0:inc     edi
  397.     shl     ax,1             ;move most significant bit to carry flag
  398.     jc      wb1              ;if set copy '1'
  399.     mov     byte ptr [edi],'0'       ;else copy '0'
  400.     jmp     wb2
  401. wb1:mov     byte ptr [edi],'1'
  402. wb2:loop    wb0
  403.     ret
  404. WordToBitString endp
  405.  
  406. .CODE
  407. ByteToBitString  proc        ; al has the number
  408.     mov     cx,8             ;called with edi set to offset of string
  409.     dec     edi
  410. bb0:inc     edi
  411.     shl     al,1             ;move most significant bit to carry flag
  412.     jc      bb1              ;if set copy '1'
  413.     mov     byte ptr [edi],'0'       ;else copy '0'
  414.     jmp     bb2
  415. bb1:mov     byte ptr [edi],'1'
  416. bb2:loop    bb0
  417.     ret
  418. ByteToBitString  endp
  419.  
  420. ENDIF
  421.  
  422.