Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing Strings out of Threads

Posted on 2003-12-03
6
Medium Priority
?
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

722 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