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

1 Answer


Please check the following steps:

  1. Create a C# UWP project in a new solution.
  2. Add a C++ DLL(Universal Windows) project(named MyDll1) in the same solution.
  3. Add your C code in the C++ DLL project. For example:
#pragma once

extern "C" _declspec(dllexport) int Sum(int a, int b);

#include "pch.h"
#include "MyDll1.h"
int Sum(int a, int b)
    return a + b;
  1. Add a Windows Runtime Component(C++/WinRT) project in the same project.
  2. Right-click on the name of the Windows Runtime Component(C++/WinRT) project, and select option Add > Reference, check your DLL project in Projects tab. Click OK.
  3. There is an auto-generated class Class, you could use the class or add other class(Add > New Item > Code > Midl File(.idl)) as needed. The new class must be generated from a midl file. You could get more information about authoring api referring to the document.
  4. Take Class class as an example. Include the header file of dll in Windows Runtime Component(C++/WinRT) project.
#include "..\MyDll1\MyDll1.h"
  1. There is a sample method named MyProperty shown in Class class. The MyProperty method is added to the Class.idl file and the complier will generate the corresponding methods in Class.h and Class.cpp after you build the project. And you need to go to the locations \RuntimeComponent\ RuntimeComponent \Generated Files\sources\Class.h and Class.cpp in File Explorer and open the .h and .cpp file to copy the generated methods into your code in Visual Studio. You could use MyProperty method to pass values to C# project or add other methods in classes. Refer to the document for more information about how to add new method in idl file.
  2. You could call the Sum(int a, int b) of MyDll1 project in MyProperty method.
int32_t Class::MyProperty()
    int t = Sum(1, 2);
    return t;
  1. Right-click on the name of the C# UWP project, and select option Add > Reference, check your Windows Runtime Component(C++/WinRT) project in Projects. Click OK.
  2. Add include statement in C# UWP project.
using RuntimeComponent;   // RuntimeComponent is the name of Windows Runtime Component(C++/WinRT) project.
  1. You could call the MyProperty method in C# UWP project.
RuntimeComponent.Class myClass = new Class();
var value = myClass.MyProperty;
answered on Stack Overflow Jan 13, 2021 by YanGu

User contributions licensed under CC BY-SA 3.0