我尝试了很多方法在WinUI3中使用OCX,但没有办法,我试图在控制台中找到可执行代码,但只有一种方法使用System.Windows.Forms。由于 System.Windows.Forms 在 WinUI3 中不可用,我使用 Win32 API 尝试了下面的代码。
internal class Program
{
[DllImport("ole32.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern object CoCreateInstance([In] ref Guid clsid, [MarshalAs(UnmanagedType.Interface)] object punkOuter, int context, [In] ref Guid iid);
static void Main()
{
var guid1 = new Guid("A1574A0D-6BFA-4BD7-9020-DED88711818D");
var guid2 = new Guid("00000000-0000-0000-C000-000000000046");
var a = CoCreateInstance(ref guid1, null, 1, ref guid2);
var b = a as KHOpenAPILib.KHOpenAPI; // KHOpenAPILib.KHOpenAPI is an interface on the dll automatically created by the visual studio with tlbimp when adding com references.
b.CommConnect(); // E_UNEXPECTED
}
}
然而,如注解中所写,出现了E_UNEXPECTED。你能告诉我原因或任何其他方式吗?