简体   繁体   English

使用Native C ++中的通用Windows C#类库

[英]Use Universal Windows C# class library from Native C++

I have Universal Windows C# class library with UI components. 我有带有UI组件的通用Windows C#类库。 I was wondering if I can use it from Native C++. 我想知道我是否可以使用Native C ++。

I tried to use regasm to convert class library dll into tlb file, but it throws error 我试图使用regasm将类库dll转换为tlb文件,但它会抛出错误

Error: Assembly must not be a Windows Runtime assembly. 错误:程序集不能是Windows运行时程序集。

Also I tried to make a WinRT/WRL wrapper for C# class library, and tried to load it from Native C++. 我还尝试为C#类库创建一个WinRT / WRL包装器,并尝试从Native C ++加载它。 But when I call LoadLibrary for wrapper dll, it returns 'nullptr' with 126 error, even though all dlls and executables are in the same directory. 但是当我为包装器DLL调用LoadLibrary时,它会返回“nullptr”,错误为126,即使所有dll和可执行文件都在同一目录中。

So how can I use Universal Windows class library from Native C++? 那么如何使用Native C ++的通用Windows类库呢? Is it possible? 可能吗?

You will have to expose your class library as COM component and call it from native code, this is the most convenient solution. 您必须将类库公开为COM组件并从本机代码调用它,这是最方便的解决方案。 you won't be able to call Universal Windows C# class library from native C++, as it won't be recognized and, as you mentioned in your question, it will cause a nullptr exception. 您将无法从本机C ++调用Universal Windows C#类库,因为它将无法识别,正如您在问题中提到的,它将导致nullptr异常。

The interesting things is, you can do the other way around! 有趣的是, 你可以做相反的事情

you can create a native C++ library and call it in Universal Windows C# platform - there is whole post in MSDN regarding this practice: 您可以创建一个本机C ++库并在Universal Windows C#平台中调用它 - 在MSDN中有关于此实践的完整帖子:

Use Existing C++ Code in a Universal Windows Platform App 在通用Windows平台应用程序中使用现有C ++代码

funny thing in my opinion you can do one thing but not the other way around but still, it's good to know that at least one way is actually possible. 有趣的是,在我看来,你可以做一件事,但不是相反,但仍然,很高兴知道至少有一种方式实际上是可能的。

I am not sure "convenient" is the word I would use to describe exposing native C++ as a COM component. 我不确定“方便”这个词我会用来描述将本机C ++暴露为COM组件。

You should take a look at C++/WinRT 你应该看看C ++ / WinRT

https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis . https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis

It appears to supersede C++/CX which was Microsoft's initial approach to allow C++ to be used to build and use WinRT components. 它似乎取代了C ++ / CX,这是微软允许C ++用于构建和使用WinRT组件的初始方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM