So erstellen und registrieren Sie ein Objekt über Comwapper im nativen AOT
Posted: 21 May 2025, 14:26
Es gibt ein Testprojekt, das den Inhalt eines Notizblocks zeigt, in der Perspektive den Inhalt anderer Fensterelemente: < /p>
Exe
net9.0-windows
enable
enable
tlbimp
0
1
944de083-8fb8-45cf-bcb7-c477acb2f897
0
false
true
< /code>
using System.Diagnostics;
using UIAutomationClient;
namespace ConsoleApp5;
internal class Program
{
static void Main(string[] _)
{
var automation = new CUIAutomation();
var process = Process.GetProcessesByName("Notepad").First();
var elm = automation.ElementFromHandle(process.MainWindowHandle);
var condition = automation.CreatePropertyCondition(30003, 50030);
var elm2 = elm.FindFirst(TreeScope.TreeScope_Subtree, condition);
var val = (IUIAutomationValuePattern?)elm2?.GetCurrentPattern(10002);
Console.WriteLine(val?.CurrentValue);
}
}
< /code>
It works. But I need to compile it to native code. If I add:
true
< /code>
I get an exception:
System.NotSupportedException: Built-in COM has been disabled via a feature switch.
< /code>
From the documents I started to rebuild the code:
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace ConsoleApp5;
internal unsafe partial class Program
{
static void Main(string[] _)
{
ComWrappers comWrappers = new StrategyBasedComWrappers();
var automation = new Automation();
???
var process = Process.GetProcessesByName("Notepad").First();
var elm = automation.ElementFromHandle(process.MainWindowHandle);
var condition = automation.CreatePropertyCondition(30003, 50030);
var elm2 = elm.FindFirst(TreeScope.TreeScope_Subtree, condition);
var val = (IUIAutomationValuePattern?)elm2?.GetCurrentPattern(10002);
Console.WriteLine(val?.get_CurrentValue());
}
[GeneratedComClass]
public partial class Automation : IUIAutomation
{
public IUIAutomationCondition CreatePropertyCondition(in int propertyId, in object value)
{
throw new NotImplementedException();
}
public IUIAutomationElement ElementFromHandle(in nint hwnd)
{
throw new NotImplementedException();
}
}
[GeneratedComInterface]
[Guid("30CBE57D-D9D0-452A-AB13-7AC5AC4825EE")]
public partial interface IUIAutomation
{
IUIAutomationElement ElementFromHandle(in IntPtr hwnd);
IUIAutomationCondition CreatePropertyCondition(in int propertyId, in object value);
}
[GeneratedComInterface]
[Guid("D22108AA-8AC5-49A5-837B-37BBB3D7591E")]
public partial interface IUIAutomationElement
{
IUIAutomationElement FindFirst(in TreeScope scope, in IUIAutomationCondition condition);
object GetCurrentPattern(in int patternId);
}
[GeneratedComInterface]
[Guid("352FFBA8-0973-437C-A61F-F64CAFD81DF9")]
public partial interface IUIAutomationCondition
{
}
[GeneratedComInterface]
[Guid("A94CD8B1-0844-4CD6-9D2D-640537AB39E9")]
public partial interface IUIAutomationValuePattern
{
string get_CurrentValue();
}
public enum TreeScope
{
TreeScope_None = 0,
TreeScope_Element = 1,
TreeScope_Children = 2,
TreeScope_Descendants = 4,
TreeScope_Parent = 8,
TreeScope_Ancestors = 16,
TreeScope_Subtree = 7
}
}
< /code>
I have no experience with COM. Help me understand how an object is created and registered.
Do I need to register all interfaces or just IUIAutomation?
How do I implement a method in a class and return a value from it? For example: class Automation - ElementFromHandle.
Exe
net9.0-windows
enable
enable
tlbimp
0
1
944de083-8fb8-45cf-bcb7-c477acb2f897
0
false
true
< /code>
using System.Diagnostics;
using UIAutomationClient;
namespace ConsoleApp5;
internal class Program
{
static void Main(string[] _)
{
var automation = new CUIAutomation();
var process = Process.GetProcessesByName("Notepad").First();
var elm = automation.ElementFromHandle(process.MainWindowHandle);
var condition = automation.CreatePropertyCondition(30003, 50030);
var elm2 = elm.FindFirst(TreeScope.TreeScope_Subtree, condition);
var val = (IUIAutomationValuePattern?)elm2?.GetCurrentPattern(10002);
Console.WriteLine(val?.CurrentValue);
}
}
< /code>
It works. But I need to compile it to native code. If I add:
true
< /code>
I get an exception:
System.NotSupportedException: Built-in COM has been disabled via a feature switch.
< /code>
From the documents I started to rebuild the code:
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace ConsoleApp5;
internal unsafe partial class Program
{
static void Main(string[] _)
{
ComWrappers comWrappers = new StrategyBasedComWrappers();
var automation = new Automation();
???
var process = Process.GetProcessesByName("Notepad").First();
var elm = automation.ElementFromHandle(process.MainWindowHandle);
var condition = automation.CreatePropertyCondition(30003, 50030);
var elm2 = elm.FindFirst(TreeScope.TreeScope_Subtree, condition);
var val = (IUIAutomationValuePattern?)elm2?.GetCurrentPattern(10002);
Console.WriteLine(val?.get_CurrentValue());
}
[GeneratedComClass]
public partial class Automation : IUIAutomation
{
public IUIAutomationCondition CreatePropertyCondition(in int propertyId, in object value)
{
throw new NotImplementedException();
}
public IUIAutomationElement ElementFromHandle(in nint hwnd)
{
throw new NotImplementedException();
}
}
[GeneratedComInterface]
[Guid("30CBE57D-D9D0-452A-AB13-7AC5AC4825EE")]
public partial interface IUIAutomation
{
IUIAutomationElement ElementFromHandle(in IntPtr hwnd);
IUIAutomationCondition CreatePropertyCondition(in int propertyId, in object value);
}
[GeneratedComInterface]
[Guid("D22108AA-8AC5-49A5-837B-37BBB3D7591E")]
public partial interface IUIAutomationElement
{
IUIAutomationElement FindFirst(in TreeScope scope, in IUIAutomationCondition condition);
object GetCurrentPattern(in int patternId);
}
[GeneratedComInterface]
[Guid("352FFBA8-0973-437C-A61F-F64CAFD81DF9")]
public partial interface IUIAutomationCondition
{
}
[GeneratedComInterface]
[Guid("A94CD8B1-0844-4CD6-9D2D-640537AB39E9")]
public partial interface IUIAutomationValuePattern
{
string get_CurrentValue();
}
public enum TreeScope
{
TreeScope_None = 0,
TreeScope_Element = 1,
TreeScope_Children = 2,
TreeScope_Descendants = 4,
TreeScope_Parent = 8,
TreeScope_Ancestors = 16,
TreeScope_Subtree = 7
}
}
< /code>
I have no experience with COM. Help me understand how an object is created and registered.
Do I need to register all interfaces or just IUIAutomation?
How do I implement a method in a class and return a value from it? For example: class Automation - ElementFromHandle.