Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ERROR_INVALID_HANDLE returned From WaitForSingleObject after CreateMutex

ERROR_INVALID_HANDLE returned From WaitForSingleObject after CreateMutex

Scheduled Pinned Locked Moved C / C++ / MFC
csshelptutorial
15 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F ForNow

    Hi I am getting ERROR_INVALID_HANDLE on GetLastError after WaitForSingleObject returns -1 I more or Less followed the MSDN example Using Mutex My main thread does muthandle = CreateMutex(NULL,FALSE,NULL); Second parameter is FALSE indicating it is not owned Later a second Thread calls WaitForSingleObject(muthandle,INFINITE) and gets a -1 which GetLastError retuns ERROR_INVALID_HANDLE THANKS

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #2

    what does CreateMutex return?

    image processing toolkits | batch image processing

    F 1 Reply Last reply
    0
    • C Chris Losinger

      what does CreateMutex return?

      image processing toolkits | batch image processing

      F Offline
      F Offline
      ForNow
      wrote on last edited by
      #3

      Well it's not NULL some low number like X'00000010' or X'00000030' now running x64 a handle is 64 bits I was looking at the RAX register I tried the last parameter For CreateMutex NULL and with a string didn't matter the createmutex and WaitForSigleIbject are different threads

      1 Reply Last reply
      0
      • F ForNow

        Hi I am getting ERROR_INVALID_HANDLE on GetLastError after WaitForSingleObject returns -1 I more or Less followed the MSDN example Using Mutex My main thread does muthandle = CreateMutex(NULL,FALSE,NULL); Second parameter is FALSE indicating it is not owned Later a second Thread calls WaitForSingleObject(muthandle,INFINITE) and gets a -1 which GetLastError retuns ERROR_INVALID_HANDLE THANKS

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Hi, The function returning the error does not lie. The first parameter you are passing to the WaitForSingleObject function[^] is not a valid HANDLE. If I were a gambling man... I would bet that you are attempting to pass the pointer to a HANDLE. Best Wishes, -David Delaune

        F 1 Reply Last reply
        0
        • L Lost User

          Hi, The function returning the error does not lie. The first parameter you are passing to the WaitForSingleObject function[^] is not a valid HANDLE. If I were a gambling man... I would bet that you are attempting to pass the pointer to a HANDLE. Best Wishes, -David Delaune

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #5

          It's the handle I guess the next step is to go to disassembly mode and see

          L 2 Replies Last reply
          0
          • F ForNow

            It's the handle I guess the next step is to go to disassembly mode and see

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            ForNow wrote:

            go to disassembly mode

            Shouldn't be necessary. You should be able to step through with your debugger to check if the handle is valid. Remember, it could easily have been corrupted by your (or someone's) code.

            F 1 Reply Last reply
            0
            • L Lost User

              ForNow wrote:

              go to disassembly mode

              Shouldn't be necessary. You should be able to step through with your debugger to check if the handle is valid. Remember, it could easily have been corrupted by your (or someone's) code.

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #7

              For some reason i am not able to display certain data areas on quick watch That's the only reason I got intobinto disassembly mode Thanks

              L 1 Reply Last reply
              0
              • F ForNow

                It's the handle I guess the next step is to go to disassembly mode and see

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                ForNow wrote:

                It's the handle I guess the next step is to go to disassembly mode and see

                Since you are such an expert at x86/x64 assembler perhaps you should utilize WinDbg: Common WinDBG Commands Reference[^]

                !handle displays information about a handle or handles that one or all processes in the target system own.

                0:000> !handle c0dedbad f
                Handle 38
                Type Mutant
                Attributes 0
                GrantedAccess 0x1f0001:
                Delete,ReadControl,WriteDac,WriteOwner,Synch
                QueryState
                HandleCount 1
                PointerCount 96605
                Name Object Specific Information
                Mutex is Owned
                Mutant Owner 11bc.1418

                Here are some other ideas in case you don't know how to use WinDbg:

                //Windows 8.1 and below:
                BOOL IsValidHandle(HANDLE h)
                {
                DWORD dwFlags = 0;
                return GetHandleInformation(h,&dwFlags);
                }

                //Windows 10 and above:
                BOOL IsValidHandle(HANDLE h)
                {
                return CompareObjectHandles(h,h);
                }

                use it like this to debug your code:

                if (IsValidHandle(h))
                {
                DWORD dwResult = ::WaitForSingleObject(h, INFINITE);
                DWORD dwError = GetLastError();
                }

                Best Wishes, -David Delaune

                1 Reply Last reply
                0
                • F ForNow

                  Hi I am getting ERROR_INVALID_HANDLE on GetLastError after WaitForSingleObject returns -1 I more or Less followed the MSDN example Using Mutex My main thread does muthandle = CreateMutex(NULL,FALSE,NULL); Second parameter is FALSE indicating it is not owned Later a second Thread calls WaitForSingleObject(muthandle,INFINITE) and gets a -1 which GetLastError retuns ERROR_INVALID_HANDLE THANKS

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #9

                  How are you passing muthandle to your second thread? ...is it possible that you're not passing that correctly (for example, if you passed by pointer that you're not dereferencing your pointer)?

                  F 1 Reply Last reply
                  0
                  • A Albert Holguin

                    How are you passing muthandle to your second thread? ...is it possible that you're not passing that correctly (for example, if you passed by pointer that you're not dereferencing your pointer)?

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #10

                    There is a global block accessible to all the threads This is one of the fields I going to double check that The handle has the value initialized by CreateMutex later in tonite

                    A L 2 Replies Last reply
                    0
                    • F ForNow

                      There is a global block accessible to all the threads This is one of the fields I going to double check that The handle has the value initialized by CreateMutex later in tonite

                      A Offline
                      A Offline
                      Albert Holguin
                      wrote on last edited by
                      #11

                      Since it's a different thread... also check that the thread going to the wait state isn't waiting on a handle that hasn't been created yet.

                      1 Reply Last reply
                      0
                      • F ForNow

                        For some reason i am not able to display certain data areas on quick watch That's the only reason I got intobinto disassembly mode Thanks

                        L Offline
                        L Offline
                        leon de boer
                        wrote on last edited by
                        #12

                        You are inside a thread the debugger doesn't always have access to all the memory.

                        In vino veritas

                        F 1 Reply Last reply
                        0
                        • F ForNow

                          There is a global block accessible to all the threads This is one of the fields I going to double check that The handle has the value initialized by CreateMutex later in tonite

                          L Offline
                          L Offline
                          leon de boer
                          wrote on last edited by
                          #13

                          You do realize you don't pass the handle around between threads right? Read carefully

                          Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.

                          If you go the other way and guarantee the inital thread opens it first you need to set the bInitialOwner flag and do a release. So your other choice.

                          The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.

                          You seem to get getting confused between the two modes from what I am reading. You need to make a choice (a) or (b). So I think you are getting this error because you are using the named technique without a name.

                          If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

                          In vino veritas

                          F 1 Reply Last reply
                          0
                          • L leon de boer

                            You are inside a thread the debugger doesn't always have access to all the memory.

                            In vino veritas

                            F Offline
                            F Offline
                            ForNow
                            wrote on last edited by
                            #14

                            That's it I was never able to display the global block storage with quick watch I think it was build as a DLL on its own Thanks

                            1 Reply Last reply
                            0
                            • L leon de boer

                              You do realize you don't pass the handle around between threads right? Read carefully

                              Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.

                              If you go the other way and guarantee the inital thread opens it first you need to set the bInitialOwner flag and do a release. So your other choice.

                              The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.

                              You seem to get getting confused between the two modes from what I am reading. You need to make a choice (a) or (b). So I think you are getting this error because you are using the named technique without a name.

                              If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

                              In vino veritas

                              F Offline
                              F Offline
                              ForNow
                              wrote on last edited by
                              #15

                              I am modifying the Hercules mainframe emulator code I have created a child Process a Windows MFC Program Basically the data shared between all of the threads is stored in a DLL named sysblk Thanks

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups