Link to home
Start Free TrialLog in
Avatar of brosenb0
brosenb0Flag for Australia

asked on

Need to Find the TWindow that owns a HWND

I'm converting an app from MFC 4.0 to OWL 5 and need to know how to do the following: -

MFC provides a static member function of the CWnd class that returns a pointer to the CWnd object that is attached to a specific HWND.  If no object is attached, NULL is returned.  This is useful when the Cwnd (TWindow ?) object does not directly create the actual window, but rather is passed a handle to an already existing window and you want to verify that the handle is not already assigned to an object.

 Its declaration is: -
static CWnd * CWnd::FromHandlePermanent(HWND hWnd)

Is there a similar functionality within OWL ?  I've gone over the TWindow class and can't seem to find anything similar.

Thanks.
Avatar of duneram
duneram

I am not aware of a way to do this in OWL, but if you wanted to find the child windows for a parent you could use the EnumChildWindows api.
Another way to do this is

You have a  HWND for a window but don't know which TWindow it is tied to, right?

Well if you have a list of TWindows, you could always traverse the list until the hwnd values matched....




ASKER CERTIFIED SOLUTION
Avatar of duneram
duneram

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
Avatar of brosenb0

ASKER

There's a couple of small problems: -

1) What if I don't have a list of TWindows and need to build that first ?

2) I can't find a method TWindow::AttachHandle(HWND).

3) TWindow::Init is protected and is meant to be called by a derived class's constructor, which basically boils down to a whole lot more coding.  It also means the HWND has to be available when the TWindow is created.

From what I can see OWL is not flexible enough to do the job without a substantial amount of work, so I will recommend that we stick with MFC.  At least we can use the Borland (Inprise ?) compiler to build MFC based code and won't have to wait for several weeks for it to compile.

Thanks anyway.
the attachhandle is part of the TWindow Class, it calls Init  I found it in window.cpp  I will verify this in a few hours as I am upgrading my disks and my compilers are temporarily not available on the computer (the disk has been removed temporarily).

If you do a find and hunt for files containing attachhandle you will see it.  

Attach handle is there.  all you have to do is call it.
here is some extracted code from window.cpp:

void
TWindow::AttachHandle(HWND handle)
{
  ClearFlag(wfDetached);
  ::FreeInstanceThunk(Thunk);
  Thunk = 0;
  Init(handle, GetModule());
}

and this is the Init you end up calling:

void
TWindow::Init(HWND handle, TModule* module)
{
}

Borland originally added this for Delphi/Owl applications...

I found AttachHandle in the window.cpp file in the OWL source dir and it looks like it will do the job.  The only problem I'm left with is finding the TWindow *.  The reason I need to be able to do this is because I'm writing a DLL that has to provide a callback function  for the Windows C interface.  Windows passes a HWND which I have to associate with a TWindow.  There is a protected TWindow member function,
TWindow * GetWindowPtr(HWND), however, it calls the TApplication Object (I don't have one because its a DLL).  In any case I can't call it with any old TWindow * because its protected.  Perhaps I could over-ride the access specifier in a derived class eg.

class derived : public TDialog
{
public:
    TWindow * TWindow::GetWindowPtr(HWND)  
}

I'm not sure if this is legal.  Any thoughts ?

Thanks for the assistance.