Native Object Class from WinControl?
Asked Answered
I

2

6

I'm trying to take a WinTable object and cast it to its native object type as such:

CustomControl control = (CustomControl) this.UIMap.UIMainWindow.UICustomControl.NativeElement;

Then I want to treat the resulting CustomControl as I would in the source code for my program like control.DoAThing(), I have already referenced the .dll containing the CustomControl class but the problem is that .NativeElement; is returning a type Object[] rather than an Object like the definition of the function says it should.

Is .NativeElement the way to go or am I misunderstanding its purpose?

UPDATE: I checked the types of the objects in the resulting Object[] and the first one is of type System.__ComObject and the 2nd is System.Int32 but I'm not sure what either of these represent...

Interviewee answered 11/6, 2013 at 15:6 Comment(3)
It looks like you're not getting the managed object, but just the DLL the com control resides in and an integer (intended as a pointer) for the entry point of the object. (which is incidentally also why doing this requires full trust for the immediate caller) My guess is .NativeElement isn't supposed to do what you expect it to. What is CustomControl ? Did you write that class yourself?Fryer
@TimothyGroote No, it is a third-party control. I do have access to the source code however.Interviewee
I'm pretty sure that since Array inherits Object, that Array<Object> aka Object[] is an Object and doesn't really violate the return typeAdriannaadrianne
M
0

It would be helpful if you posted your code for your CustomControl and UICustomControl classes. Based on my vague understanding of your problem, the following might work: try it out and post the results.

object[] native = 
  this.UIMap.UIMainWindow.UICustomControl.NativeElement as object[];
if ((native[0] != null) && (native[0] is IAccessible)) {
    IAccessible a = native[0] as IAccessible;
    if (a is CustomControl)
        CustomControl control = (CustomControl)a;
}
Magnifico answered 19/6, 2013 at 13:38 Comment(2)
What guarantee do you have that the first item in the array is even defined, let alone the object you're looking for?Fryer
I have already tried this before, the result was an error message saying that an object reference was not set aka IAccessible a = native[0] as IAccessible; didn't work. I don't think posting the custom control source code is relevant in this case, theoretically my issue could be applied to any control including default Windows Forms controls.Interviewee
G
0

NativeElement gets the object used by the underlying Automation Technology.

If you're using WinForms, NativeElement will return an object[] where the fist index is the IAccessible. I believe any other values will be childids for MSAA use but I've never used them personally.

public static IAccessible AsIAccessible(this WinControl control)
{
    var native = control.NativeElement as object[];
    return native[0] as IAccessible;
}

If you're using WPF, NativeElement will return an AutomationElement.

public static AutomationElement AsAutomationElement(this WpfControl control)
{
    return control.NativeElement as AutomationElement
}

if you're using Web, NativeElement will return the appropriate mshtml interface

public static T AsNativeHtml<T>(this HtmlControl control) where T : HTMLElement
{
    return control.NativeElement as T
}

myDiv.AsNativeHtml<HTMLDivElement>()

However, I do not believe there is a way to get the Native object class without hooking into the target process in some way.

I've tried to make custom wrappers of hawkeye in the past. Which is a ui inspection tool that can get and set native .Net properties. However, I've had limited success due to complexity and time constraints.

Gustatory answered 2/4, 2018 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.