如何在 UWP 应用程序中创建 COM 对象?(C#) [英] How to create a COM object in a UWP application? (C#)

查看:26
本文介绍了如何在 UWP 应用程序中创建 COM 对象?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何在通用 Windows 平台 (UWP) 应用程序中创建 COM 对象?

动机:我想从 WPF 切换到 UWP.由于我的工作量需要调用只能通过 COM 访问的第三方库(据我所知),我需要从 UWP 进行 COM 调用.

上下文:

  • C#
  • .NET
  • Visual Studio 2015
  • Windows 10
  • 理想情况下针对所有 UWP 设备,但如果仅限于台式机/笔记本电脑也可以.

背景

在 Visual Studio 2013(Visual Studio 2015 中的经典桌面"项目)中,我使用了 C# 代码

//概念:DotNetInterface comObjectInstance =(DotNetInterface)Microsoft.VisualBasic.Interaction.CreateObject(这个字符串指定了 COM 对象类型");//示例:通过 COM 打开 Excel: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 对象类型库的引用,但无法引用 Microsoft.VisualBasic,因为它没有出现在 Visual Studio 的引用管理器中.

想法、尝试过的解决方案、推测等

我添加了对适用于 UWP 的 Windows 桌面扩展"的引用,希望它可以调用普通的 .NET 功能,但还没有弄清楚如何使用它.

我认为即使 UWP 应用程序从根本上无法进行 COM 调用,那么我们至少可以构造一个包装器来调用普通的 .NET 程序(即使通过网络端口),而该程序又可以运行 COM称呼.由于即使在最坏的情况下显然也可以解决,我觉得应该(并且可能是)Microsoft 提供的解决方案来制作 COM 对象.但我想由于 UWP 太新了,在线文档现在非常稀少而且很难找到.

更新 #1

找到一篇 MSDN 文章,Win32 and COM for Windows Runtime apps和通用 Windows 平台 (UWP) 应用,声称 WinRT 应用(包括 UWP 应用)只能使用 COM 对象的子集.MSDN 建议使用受支持的 COM API 元素或从不受支持的 COM API 迁移到功能替代品.

在找到对第三方库进行 COM 调用的方法后,我通过谷歌搜索抛出的运行时错误找到了这篇文章.错误:

<块引用>

System.Runtime.InteropServices.COMException"类型的异常发生在 mscorlib.ni.dll 中,但未在用户代码中处理

附加信息:创建 COM 组件的实例CLSID {} 使用由于以下错误,CoCreateInstanceFromApp 失败:80040154类未注册(来自 HRESULT 的异常:0x80040154(REGDB_E_CLASSNOTREG)).请确保您的 COM 对象在CoCreateInstanceFromApp 的允许列表.

我仍然不确定是否有一种内置方式可以访问我的第三方库的 COM API.如果没有,这可能意味着我必须使用网络端口或其他东西制作自己的包装器,这似乎是错误的.

解决方案

如您所见,无法从通用 Windows 应用程序访问任意 COM 对象.您的第三方库很可能也使用了无法直接从 Windows 运行时中使用的 API.

假设您打算旁加载应用程序而不是通过商店进行部署,您可以通过 用于旁加载的 Windows 应用商店应用程序的代理 Windows 运行时组件(适用于 Windows 8.1 的文档,但对 Windows 10 仍然有效).此功能专为企业应用设计,旨在提供现代用户界面,同时仍可访问现有功能.

如果您想通过应用商店进行部署,那么您将继续受限于 Windows 运行时上下文中允许的 API,并且不能使用代理的 Windows 运行时组件.

如果您的主要目标是通过商店进行部署并且您不需要以其他方式转换为通用应用程序,那么请查看即将推出的 Windows Bridge for Classic Windows 应用程序(也称为Project Centennial"),它将允许打包您当前的 .Net 项目以进行商店部署,并允许对其进行扩展以使用某些 UWP 功能.>

Question: How to create COM object in a Universal Windows Platform (UWP) application?

Motivation: I want to switch from WPF to UWP. Since my workload requires making calls to third-party libraries accessible only through COM (so far as I know), I need to make COM calls from UWP.

Context:

  • C#
  • .NET
  • Visual Studio 2015
  • Windows 10
  • Ideally targeting all UWP devices, but okay if restricted to desktops/laptops.

Background

In Visual Studio 2013 ("Classic Desktop" project in Visual Studio 2015), I used the C# code

// 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");

The Visual Studio project required a reference to Microsoft.VisualBasic to use Interaction.CreateObject() and the COM object's type library.

I want to use this C# code in a Universal Windows Platform (UWP) application produced by Visual Studio 2015 Enterprise on Windows 10 Education. I am able to add a reference to the COM object's type library, but unable to reference Microsoft.VisualBasic since it doesn't appear in Visual Studio's Reference Manager.

Thoughts, tried solutions, speculation, etc.

I added a reference to "Windows Desktop Extensions for the UWP" hoping that it might enable calls to normal .NET features, but haven't figured out how to use it yet.

I figure that even if UWP applications fundamentally can't make COM calls, then we could at least construct a wrapper that calls a normal .NET program (even if through network ports) that would in turn be able to run the COM call. Since it's clearly possible to work around even in the worst case scenario, I feel like there should be (and so probably is) a Microsoft-provided solution to making COM objects. But I guess since UWP's so new, the online documentation's pretty sparse and hard to find right now.

Update #1

Found an MSDN article, Win32 and COM for Windows Runtime apps and Universal Windows Platform (UWP) apps, that claims that WinRT apps (which includes UWP apps) can only use a subset of COM objects. MSDN suggests either using a supported COM API element or migrating from an unsupported COM API to a functional replacement.

I was able to find this article by googling a run-time error thrown after I found a way to make a COM call to my third-party library. The error:

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.

I'm still unsure about whether or not there's a built-in way to access the COM API for my third-party libraries. If there's not, it may mean that I'll have to make my own wrapper using network ports or something, which seems wrong.

解决方案

As you note, it isn't possible to access arbitrary COM objects from a Universal Windows app. It is likely your third party libraries also use API which aren't available directly from within the Windows Runtime.

Assuming you intend to sideload the app rather than deploying through the store you can call your COM objects and libraries indirectly through a Brokered Windows Runtime Components for side-loaded Windows Store apps (docs for Windows 8.1 but still valid for Windows 10). This feature is designed for enterprise apps to provide a modern UI while still having access to existing functionality.

If you want to deploy through the store then you will stay restricted to the API allowed in the Windows Runtime context and can't use a Brokered Windows Runtime Component.

If your main goal is to deploy through the store and you don't need to otherwise convert to a Universal app then take a look at the upcoming Windows Bridge for Classic Windows apps (also called "Project Centennial") which will allow packaging your current .Net project for store deployment and will allow extending it to use some UWP capabilities.

这篇关于如何在 UWP 应用程序中创建 COM 对象?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆