?
Solved

CreateThread and hwnd

Posted on 2005-04-18
9
Medium Priority
?
964 Views
Last Modified: 2008-01-09
I need to pass a handle to a control to a thread. This is what I've come up with.  The problems is in my, hDlgCombo doesn't receive the hDlg handle.   How do I pass the handle to the window to the thread and have the called function interpret it properly?

hThread = CreateThread(
      NULL,                        // default security attributes
      0,                           // use default stack size  
      MyProc,                      // thread function
      hDlg,                        // argument to thread function (Handle to a windows control)
      0,                           // use default creation flags
      &dwThreadId);                // returns the thread identifier

DWORD WINAPI MyProc(LPVOID lpParam)
{
  HWND hDlgCombo=*(HWND*)lpParam;
 
  // my processing

}
0
Comment
Question by:Pber
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13806611
That should be

HWND hDlgCombo=(HWND*)lpParam;
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13806619
Sorry,

HWND hDlgCombo=(HWND)lpParam;

Or in total

hThread = CreateThread(
     NULL,                        // default security attributes
     0,                           // use default stack size  
    MyProc,                      // thread function
     (LPVOID)hDlg,                        // argument to thread function (Handle to a windows control)
    0,                           // use default creation flags
     &dwThreadId);                // returns the thread identifier

DWORD WINAPI MyProc(LPVOID lpParam)
{
 HWND hDlgCombo=(HWND)lpParam;

  // my processing

}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13806625
Hi Pber,
> >HWND hDlgCombo=*(HWND*)lpParam;

Do the following:
HWND hDlgCombo=(HWND)lpParam;


David Maisonave :-)
Cheers!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

by:grg99
ID: 13806626
Hold on there, Bucko.  We need a few more details, using words like "of my app", "to some thread of my/some other app".  We need the whole sordid story of what you're trying to do.   Who do these threads and dialogs and windows belong to, and what exactly are you trying to intercept and pass on and to whom?

.

0
 
LVL 30

Expert Comment

by:Axter
ID: 13806925
FYI:
HWND hDlgCombo=*(HWND*)lpParam;
HWND hDlgCombo=(HWND)lpParam;

Both of the above lines are the same from the compiler point of view.

Exactly what type of problems are you having with the code?
And what are you trying to do with the window handle?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13806964
>>Both of the above lines are the same from the compiler point of view.

Not if the input is a HWND. E.g.

int n = 42;
void* p = (void*)n;

int test = *(int*)p; // p == 42, thus test = *(42); --> bang!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13807066
>>Not if the input is a HWND. E.g.

You're above test does not reflect the correct comparison.
You're comparing apples and oranges.


The following is more accurate comparison:
int main(int argc, char* argv[])
{
      int n = 42;
      void* p = (void*)&n;
      
      int test = *(int*)p;
      printf("%i\n", test); //This will print 42


0
 
LVL 30

Expert Comment

by:Axter
ID: 13807183
Disregard my previous post.

jkr,
You're right, there is a difference.
0
 
LVL 26

Author Comment

by:Pber
ID: 13807708
Thanks guys, works like a charm.
0

Featured Post

Industry Leaders: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

850 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