UWP API in ASP.NET

后端 未结 2 1807
野趣味
野趣味 2021-01-20 22:01

I\'d like to use a piece of Windows 10 specific UWP API (specifically, the Windows.Graphics.Printing3D stuff) in an ASP.NET code-behind DLL. Is there any way to do so?

2条回答
  •  失恋的感觉
    2021-01-20 22:41

    While looking for a .NET-only resolution to this one, I've found a moderately clean way - a Win32/64 C++ DLL that would consume UWP API and present a COM- or P/Invoke-based interface to .NET.

    Create a regular Win32 DLL. Build an interface for .NET to consume - exported functions or objects, depends. In my case, a single exported function will do. In the project's C/C++ settings, make the following changes:

    • Under General, set Consume Windows Runtime Extensions to Yes.
    • Under General, set Additional #using Directories to: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcpackages;C:\Program Files (x86)\Windows Kits\10\UnionMetadata (assuming Visual Studio 2015)
    • Under Code Generation, set Enable Minimal Rebuild to No (it's only Yes for Debug, not for Release).

    Then instantiate and use UWP components in the DLL in the usual C++/CX manner, like you would in a Store app, via using namespace Windows::... and ref new.

    In this approach, you lose bitness agnosticism; an unmanaged DLL can't be "Any CPU". You win some, you lose some. Also, the site will not run without the Visual C++ redistributable package on the system. On the other hand, it may run faster than a .NET app; less managed/native boundary crossings.

    Inspiration: "Using C++/CX in Desktop apps" by Pavel Y.

提交回复
热议问题