Solved

Passing Strings out of Threads

Posted on 2003-12-03
6
305 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 50
Different colored text in ComboBox without Subclassing 8 62
Strange date stored 22 125
sumHeights2  challenge 7 108
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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