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):
Tried to add it to the UWP by using DllImport (which in a C# Console App program worked).
The code in the C file:
#include<stdio.h>
_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))
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.
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.
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!
Please check the following steps:
MyDll1
) in the same solution.//MyDll1.h
#pragma once
extern "C" _declspec(dllexport) int Sum(int a, int b);
//MyDll1.cpp
#include "pch.h"
#include "MyDll1.h"
int Sum(int a, int b)
{
return a + b;
}
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.Class
class as an example. Include the header file of dll in Windows Runtime Component(C++/WinRT)
project.//Class.h
#include "..\MyDll1\MyDll1.h"
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.Sum(int a, int b)
of MyDll1
project in MyProperty
method.int32_t Class::MyProperty()
{
int t = Sum(1, 2);
return t;
}
using RuntimeComponent; // RuntimeComponent is the name of Windows Runtime Component(C++/WinRT) project.
MyProperty
method in C# UWP project.RuntimeComponent.Class myClass = new Class();
var value = myClass.MyProperty;
User contributions licensed under CC BY-SA 3.0