Conversion from LPVOID to HANDLE[] Win32/c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    Conversion from LPVOID to HANDLE[] Win32/c++

    Hi all,

    Code:
    HANDLE hnd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, (LPVOID)handleArray, 0, &dwThread);
    
    ...
    
    DWORD ThreadProc(LPVOID lpParameter)
    {    
    
    HANDLE hArray[NUM_EVENTS];
    hArray = (HANDLE)&lpParameter;
    
    ...
    Error is cannot convert from HANDLE to HANDLE[1]. I know it has something to do with the cast that I have there on that second line of the ThreadProc but, for the life of me, I cannot figure out how to get it right.

    Thanks in advance!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Code:
    /* I am guessing this */
        HANDLE handleArray[NUM_EVENTS];
    
        HANDLE hnd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, 
    /* At this point we have a pointer of type HANDLE * and 
       cast it to LPVOID */
                       (LPVOID)handleArray, 0, &dwThread);
    
    ...
    
    DWORD ThreadProc(LPVOID lpParameter)
    {    
    
        HANDLE hArray[NUM_EVENTS];
    
    /* 
      1.  hArray is of type HANDLE [NUM_EVENTS], i.e. an array 
          but the cast is to type HANDLE
      2.  lpParameter contins a HANDLE * but you take a pointer(&) to 
          it which will just be a pointer to somewhere random on the 
          stack not a pointer to the original array.  The & is not required.
      3.  Attemping to assign a pointer to an array is illegal
    */
        hArray = (HANDLE)&lpParameter;
    
    ...
    2 options

    1. In the thread use a HANDLE pointer and then just use it like an array
    Code:
    DWORD ThreadProc(LPVOID lpParameter)
    {    
    
        HANDLE *hArray;
        hArray = (HANDLE)lpParameter;
    ...
    }
    2. In the thread copy the handles from the pointer to the array
    Code:
    DWORD ThreadProc(LPVOID lpParameter)
    {    
        HANDLE hArray[NUM_EVENTS];
    
        memcpy((void *)hArray, lpParameter, sizeof hArray);
    ...
    }

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      You know I think I got confused because in that Thread Proc I am using a method called MsgWaitForMulti pleObjects which needs an array of object handles. I'll go with option 1 since it's simple.

      Thanks for the help!

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by Banfa
        2 options

        1. In the thread use a HANDLE pointer and then just use it like an array
        Code:
        DWORD ThreadProc(LPVOID lpParameter)
        {    
        
            HANDLE *hArray;
            hArray = (HANDLE)lpParameter;
        ...
        }
        That is almost right...still a type mismatch

        Code:
        DWORD ThreadProc(LPVOID lpParameter)
        {    
        
            HANDLE *hArray;
            hArray = (HANDLE*)lpParameter;
        ...
        }
        No worries, compiler caught it straight away and complained about it. Easy fix too. :D

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          So? Even experts like me can still make typing mistakes :D

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Sure, sure... "a typing mistake" LOL :D

            Comment

            Working...