2

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.

AMG194
  • 43
  • 7
  • 1
    Does this answer your question? [DllNotFoundException, but DLL is there](https://stackoverflow.com/questions/1246486/dllnotfoundexception-but-dll-is-there) – Charlieface Apr 06 '21 at 10:39
  • You'll probably have to compile your C++ library to WASM and interop with it like you would with any other WASM library – MindSwipe Apr 06 '21 at 10:40
  • @Charlieface the same dll in the same space is found by the desktop app but not by Blazor. – AMG194 Apr 06 '21 at 10:59
  • 1
    @Charlieface, this question should not be closed as a dup of that other question. Blazor is different, and the answers on that question are completely irrelevant. – Kirk Woll Apr 06 '21 at 19:55

2 Answers2

5

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.

H H
  • 263,252
  • 30
  • 330
  • 514
1

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

Remi THOMAS
  • 854
  • 6
  • 11
  • Do you know of an example for the third option? – AMG194 Apr 13 '21 at 13:15
  • Answer updated to point to an example I did. ASP.NET Core is really better to write API. If you work in a team with a lot of APIs, take a look at gRPC, really mandatory for implementing many endpoints. – Remi THOMAS Apr 14 '21 at 06:33