winfunc.p 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* ===========================================================================
  2. file : WINFUNC.P
  3. by : Jurjen Dijkstra, 1997
  4. mailto:jurjen@global-shared.com
  5. http://www.global-shared.com
  6. purpose : implementation of functions that call windows API procedures
  7. =========================================================================== */
  8. &GLOB DONTRUN-WINFUNC
  9. {windows.i}
  10. /* this CreateProcess function is a simplified version of the
  11. CreateProcess API definition.
  12. Parameters:
  13. 1. Commandline, for example "notepad.exe c:\config.sys"
  14. 2. CurrentDir, is default directory for new process
  15. 3. wShowWindow, 0=hidden, 1=normal, 2=minimized, 3=maximized
  16. r. return if <>0 then handle of new process
  17. if =0 then failed, check GetLastError */
  18. FUNCTION CreateProcess RETURNS INTEGER
  19. (input CommandLine as CHAR,
  20. input CurrentDir as CHAR,
  21. input wShowWindow as INTEGER) :
  22. def var lpStartupInfo as memptr.
  23. set-size(lpStartupInfo) = 68.
  24. put-long(lpStartupInfo,1) = 68.
  25. put-long (lpStartupInfo,45) = 1. /* = STARTF_USESHOWWINDOW */
  26. put-short(lpStartupInfo,49) = wShowWindow.
  27. def var lpProcessInformation as memptr.
  28. set-size(lpProcessInformation) = 16.
  29. def var lpCurrentDirectory as memptr.
  30. if CurrentDir<>"" then do:
  31. set-size(lpCurrentDirectory) = 256.
  32. put-string(lpCurrentDirectory,1) = CurrentDir.
  33. end.
  34. def var bResult as integer.
  35. run CreateProcess{&A} in hpApi
  36. ( 0,
  37. CommandLine,
  38. 0,
  39. 0,
  40. 0,
  41. 0,
  42. 0,
  43. if CurrentDir=""
  44. then 0
  45. else get-pointer-value(lpCurrentDirectory),
  46. get-pointer-value(lpStartupInfo),
  47. get-pointer-value(lpProcessInformation),
  48. output bResult
  49. ).
  50. def var hProcess as integer no-undo.
  51. def var hThread as integer no-undo.
  52. hProcess = get-long(lpProcessInformation,1).
  53. hThread = get-long(lpProcessInformation,5).
  54. /* I am pretty sure you are not interested in hThread
  55. so let's invalidate the handle right now.
  56. This does not mean the thread is terminated, it just
  57. means that Kernel doesn't need to keep the object for US. */
  58. DEF VAR ReturnValue as INTEGER NO-UNDO.
  59. RUN CloseHandle in hpApi(hThread, output ReturnValue).
  60. set-size(lpStartupInfo) = 0.
  61. set-size(lpProcessInformation) = 0.
  62. set-size(lpCurrentDirectory) = 0.
  63. return ( hProcess ).
  64. END FUNCTION.
  65. /* GetLastError returns the Error code, set by the most recently
  66. failed api-call. */
  67. /* PROBLEM : GetLastError will always return 127. The reason is that
  68. Progress will have called some api function AFTER the one you have
  69. called. (29 januari 1998) */
  70. FUNCTION GetLastError RETURNS INTEGER :
  71. def var dwMessageID as integer no-undo.
  72. run GetLastError in hpApi (output dwMessageID).
  73. RETURN (dwMessageID).
  74. END FUNCTION.
  75. /* GetParent returns the hWnd of the parent window */
  76. FUNCTION GetParent RETURNS INTEGER
  77. (input hWnd as INTEGER) :
  78. def var hParent as integer no-undo.
  79. run GetParent in hpApi (hWnd, output hParent).
  80. RETURN (hParent).
  81. END FUNCTION.
  82. /* ShowLastError calls GetLastError and shows the message text in a
  83. alert-box. The Message text is simply only searched in the system
  84. module, using the default language and does not insert any
  85. arguments (like in the P4GL 'substitute' function) */
  86. /* Note: can't work because GetLastError doesn't work with Progress */
  87. FUNCTION ShowLastError RETURNS INTEGER :
  88. def var ErrorId as integer no-undo.
  89. def var txt as char no-undo.
  90. def var TxtLength as integer no-undo.
  91. ErrorId = GetLastError().
  92. txt = fill(" ",300).
  93. run FormatMessage{&A} in hpApi (512 + 4096, /* = FORMAT_MESSAGE_IGNORE_INSERTS
  94. + FORMAT_MESSAGE_FROM_SYSTEM */
  95. 0,
  96. ErrorId,
  97. 0,
  98. output txt,
  99. length(txt),
  100. 0,
  101. output TxtLength).
  102. message txt view-as alert-box error.
  103. RETURN ( ErrorId ).
  104. END FUNCTION.