Hi All!
I am having a problem in obtaining the handle of a particular accessible object using WindowFromAccessibleObject.
First, let me show you my code:
private static string GetWindow(IAccessible iAccessibleControl)
{
try
{
IAccessible accWindow = iAccessibleControl;
while((AccessibleRole)accWindow.get_accRole(0) != AccessibleRole.Window)
{
accWindow = (IAccessible) accWindow.accParent;
}
IntPtr hwnd = IntPtr.Zero;
IntPtr result = OleAcc.WindowFromAccessibleObject(accWindow, out hwnd);
if(result.ToInt32() == 0)
{
return hwnd.ToString();
}
else
{
if((AccessibleRole)accWindow.get_accRole(0) == AccessibleRole.Window)
{
return "Window but no handle.";
}
else
{
return "No handle.";
}
}
}
catch(Exception err)
{
return err.Message;
}
}
What this function do is that it checks for the control passed if it is a window. If not, it searches nearest window in the hierarchy. Then, it returns the window handle.
There is no problem if the parameter passed is already a window but if it is not, it does not return any handle although it has obtained the nearest window.
Please help.
Thanks and regards,
Mike

Problem in getting handle using WindowFromAccessibleObject
BrianStevens
Hi Mike,
It seems that the problem is more related with how the WindowFromAccessibleObject works.
I think it would better post this question in the newsgroup below.
microsoft.public.accessibility.developer
We recommend posting appropriately so you will get the most qualified pool of respondents, and so other partners who regularly read the newsgroups can either share their knowledge or learn from your interaction with us.
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
japieson
Hi!
I have tried converting it to C++. The result was when I call AccessibleObjectFromPoint to a control that has no handle (based on the previous C# code), there was no accessible object returned. Instead, an error spits right into my face saying "CoIntialize has not been called". This is even worse than the C# equivalent since the previous code at least returns a valid accessible object. Only the problem was its handle can not be retrieved. So, I can view its other properties. Now, I can not view everything.
I don't know what's the root cause of this problem. Hope you could help me out.
Thanks and regards,
Mike
Jonathan Brown
Hi Mike,
Thanks for your reply.
It seems that the microsoft.public.accessibility.developer has been removed althought we can still find some information in the newsgroup in google.
Anyway the microsoft.public.win32.programmer.ole is also an candidate for your issue.
Based my understanding so far, WindowFromAccessibleObject method will not work all the time.
Although you are call WindowFromAccessibleObject from .NET, but in the underlying it will call the Win32 DLL. We only call it through the .NET Wrapper. Because the problem occurred when you call the WindowFromAccessibleObject , but the WindowFromAccessibleObject method is not written by you. .NET will generate a Wrap for it and then the wrapper will call the Win32 DLL.
So if you want to isolate the problem, I suggest you make a simple test with a Win32 Application, e.g. VB6 or C++ to see if the same code will work.
If it works at Win32 but not under .NET, then commonly we can know the problem is in the .NET wrapper, or it the Win32 DLL.
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang
Sumesh Nair
Hi!
Thanks for the reply. I have posted this problem as well in microsoft.public.win32.programmer.ole newsgroups.I can't find microsoft.public.accessibility.developer newsgroups.
Anyway, I've noticed that using the acessible object returned from AccessibleObjectFromPoint as the parameter passed on the WindowFromAccessibleObject gives the correct result as long as the accessible object on that point is a window. If it's not and when it starts searching for the nearest parent window, there goes the problem.
What is wrong with my code
Thanks and regards,
Mike
AlexandruS
Hi Peter!
I have figured out the solution. I used the API function WindowFromPoint after obtaining the correct coordinates of the Accessible Control. I've noticed that this function always returns a good handle so I think this solves my problem.
Regards,
Mike
tonyotter
Hi Mike,
I am not familar with C++, but to use COM Object in C++ we must call CoInitialize first before any COM related code. You may have a try.
Based on my experience, a problem as complex as this may take extensive time to narrow down. If the issue is urgent, you may want to work with Microsoft Customer Service and Support (CSS) for a faster resolution. Once you open a Support incident with Microsoft CSS, a dedicated Support Professional can work with you in a more efficient manner.
If you still have any concern, please feel free to post here.
Best regards,
Peter Huang