tags:

views:

829

answers:

2

If you know an object type, and you have the object's handle (hwnd), is there a way to turn that into a variable?

Example:

Set myObj = Magic(hwnd)

A: 

HWNDs are just numbers that are mapped to internal objects by the OS (and they can and do get recycled). You can't really turn them into anything.

What would you expect myObj to do in your example? All the Windows APIs for manipulating windowss take HWNDs.

jeffamaphone
What I am really trying to do is respond to events in a control in another application. At first I figured I'd just read the message queue (which is what started me down the hwnd road). But then I realized I'd have to put it in some sort of timer and keep reading the queue. So I thought "well if I could get the object I could respond to it's events using withevents".
Oorang
If you want to get the messages for another window, you can subclass the window proc. Use SetWindowLongPtr() with GWLP_WINDOWPROC. It will call your window proc, and you handle what you want and pass the rest to the old window proc. See: http://msdn.microsoft.com/en-us/library/ms633585%28VS.85%29.aspxYou should be able to figure out how to call it from VB.
jeffamaphone
A: 

You may be able to use GetObject() for COM, but it does have limitations -- you can't specify a particular hWnd, for one.

For example, you can get a running Excel instance using GetObject("", Excel.Application), but if there are multiple running instances, you won't know which one will be returned and there is no way to specify.

Jay