Link to home
Start Free TrialLog in
Avatar of Pber
PberFlag for Canada

asked on

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

}
Avatar of jkr
jkr
Flag of Germany image

That should be

HWND hDlgCombo=(HWND*)lpParam;
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Pber,
> >HWND hDlgCombo=*(HWND*)lpParam;

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


David Maisonave :-)
Cheers!
Avatar of grg99
grg99

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?

.

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?
>>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!
>>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


Disregard my previous post.

jkr,
You're right, there is a difference.
Avatar of Pber

ASKER

Thanks guys, works like a charm.