Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing Strings out of Threads

Posted on 2003-12-03
6
Medium Priority
?
311 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 80 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Accepted Solution

by:
SteH earned 120 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

926 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