CreateThread and hwnd

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

}
LVL 27
PberSolutions ArchitectAsked:
Who is Participating?
 
jkrCommented:
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
 
jkrCommented:
That should be

HWND hDlgCombo=(HWND*)lpParam;
0
 
AxterCommented:
Hi Pber,
> >HWND hDlgCombo=*(HWND*)lpParam;

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


David Maisonave :-)
Cheers!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
grg99Commented:
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
 
AxterCommented:
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
 
jkrCommented:
>>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
 
AxterCommented:
>>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
 
AxterCommented:
Disregard my previous post.

jkr,
You're right, there is a difference.
0
 
PberSolutions ArchitectAuthor Commented:
Thanks guys, works like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.