Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP API in ASP.NET

Tags:

.net

asp.net

uwp

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?

like image 758
Seva Alekseyev Avatar asked Feb 04 '26 09:02

Seva Alekseyev


1 Answers

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.

like image 169
Seva Alekseyev Avatar answered Feb 06 '26 01:02

Seva Alekseyev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!