How to call a C# DLL method from electron? - c#

I have an electron app which needs to call some methods from an existing DLL. The DLL is a C# DLL and I tried using electron-edge-js but got some errors like : The edge module has not been pre-compiled for Electron version
Can you please let me know how can I call C# DLL methods from electron? Also if you can point me to a sample code for the same.
Thanks in advance.

Related

missing method exception can't find pinvoke dll

I will give you a brief description of my software before I ask questions.
So there is a front end C# code which provides the user interface and a back end C++ code that deals with the hardware.
The C# project creates an executable (.exe) file and the C++ project creates a DLL which talk with each other using pinvoke calls (import/export functions). All this worked fine on WEC7.
Now, we are moving to WEC2013 with Toradex. I downloaded the WEC2013 SDK from Toradex’s website and was able to port my code and build it on VS2013.
I copied all the required files on a USB and when I tried running it the C# exe is not able to communicate with the C++ Dll. The error I get is :
missingmethodexception can't find pinvoke dll “xyz.dll”
I have made sure the dll exists at the specified location. The dll also has the required export functions.
First I thought the function parameters in the export functions could be an issue, but I tried calling function which requires no parameter with the same result.
Any help will be really appreciated.

DllNotFoundException From C# but not VB.NET

I have a third party DLL to integrate with their application. It is a 32 bit C++ DLL that does not expose itself via COM, and I have not been supplied source code or a header file.
It was supplied with working VB.NET example code in a WinForms example. This works, as long as the executable is run from the same directory as the API DLL and the application for which the API is interfacing.
I was using the example function declaration:
Declare Ansi Function GetVersion Lib "ThirdPartyAPI.dll" () As Integer
However, we’ll be using the API calls from a C# library to be used in a web site or web service, so I converted the example VB code to
[DllImport("ThirdPartyAPI.dll", SetLastError = true, CharSet = CharSet.Ansi)]
public static extern int GetVersion();
If I call this from within the C# library I get the error
Unhandled Exception: System.DllNotFoundException: Unable to load DLL
'ThirdPartyAPI.dll': A dynamic link library (DLL) initialization
routine failed. (Exception from HRESULT: 0x8007045A)
I took an alternative route and put the original working VB code from the example into its own VB.NET DLL and called that via C#. I got the same error. Strangely, a call to the VB.NET DLL from a VB.NET console app works.
In summary:
A simple VB.NET console app will work
A simple VB.NET WinForms app will work
A simple VB.NET console app can call GetVersion via a VB.NET DLL
A simple C# console app cannot call GetVersion via a VB.NET DLL
A simple C# console app cannot call GetVersion directly.
This could be to do with incorrectly defining the function calls. At
this stage, I’m not too worried about this.
32 bit compilation does not fix the issue
I have tried this on Server 2008 R2 and Windows 7 Enterprise
I created a WCF host via an NT service to expose the functions, hoping this would work around file locations and dependencies. This was written in VB.NET and placed in the same folder as ThirdPartyApi.dll.
None of the code in the NT service could access GetVersion
None of the code in the WCF service could access GetVersion
Please note, in all of the above cases, the executables are in the same directory as ThirdPartyApi.dll. All of its dependencies also exist in this directory.
Can anyone explain this behaviour, or suggest how this can be made to work within a C# web site?
Thanks
The error code is 0x8007045A. That is a COM error code that maps to the Win32 error code ERROR_DLL_INIT_FAILED. Which is described as:
A dynamic link library (DLL) initialization routine failed.
What this means is that your DLL was found, in spite of the perhaps misleading exception class DllNotFoundException. However, the DLL failed to load. You can gain no more information from the system. There is no mechanism for the DLL to report reasons why it failed to load. I suggest that you contact the vendor of the DLL and ask for support.

System.DllNotFoundException in .NET DLL

I took a program written in C/C++ and modified it's main function to accept some arguments as input and return a variable as output and created a Win32 DLL out of it. I then created a .NET DLL which uses InterOp to access the first DLL. Now when I load the .NET DLL in my C# app I get a System.DllNotFoundException from the DLL which is really baffling me as there were never memory issues with the program and both Win32/.NET dlls are located in the same directory (apart from modifying the main function, the code has not really changed).
The solution was provided in this thread, which was my original question some time ago. I'm pretty sure that answer is correct but I'm just missing something.
You can download my VS solution Here. The solution contains three projects: the Win32 DLL, the .NET DLL, and a winform app that references the .NET DLL (but when trying to test gives the DLL exception). Any help or debugging guidance would be greatly appreciated.
UPDATE: I have tried all the tips/suggestions below but I still get the exact same error. If it makes things easier, my VS solution is available to download in the hyperlink above.
Make sure you have placed the win32 dll on /windows/system32 folder(if only the dll name is passed to DllImport)
Alternatively you can also pass the full path of the dll to the DllImport Attribute.
Use a tool such as Dependency Walker to make sure you are not missing out on any dependent assembly.

How to call c++ Oppencv project from c# web service?

I have implemented image recognition algorithm as OpenCV c++ project and also i have C# web service.I want to call my c++ Opencv project from my c# web service. this is how my project should work.from mobile it send image to the c# web service.Then i need to call the Opencv c++ project in order to do the image recognition. I tried to do this by using DLL.If you think using the DLL is best method can you please tell how i convert my Opencv c++ projetc into DLL OR are there any other method.
Thank you very much
I think that you should use PInvoke to do your job. Haven't used it, but from what I know, this is the way to do it:
http://msdn.microsoft.com/en-us/library/aa446536.aspx
You need to create a C++ DLL project where you will write the image recognition part of the project. You can check out this tutorial on how to create a C++ DLL project with Visual Studio: Walkthrough: Creating and Using a Dynamic Link Library
You should create a class in that DLL file, and you will be able to use that class to process an image. By using PInvoke, you will be able to use that class in a C# project. Here is a great tutorial on how to use a C++ class from a DLL in a C# project: How to Marshal a C++ Class. I have used solution A from that article in many projects to call C++ code from C#.
In order to convert an existing C++ project to a DLL, you can change its output type. Go to Project->Properties, and in that window you need to go to Configuration Properties->General, and change Configuration type to Dynamic Library (.dll), like in this image:
After that, you need to expose your class or functions to be exported from the dll. I strongly suggest you start a new dll project (folow the first link) and work from there, because the AppWizard will create some code for you and it will be easier.

Using C++ code in C#

I had written a code snippet in VC++. However, I cannot continue rest of the application in VC++, so would want to move to C#.
Can I make a dll of the VC++ code and call the functions written in VC++ to work in C#?
I'm quite un-aware if this will work, but I have seen how the native code is called in C# using dll.
Can anyone please help me on this.
Thanks.
You could try compiling your C++ code as managed code using the /clr option and then use it directly or use P/Invoke to call the unmanaged functions from managed code.

Categories