I wrote a C++ Class Library in Visual Studio that just defines a function that invokes some Python:
#pragma once
#include <Python.h>
extern "C"
__declspec(dllexport)
void python()
{
Py_Initialize();
PyRun_SimpleString("2 + 2");
}
I made another project in the same solution that was a C# Blank Universal app. I tried to reference the DLL generated from the previous project I mentioned:
using System;
...
namespace StartupApp
{
...
sealed partial class App : Application
{
private const string CPPPythonInterfaceDLL = #"pathtodll";
[DllImport(CPPPythonInterfaceDLL, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
private static extern void python();
public static void Python()
{
python();
}
...
public App()
{
...
Python();
}
...
}
}
The app is in a Release configuration.
Whenever I try to run the app on my Local Machine, it always gives an error:
The program '[2272] StartupApp.exe' has exited with code -1073741790 (0xc0000022).
Activation of the Windows Store app 'ab6a8ef2-1fa8-4cc7-b7b3-fb7420af7dc3_7dk3a6v9mg4g6!App' failed with error 'The app didn't start'.
So my question is this: can I reference a C++ class library from a C# UWP project? Or does the security on UWP apps not allow this?
Or is it because of Python.h?
EDIT:
I built the project with a DLL project and a Runtime Component that wrapped it, and now I have this error:
An exception of type 'System
'System.DllNotFoundException' occurred in StartupApp.exe but was not handled in user code
Additional information: Unable to load DLL 'pathtodll': Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
I added a user to the DLL with the object name "Everyone" (I am not sure how else to give everyone permissions) but the error still comes up.
Firstly, UWP can't consume a legacy C++ dll just by DLLImport.
If you want to expose legacy c++ functions to C#, the first suggestion is to wrap that C++ logic using a WinRT component. Then you can reference this component in UWP application by following steps: adding it to the project, open the files' properties in the Solution Explorer window, and mark them as content to be included in the app package. This post would be helpful. This one provides more detailed steps.
If you want to PInvoke the dll, you can follow these steps (You can refer to this MSDN post):
Add win32 dll into your UWP project making sure to set its type as 'content'
Then in the proper cs file, using DllImport to PInvoke the dll.
There is one more thing: You need to make sure your Python dll is not using prohibited APIs in WinRT. You can check this by using /ZW compile option for the dll.
Related
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):
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:
#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))
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.
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:
https://learn.microsoft.com/en-us/cpp/cppcx/dlls-c-cx?view=msvc-160
https://www.c-sharpcorner.com/forums/how-to-use-cpp-dlls-in-c-sharp-uwp-projects
How to call a function from DLL in UWP?
Can you use C++ DLLs in C# code in a UWP?
https://learn.microsoft.com/en-us/cpp/porting/how-to-use-existing-cpp-code-in-a-universal-windows-platform-app?view=msvc-160
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:
Create a C# UWP project in a new solution.
Add a C++ DLL(Universal Windows) project(named MyDll1) in the same solution.
Add your C code in the C++ DLL project. For example:
//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;
}
Add a Windows Runtime Component(C++/WinRT) project in the same project.
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.
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.
Take Class class as an example. Include the header file of dll in Windows Runtime Component(C++/WinRT) project.
//Class.h
#include "..\MyDll1\MyDll1.h"
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.
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;
}
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.
Add include statement in C# UWP project.
using RuntimeComponent; // RuntimeComponent is the name of Windows Runtime Component(C++/WinRT) project.
You could call the MyProperty method in C# UWP project.
RuntimeComponent.Class myClass = new Class();
var value = myClass.MyProperty;
I have code written in ANSI C that I would like to use in C#. I have compiled the C code into a DLL and created C# wrapper classes to interop with the C code. The point of the wrapper is to simplify a users interaction with the underlying C code.
[DllImport(DLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern void PrintHelloWorld();
public void PrintHelloWorldC()
{
PrintHelloWorld();
}
Above is a simplified example of what I have done so far. Now, I am trying to create a DLL from the C# wrapper classes I wrote. From here I could give both the DLLs created and someone would be able to interact with the C# based DLL to interact with the underlying C based DLL. Below is what I have done and what problem I am having.
I compile the C code into a DLL in Visual Studio 2019 with my Configuration=Release and Platform=Win32.
I compile my C# classes into a DLL in Visual Studio 2019 with my
Configuration=Release, Platform=Win32, and Platform Target as x86.
I create a new project and link my C# DLL and set Platform Target as x86.
I put my C DLL into the Debug folder of the project so that it is available to the C# DLL through the marshal directive.
I try to make a call into the C# DLL and below are the chain of events occurring.
Program calls C# DLL method PrintHelloWorldC() and is successful
Within PrintHelloWorldC() it tries to make a call to PrintHelloWorld();.
Following Error Occurs:
System.BadImageFormatException
HResult=0x8007000B
Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
My research yielded that it was most likely a mismatch in Platform Target between the DLLs and Test Project, however, I have double checked all the configurations and they should all be compiled for x86.
Below is what additional testing I have done.
Created a C# Project, and wrote simple code to make a call directly into the C based DLL. This works fine.
It seems that as soon as I try to use the two DLLs on top of each other in another project I start facing issues.
Any help or guidance is appreciated. Thanks in advance!
I'm trying to implement a simple .dll to my Project which will be builded for the HoloLens Augmented Reality Application. I am trying to simply call a function from my C++ .dll to implement an OpenCV Code but on the HoloLens it throws an error shown below.
.dll C++ Code:
extern "C" void __declspec(dllexport) __stdcall test() {
return;
}
Unity C# Code:
internal class OpenCV
{
// Define the functions which can be called from the .dll.
[DllImport("Project1")]
internal static extern void test();
public static void testmeth()
{
test();
}
}
Error when Debugging on HoloLens with Visual Studio:
System.DllNotFoundException: 'Unable to load DLL 'Project1': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'
Here you can see the Settings and Path of the dll:
Problem:
The source files of OpenCV which I compiled for x86 and used successfully for my UWP App somehow didn't work for the HoloLens.
Solved:
I used the NuGet-Package "OpenCV-HoloLens" for my C++ .dll and copied the .dlls to Unity as well.
I put a dll build in native C++ as a Universal DLL in my project directory of C# UWP app and setting content to copy always it was finding it and then the next day suddenly nothing, constantly getting:
"Unable to load DLL 'AVEngine.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
I am calling with:
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
private static extern int OpenForProcessing();
I ensured the architecture and build config matches, I have also checked SDK versions (min/target versions), I'm stumped.
Use Dependency Walker to check dependencies of your AVEngine.dll. Probably some of them missing and because of it Dll cannot be loaded.
Better proper approach to expose old functionality to your modern C# UWP application is to wrap AVEngine.dll logic using a WinRT component. Then you can reference this component in UWP application. Article "Use Existing C++ Code in a Universal Windows Platform App" could be helpful.
I am developing an application that communicates with some hardware using the C++ DLL provided by the hardware provider. I also have a header file for this DLL, but no source file. In my C# application, I'm trying to import and call the DLL's functions like this:
public class MyClass
{
[DllImport("CaGe.dll")]
public static extern long someLibraryFunction();
public void callLibFunction()
{
Console.WriteLine("function call: " + someLibraryFunction());
}
}
The library.dll file is located in the same folder as the MyClass.cs file and also the rest of the project files. When I start the debugging session however, I first receive a dialog window from system saying: CAGE InitDll: Erro on load library "CdbClientInit" and after I click OK on that, an unhalted exception on the line where I call the library function occours.
The exception says: System.DllNotFoundException: Exception from HRESULT: 0x8007045A
I found on the internet that this may be caused by a missing DLL that the CaGe.dll needs, so I tried to put the DLL through the Dependency Walker, but the results are a little bit confusing for me. Under the CAGE.DLL there are WS32_32.DLL, VERSION.DLL, KERNEL.DLL and USER32.DLL tabs, I susspect that the libraries I need are somwhere under the USER32.DLL tab, but I don't recognize any of them (if a missing DLL is what is causing this problem).
Any advice would be much appretiated :)
More help to use C++ function in C# please check below URL
http://www.codeproject.com/Questions/107152/Using-a-C-dll-in-a-C-application