Solved

Passing Strings out of Threads

Posted on 2003-12-03
6
304 Views
Last Modified: 2013-11-20
Hi

I want to pass a string back from a worker thread but can;t find any way of doing it.

I want to send a message from the thread to a dialog window and use the WPARAM
value for passing back the string but it seems that the WPARAM must be a DWORD or int
or something - or have I mis-interpreted here.

Can anyone suggest a method of doing this?

Thanks in advance

LittlePerson
0
Comment
Question by:LittlePerson
  • 3
  • 2
6 Comments
 
LVL 11

Assisted Solution

by:KurtVon
KurtVon earned 20 total points
ID: 9867221
If the worker thread is in the same process space (and it probably is) you can just pass a pointer to the string in the WPARAM instead:

   ::SendMessage(hWnd, WM_MYMESSAGE, (WPARAM)&strText, 0);

and get it as

LRESULT HandleMyMessage(WPARAM wParam, LPARAM lParam)
{
    char* strText = (char*)wParam;

    // Do whatever with strText.
}

The trick is you need to keep the variable alive and unchanged in the worker thread until the message returns.  If you use a PostMessage you will probably want to allocate memory for the string inteh worker thread, and then release it in the message handler.

Hope this helps.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9867246
CString * csMess;
*csMess = "your return value";

pMainWnd->PostMessage (ID, (WPARAM) csMess, yourLParam);

and don't forget to delete the CString pointer in the message handler of the dialog.

You can't post an object with is located on the stack. Therefore use a pointer to an object allocated on the heap.  That pointer needs to be type casted to WPARAM. Both a pointer and WPARAM are 32bits. Since the string is on the heap you need to delete it when the message handler in the dialog has used it. Only the handler knows when the memory is no longer used; not the posting thread.

0
 

Author Comment

by:LittlePerson
ID: 9867448
CString * csMess;
*csMess = "your return value";

Hi

Iam getting problems with the CString initialisation

without using new I get an access violation - no surprise

but with using new I get follwoing compile message

initialization of 'msg' is skipped by 'case' label
        see declaration of 'msg'

How should this be declared???

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
SteH earned 30 total points
ID: 9867547
it seems that you are using CString inside a switch statement like:

switch (i) {
   case 0:
      break;
  case 1:
     CString * csMess = new CString;
     ...
     break;
}

In this case you are declaring a local variable which is not known in all cases. A simple workaround is to use parenthesis:
switch (i) {
   case 0:
      break;
  case 1:
     {  // CString is only valid inside this parenthesis.
         CString * csMess = new CString;
        ...
     }
     break;
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 9867561
Or you can declare it ouside:

  CString * csMess;
 switch (i) {
   case 0:
      break;
  case 1:
     csMess = new CString;
     csMess = "test";
     PostMessage (...);
     break;
}
0
 

Author Comment

by:LittlePerson
ID: 9867646
That works a treat, thanks very much.

I had been passing my serial port object to the worker thread but the serial object is already
threaded and I knew I was walking into trouble. Not any more!


LittlePerson
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now