CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    10

    Passing CWnd and command line?

    In my application I launch a thread with AfxBeginThread. The data I want to pass to this thread is a pointer to the owner and the command line of the owner process.

    My header file for the thread process looks like:


    UINT RunTribes(LPVOID pParam);

    typedef struct _THREAD_DATA {
    CWnd* pParent;
    LPTSTR cCmdLine;
    } THREAD_DATA, *PTHREAD_DATA;




    In my parent process I create and set the parameters of a variable, then launch the thread. Like so:


    PTHREAD_DATA pParamData;

    pParamData->pParent = this;
    pParamData->cCmdLine = GetCommandLine();
    AfxBeginThread(RunTribes, pParamData);




    My actual thread looks like this:


    UINT RunTribes(LPVOID pParam)
    {
    PTHREAD_DATA pThreadData = (PTHREAD_DATA) pParam;
    char *szCmdLine = "tribes.exe ";
    LPTHREADINFO pThreadInfo = new THREADINFO;
    CEvent *pThreadEvent = new CEvent(FALSE, TRUE);

    strcat(szCmdLine, pThreadData->cCmdLine);

    ASSERT_VALID(pThreadEvent);
    if(pThreadInfo)
    {
    pThreadInfo->pTermThreadEvent = pThreadEvent;
    pThreadInfo->strPathName = szCmdLine;

    AfxBeginThread(LaunchAndWait, pThreadInfo);
    WaitForSingleObject(pThreadEvent->m_hObject, INFINITE);
    }
    pThreadData->pParent->PostMessage(WM_CLOSE, 0, 0L);

    AfxEndThread(0);
    return 0;
    }




    The problem is I get an access violation in my parent process when setting the "this" property. Can someone help me on this?

    I have successfully passed the parent pointer without the typedef(I didn't have command line data), but I don't know how I pass both of them to the process.


  2. #2
    Join Date
    Apr 1999
    Posts
    48

    Re: Passing CWnd and command line?

    The access violation is because you haven't allocated any memory using new for the pointer pParamData so it faults when first used.

    The
    strcat(szCmdLine, pThreadData->cCmdLine);
    is also a potential crash site since you're copying over a constant string. You might get away with it if the new string is not longer, but it's not a good move.

    Passing CWnd's between threads can fail because the map which MFC uses to convert CWnds to HWNDs is thread specific. Passing HWNDs is safer.
    Also I'm not sure why you need to post a message back to the original process to have it pop off. Couldn't it just wait until the handle of the thread it created goes signalled and then exit?

    Hope that helps a bit. I'm in the GMT+1 time zone so have to sign off now for my (cough) beauty sleep.

    Cheers,
    Roger.





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured