Solved

Passing Strings out of Threads

Posted on 2003-12-03
6
303 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now