How do I put a C program in a C# UWP?


I am creating a Windows 10 application that works with files. For the GUI I am using UWP (C#) and for the file processing I want to use the C language (Visual Studio 2019).

I have tried these solutions (none of them worked):

  1. C program created with Windows Desktop Wizard (DLL), then DllImport

Tried to add it to the UWP by using DllImport (which in a C# Console App program worked).

The code in the C file:


_declspec(dllexport) int getNumberOfFiles()

The code in the C# UWP app:

 [DllImport(@"...\WorkFilesDll\Debug\WorkFilesDll.dll", EntryPoint = "getNumberOfFiles", CallingConvention = CallingConvention.Cdecl)]
 internal static extern int getNumberOfFiles();

The following exception is thrown:

System.DllNotFoundException HResult=0x80131524 Message=Unable to load DLL '...\WorkFilesDll\Debug\WorkFilesDll.dll' or one of its dependencies: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

  1. C program created with Windows Desktop Wizard (DLL), then add as reference

Tried to add the same dll as reference (References->Add Reference->Browse->Add->OK). After pressing the OK button, the following message indicates the failure:

A reference to "...\WorkFilesDll\Debug\WorkFilesDll.dll" could not be added. Please make sure that the file is accessible, and that is a valid assembly or COM component.

  1. I created other types of projects for the C code (C++, UWP): Dll (Universal Windows), Windows Runtime Component (Universal Windows). The results were the same.

  2. I created other types of projects (C#, UWP): Class Library (Universal Windows), Windows Runtime Component (Universal Windows), in order to add theses projects to the UWP and to add to these projects the dll mentioned above (the C code to be added indirectly to the UWP). The results were the same.

I have found many questions and articles like this, but I didn't see a clear answer or the answers didn't work for me. Some of them are:

I have also read about static libraries. I have failed in implementing them.

How can I put the C code in UWP (C#)?

Are static libraries the answer to my application?

Which are better for this application: the DLLs or the static libraries? Why?

Thank you!

asked on Stack Overflow Jan 5, 2021 by tp123

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0