问题:如何在通用Windows平台(UWP)应用程序中创建COM对象?如何在UWP应用程序中创建COM对象? (C#)
动机:我想从WPF切换到UWP。由于我的工作负载需要调用仅可通过COM访问的第三方库(据我所知),因此我需要从UWP进行COM调用。如果仅限于台式机/笔记本电脑
- C#
- .NET
- 的Visual Studio 2015年
- 的Windows 10
- 理想目标的所有UWP设备,但没关系:
语境。
背景
在Visual Studio 2013( “经典桌面” 在Visual Studio 2015项目)中,我使用C#代码
// Conceptual:
DotNetInterface comObjectInstance =
(DotNetInterface)Microsoft.VisualBasic.Interaction.CreateObject(
"this string specified the COM object type"
);
// Example: Open Excel via COM:
Excel.Application oApp = (Excel.Application)Interaction.CreateObject("Excel.Application");
Visual Studio项目所需的参考到Microsoft.VisualBasic
使用Interaction.CreateObject()
和COM对象的类型库。
我想在由Windows 10 Education上的Visual Studio 2015 Enterprise生成的通用Windows平台(UWP)应用程序中使用此C#代码。我能够添加对COM对象的类型库的引用,但由于它未出现在Visual Studio的引用管理器中,因此无法引用Microsoft.VisualBasic
。
思考,试图解决方案,投机等
我添加了一个引用“Windows桌面扩展为UWP”希望它可能使正常的.NET功能,但还没有想出如何使用它呢。
我认为,即使UWP应用程序根本无法进行COM调用,那么我们至少可以构造一个调用常规.NET程序(即使通过网络端口)的包装器,该程序依次能够运行COM调用。由于即使在最坏的情况下,显然可以解决这个问题,所以我觉得应该有一个微软提供的解决方案来制作COM对象。但是我猜想,自从UWP是如此的新颖之后,在线文档相当稀少而且很难找到。
更新#1
找到一个MSDN文章Win32 and COM for Windows Runtime apps and Universal Windows Platform (UWP) apps,即声称的WinRT应用程序(其包括UWP应用)只能使用COM对象的子集。 MSDN建议使用受支持的COM API元素或从不受支持的COM API迁移到功能性替换。
我能找到这篇文章,通过搜索一个运行时错误后,我找到一种方法来COM调用我的第三方库。错误:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Creating an instance of the COM component with CLSID {[edit: GUID removed]} using CoCreateInstanceFromApp failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). Please make sure your COM object is in the allowed list of CoCreateInstanceFromApp.
我仍然不确定是否有内置的方式来访问我的第三方库的COM API。如果没有,这可能意味着我将不得不使用网络端口或其他东西来制作自己的包装,这似乎是错误的。
对于企业级应用程序而言,您是对的,因此无法使用该应用商店并不重要。感谢链接到Brokered Windows运行时组件 - 这正是我一直在寻找的! – Nat
我已为通用Windows应用程序的代理组件创建了一个模板。在这里查看我的帖子https://xamltips.wordpress.com/2015/11/13/brokered-component-for-uwp-on-windows-10/ – Lance