How to get a HWND for existing window? (Windows-only)

LiamStaskawicz's icon

Hi there - in doing a little Windows-specific coding, I'm having trouble determining how to get a HWND window handle for the existing window in Max - as opposed to creating a new window for a GUI object. It seems like I'd want to use wind_gethwnd(), but I don't have a reference to any t_wind instances. Is there a way to get a reference to a t_wind for an existing window?

Are the window routines in the Max API designed to be used with a GUI object creating its own window? If my object is not of the GUI persuasion, what are my options?

Ultimately, I'm looking to grab a Windows message via WindowProc(), and saw the bit in the guide regarding "Subclassing the Window". Is there any example code out there in the world doing this? I suppose it would also ideally show how to properly pass on the messages that aren't used...

Thanks!

Olaf Matthes's icon

Liam Staskawicz wrote:
> Hi there - in doing a little Windows-specific coding, I'm having trouble determining how to get a HWND window handle for the existing window in Max - as opposed to creating a new window for a GUI object. It seems like I'd want to use wind_gethwnd(), but I don't have a reference to any t_wind instances. Is there a way to get a reference to a t_wind for an existing window?
>
> Are the window routines in the Max API designed to be used with a GUI object creating its own window? If my object is not of the GUI persuasion, what are my options?
>
> Ultimately, I'm looking to grab a Windows message via WindowProc(), and saw the bit in the guide regarding "Subclassing the Window". Is there any example code out there in the world doing this? I suppose it would also ideally show how to properly pass on the messages that aren't used...

This is one way of getting the window handle, but it only works when
getting called then you patcher window is already there:

Max Patch
Copy patch and select New From Clipboard in Max.

// get our patcher window, this must be in our instance creation function
x->x_patcher = (t_patcher *)(gensym("

That's the one I use now in order to subclass it.

Olaf

LiamStaskawicz's icon

Hi Olaf - thanks for the tip. Where is main_get_client() documented? I can't find it anywhere in the C74 docs.

In any event, I tried slapping it in there and it sure returns something but in my case, I'm trying to register for a device notification using the Win32 API call RegisterDeviceNotification( ), and it inevitably returns nothing, with a GetLastError( ) code of 13, which corresponds to ERROR_INVALID_DATA. I wonder if there's anything about the handle that main_get_client() returns that would be unsuitable for this?

My code looks a bit like:
winId = main_get_client();

*hDevNotify = RegisterDeviceNotification( winId, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE );
if(!*hDevNotify)
{
post( "RegisterDeviceNotification failed: %ldn", GetLastError());
return false;
}

Thanks again for your help.

Olaf Matthes's icon

Liam Staskawicz wrote:
> Hi Olaf - thanks for the tip. Where is main_get_client() documented? I can't find it anywhere in the C74 docs.
>
> In any event, I tried slapping it in there and it sure returns something but in my case, I'm trying to register for a device notification using the Win32 API call RegisterDeviceNotification( ), and it inevitably returns nothing, with a GetLastError( ) code of 13, which corresponds to ERROR_INVALID_DATA. I wonder if there's anything about the handle that main_get_client() returns that would be unsuitable for this?
>
> My code looks a bit like:
> winId = main_get_client();
>
> *hDevNotify = RegisterDeviceNotification( winId, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE );
> if(!*hDevNotify)
> {
> post( "RegisterDeviceNotification failed: %ld
> ", GetLastError());
> return false;
> }
>
> Thanks again for your help.

Hmm, I did the same this as well some weeks ago and got the same error
messages. Then I started to open my own (invisible) child window so I
got my own window halde routine but still got the same error message
when calling RegisterDeviceNotification().
However, all the other stuff you can do with windows was working, so
there might be something wrong wth calling RegisterDeviceNotification()
in this situation at all. - Sorry I'm clueless.

Olaf

LiamStaskawicz's icon

Well damn. That's frustrating. But very good to know that I'm not the only one - thanks Olaf.

Don't suppose any C74 authorities could shed any light on the issue? Thanks!