I want to use gsdll32.dll from Metro Style App c#. I load dll as follow:
[DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
private static extern void gsapi_delete_instance(IntPtr instance);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_revision")]
private static extern int gsapi_revision(ref GS_Revision pGSRevisionInfo, int intLen);
[DllImport("gsdll32.dll", EntryPoint = "gsapi_set_stdio")]
private static extern int gsapi_set_stdio(IntPtr lngGSInstance, StdioCallBack gsdll_stdin, StdioCallBack gsdll_stdout, StdioCallBack gsdll_stderr);
But when I try to load dll not found exception occurs. I have already put it in Debug file. But it does work.
I reference from this link.
It is possible, but:
Was gsdll32.dll compiled against the WinRT SDK?
Does gsdll32.dll passes the App certification?
Did you include the dll in your package?
If the answer to any of those questions is "no", then your code will not work.
Build Ghostscript from source (PC version)
Microsoft Environment for WinRT
Ghostscript can be built in the form of a win32 DLL for use within a Windows Runtime application or Windows Runtime component. Building for WinRT requires use of Microsoft Visual Studio 2012. There is a solution file that can be loaded into VS 2012, in the directory winrt
The WinRT application or component should include iapi.h from gs/psi and link with gsdll32metro.lib from gs/debugbin or gs/releasebin. Also any app using ghostscript either directly or via a component should add gsdll32metro.dll as "content". This inclusion of the dll is necessary so that it will be packaged with the app. If one wishes to be able to run the debugger on ghostscript then gsdll32metro.pdb should also be added as content.
From the GhostScript 9.21 documentation
Yes, I do realize this question is half a decade old.
Related
I'm building a Keygenerator application with .Net Maui (not blazor).
For this i use c#-Code in the MainPage.xaml.cs that calls a cpp .dll file and imports some methods.
The import looks like this:
[DllImport("W5R._Keygen.dll", CallingConvention = CallingConvention.StdCall)]
internal unsafe static extern void
SHA256_calc(byte* hash, void* input, ulong inputlen);
The .dll is in the same folder as the MainPage.xaml, App.xaml etc.
By changing some properties of the .dll (Build = content) the Debug version on the Windows Machine works fine and it works exactly as it should.
HOWEVER:
and this is the problem:
when I run the application on the android-emulator it loads the app just fine and as soon as i press a button that invokes the usage of the .dll my App Crashes and it just stopped working.
Beforehand I had the error ".system dll not found" which i fail to reproduce in the current moment.
Anyone knows how I use the .dll library? It's cpp code that i cannot access.
You can't use a dll on Linux / Android.
Dll are a windows thing and Linux has other formats for shared libraries.
see this Post
It seems there exists a wine release for Android, but I don't know how its used.
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 try to build my project using Unity 2017.4.34f1, because Google now need both 32 & 64 bit, so i choose IL2CPP.
For Android NDK, I use r13b
However, build failed and I get below error (summary) :
Exception: /Applications/Unity/Hub/Editor/2017.4.34f1/Unity.app/Contents/il2cpp/build/il2cpp.exe did not run properly!
This failure occurs because the code in the project has extern methods in C#. Methods marked as extern with a the [Dllimport("__Internal")] attribute must be present in a native library that is linked with the Unity player when building with the IL2CPP scripting backend.
For example, this in this project one function which causes this issue is named: activateApp
You have two options:
Build a native library with all of the methods marked as extern with the [Dllimport("__Internal")] attribute for the target platform and architecture of the player. See this documentation for details about native plugins: https://docs.unity3d.com/Manual/NativePlugins.html
Remove the C# code which defines this extern method. You can do that with platform dependent compilation: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
In this case specifically, it looks like the error comes from the Facebook SDK. See if there is a newer Facebook SDK available - I believe this may have been corrected.
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 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.