C++ DLL in Blazor Project

Viewed 1555

I am trying to bring a small desktop application that I do in C # to Blazor. But I am running into the wall of native dlls.

I have a dll of my own in C ++ that I am not knowing how to take to blazor.

Currently in desktop application I am using the following method

[DllImport(@"C:\Users\amg\source\CRVentaGenerica\CRVentaGenerica\bin\Debug\My.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int My_Initialize(string archivo);

I have been looking for information and / or examples but I have not found anything that is clarifying.

The error that the browser console tells me is "DLLNotFound".

Any indication to be able to jump this wall?

Thanks for your time <3.

2 Answers

The error that the browser console tells me

When you see this in the Browser then you are using Blazor WebAssembly. You can't use a native library there.

You would have to a) compile the C++ to WebAssembly and then b) use JS Interop to load and call that. I don't know about an example.

Blazor also has a server-side application type. You could use the DLL there just as in the desktop app.

3 solutions

  • Try to compile the C++ to WebAssembly, possible if C++ code doesn't use third party library you don't have source code. This is not trivial.
  • If the C++ code is short convert it to C#
  • For a complex C++ library the only solution is to expose it using API mechanism. With ASP.NET Core you can use NativeLibrary.Load. With ASP.NET Framework you can use DllImport.

Example with ASP.NET Core here https://github.com/iso8859/ExposeCppApi

Related