Announcement

Collapse
No announcement yet.

WaitForSingleObject - pass what handle in WinMain?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • WaitForSingleObject - pass what handle in WinMain?

    'Can't get WaitForSingleObject to work.
    'What handle is passed in main thread?

    Code:
    'handle.bas
    #DIM ALL
    #COMPILE EXE
    DECLARE FUNCTION wait(hThread AS DWORD) AS STRING
    #INCLUDE "win32api.inc"
    FUNCTION WINMAIN( _
                BYVAL  hInstance    AS DWORD, _
                BYVAL  hPrevInst    AS DWORD, _
                BYVAL  lpszCmdLine  AS ASCIIZ PTR, _
                BYVAL  nCmdShow     AS LONG ) AS LONG
     
      LOCAL zBuffer AS ASCIIZ * %MAX_PATH
      LOCAL s AS STRING
      LOCAL dw AS DWORD
      LOCAL h AS LONG
      LOCAL result AS LONG
     
      h = %HWND_DESKTOP
      dw = WaitForSingleObject(h, %INFINITE)
      result = GetLastError()
      FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, Result, %NULL, zBuffer, SIZEOF(zBuffer), BYVAL %NULL
      MSGBOX zBuffer
     
     
      'This works
      MSGBOX "Test MSGBOX"
      Result = GetLastError()
      FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, Result, %NULL, zBuffer, SIZEOF(zBuffer), BYVAL %NULL
      MSGBOX zBuffer
      FUNCTION = 1
     
    END FUNCTION
    How long is an idea? Write it down.

  • #2
    It's not very clear to me what you want WaitForSingleObject to do exactly... Wait on Windows desktop shutdown?
    Regards,
    Peter

    Comment


    • #3
      Correct. It should not return 6.
      What handle is passed to WaitForSingleObject?
      Last edited by Mike Doty; 20 Dec 2007, 10:57 PM.
      How long is an idea? Write it down.

      Comment


      • #4
        Code:
        hDeskTop = GetDesktopWindow()
        Regards,
        Peter

        Comment


        • #5
          >Correct. It should not return 6.
          Code:
          %ERROR_INVALID_HANDLE            = 6
          Yes, it should return "6" because you can't WFSO on a window handle, making "h" an "invalid handle" for this function.

          If you want your program to "do something" on shutdown or logoff, you should intercept the WM_QUERYENDSESSION and/or WM_ENDSESSION notification messages and react to one and/or the other.
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            FWIW:

            The WaitForSingleObject function can wait for the following objects:
            • Change notification
            • Console input
            • Event
            • Job
            • Memory resource notification
            • Mutex
            • Process
            • Semaphore
            • Thread
            • Waitable timer
            Also FWIW, in PB/CC you can intercept a shutdown or logoff by using SetConsoleCtrlHandler and 'doing something' on either CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT
            Last edited by Michael Mattias; 21 Dec 2007, 08:34 AM.
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              This code works in batch files so output can be paused before exiting.
              Why does command prompt come back instantly with SLEEP 4000?
              Code:
              #COMPILE EXE  "c:\misc\sleep.exe"   'in path
              FUNCTION PBMAIN AS LONG
                SLEEP VAL(COMMAND$)
              END FUNCTION
              Never mind, Start/Wait 4000 works.
              How long is an idea? Write it down.

              Comment

              Working...
              X