How to solve .NET C# GDAL PINVOKE error in class library? - c#

I'm building an extension for a program (.NET Class library) where I want to use GDAL. I have installed GDAL and GDAL.Native via Nuget. I previously tested my function in a separate Console App, and everything was working fine.
But when I use the function in my Class Library and load it into the program, it's giving an error during the GdalConfiguration:
The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception. at OSGeo.GDAL.GdalPINVOKE.SetConfigOption(String jarg1, String jarg2)
at OSGeo.GDAL.Gdal.SetConfigOption(String pszKey, String pszValue)
at MyGdal.GdalConfiguration..cctor() in C:....MyGdal\GdalConfiguration.cs:line 98
In Line 98 the configuration for variable "GDAL_DATA" is set:
// Set the additional GDAL environment variables.
string gdalData = Path.Combine(gdalPath, "data");
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
Gdal.SetConfigOption("GDAL_DATA", gdalData);
Does anyone know why this variable cannot be set when using the function in the program but it works when using it in a Console App? I have tried to reference the Console App to my Class Library and call the function from there, but it is giving te same error.

Related

How to retrieve data from Elipse server with .Net Core? Am I restrained to .NET Framework for using a DLL?

I have an Elipse E3 Studio (build 5.0.434) server with a bunch of tags (running on a x64 windows) and I want to read then from a .NetCore (3.0) console application (same machine). The thing is Elipse works with COM (as far as I know) and .NetCore can't natively handle it. Gotta use some Interoperability Library or something. .netCore3.0 Release Notes at Windows Native Interop
To make the Elipse server work I used a hardkey so the server was running locally.
I have named my tag "A1" and set the value inside Elipse.
To make the access I made a C# program using e3DataAccessLib and referenced it on the .csprj.
The Program.cs is as follows :
using System;
using E3DATAACCESSLib;
namespace ElipseNetCore{
class Program{
static void Main(string[] args){
try{
E3DataAccessManager e3DA = new E3DataAccessManager();
e3DA.Server = "localhost"; //kinda pointless but still
object Value = new object();
object Timestamp = new object();
object Quality = new object();
e3DA.ReadValue("A1.Value", ref Timestamp, ref Quality, ref Value); //ReadValue is a Elipse Server method that takes in a "tag" and place the result in the ref's
Console.WriteLine($"Value: {Value}, Timestamp: {Timestamp} and Quality: {Quality}");
}//end try
catch(Exception ex){
Console.WriteLine("the mother funking error now is :" +ex.ToString());
//regsvr32 C:\Users\lucas.battistella\Documents\Desenvolvimento\ElipseNetCore\ElipseNetCore\obj\Release\netcoreapp2.2\win-x64\ElipseNetCore.dll
}//end catch try
}//end Main
}//end Program
}//end namespace
The Error I get is the following:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {80327130-FFDB-4506-B160-B9F8DB32DFB2} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
Other answers point to a x32 on a x64 or vice-versa issue.
However I've already tried making sure everything is running on x64.
Then I tried everything on x32/x86.
Also tried manually registering the .dll with regsvr32 (as show in the commented out line in the first code block and also the E3DATAACESSLib.dll), got and error popup saying "the said dll was loaded but the entry point DllRegisterServer was not located. Verify if the said dll is a DLL or OCX file"
I've been entangled with this problem for a few days now and since I'm new to all this I don't even know if I'm tumbling in the right direction. I would really appreciate any explanation and please excuse my typos.
How do I retrieve data from an Elipse server? Have I missed something?
UPDATE: I have tried that exact same code on Visual Studio running on .Net Framework 4.7.2 and it worked.
Also tried (still on Visual Studio) on .NetCore and got the aforementioned error.
Work Around:
Forget about NetCore and migrate to NetFramework 4.8. Forget about VSCode and keep rolling with VS.
Every time I look back at this problem it intrigues me. The E3DATAACCESSLib was build against x32 and for NetFramework (which mean Windows necessarily). The weird bit is that it ran on my machine targeting x86 (VS and NetFramework 4.8) but not on VSCode and NetCore. I read conflicting information on libraries built for NetFramework working (or not) on Core.
Today I tried running the built working code on a different machine (virtual and remote) and it showed me the exact same error message. And I fixed it by installing the E3 program and restarting the machine, simple as that.
If that ring any bell to you please share the light.

Exception: System.DllNotFoundException - Invoke CoolProp (Native C++ library) functions with .NET Core 2.1

I have a .NET project written in C# that has a dependency with the CoolProp library (available here https://github.com/CoolProp/CoolProp). It calls the CoolProp functions using PInvoke.
Unluckily I have to run this program in a linux environment (precisely the AWS lambda env https://docs.aws.amazon.com/en_us/lambda/latest/dg/current-supported-versions.html).
For now, I want to execute it with .NET core (command dotnet run) on my PC with Ubuntu OS but I get always the following error:
Unhandled Exception: System.DllNotFoundException:
Unable to load shared library 'libCoolProp.so' or one of its dependencies.
In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibCoolProp.so.so: cannot open shared object file: No such file or directory
at Test1.Program.PropsSI(String Output, String Name1, Double Prop1, String Name2, Double Prop2, String Ref)
at Test1.Program.Main(String[] args) in /home/user/Desktop/TestDllInUbuntu/Test1/Program.cs:line 23
The test program is:
using System;
using System.Runtime.InteropServices;
namespace Test1
{
class Program
{
[DllImport("libCoolProp.so")]
private static extern double PropsSI(string Output, string Name1, double Prop1, string Name2, double Prop2, string Ref);
static void Main(string[] args)
{
double propsRes = PropsSI("H", "T", 300.0, "Q", 0.0, "R410A");
Console.WriteLine(propsRes);
}
}
}
The Program.cs is in the same folder of libCoolProp.so.
Notes:
The same program in Windows 10 compiled and executed with .Net Core with its libCoolProp.dll works.
The same program in Ubuntu 18 compiled and executed with Mono Runtime works.
How to solve the compatibility issue between CoolProp lib and .Net Core runtime?
I found the solution.
The executable file build by .NET core is inside bin/debug/netcoreapp2.1/, hence is sufficient to link the library with the correct path:
[DllImport("../../../libCoolProp.so")]
This is not necessary for Windows 10 as the .NET core runtime search the dll inside the folder in which the command dotnet run is called.
For more info look up the issue: https://github.com/dotnet/core/issues/2015

Import scikit in C# application

I am trying to import scikit-learn in a C# (console) application. I am using Python Tools for Visual Studio and IronPython 2.7.3.
I managed to run an external python script and I also managed to import numpy by declaring the python path: "C:\Python27\Lib\site-packages\"
However, when it comes to scikit-learn I get an error message:
Oops! We couldn't execute the script because of an exception: No module named _c
heck_build
___________________________________________________________________________
Contents of C:\Python27\Lib\site-packages\sklearn\__check_build:
setup.py setup.pyc setup.pyo
_check_build.pyd __init__.py __init__.pyc
__init__.pyo
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.
If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.
If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.
The file "_check_build.pyd" exists in "C:\Python27\Lib\site-packages\sklearn__check_build\".
My code is based on this article: http://devleader.ca/2013/09/23/visual-studio-c-python-sweet/
The file I am using has only the following code:
from sklearn.svm import SVC
print('Hello Python in C#')
Is it possible to add and use scikit in C#? If yes, could you please provide a workaround?
Looks like scikit-learn requires a C extension, which means it won't run under IronPython.

C#: MWArray and Getting an Instance of Class

Using MATLAB Compiler Runtime (mcr) i have created dll of the entropy.m choosing FrameWork version 4.0. I have added this dll to c# references. Also i have to add MWArray.dll to create the needed paramters for methods.
> public class Entropy : IDisposable
> {
> //Constructors,Finalize,Methods,Class Members are located here.
> }
> using com.Entropy;
> using MathWorks.MATLAB.NET.Arrays;
> using MathWorks.MATLAB.NET.Utility;
Here is my problem. I am trying to get an instance of Entropy class like
Entropy ep = new Entroyp();
However it throws an exception that says:
> The type initializer for 'com.Enthropy.Enthropy threw an exception.
Also while i try to create an MWNumericArray it throws the same exception. I've tried to change framework version of my project to 4.0 but it did not work. What is the reason of this error?
In addition i've looked at links below.
ACCESSING MATLAB FUNCTIONS FROM C#.NET
Using MATLAB Builder NE
Is there an example of using the "MWArray" data type in a .NET language such as C# with a MATLAB Builder for .NET component?
Okay. After installing the MCR, i have done everything at the beginning. Created the dll of entropy.m. Then i opened an earlier version of my project, i mean i deleted the added references; MWArray.dll and Entropy.dll. After that i have added those references to my project back. Now it works fine.
The important points:
1)Before create a dll file of any MatLab function INSTALL MCR.
2)While creating dll try to give different names to class and project. I mean if you create project that name is Entropy.prj dont create your class name as Entropy.
3)Don't forget to add MWArray.dll to your project. Its location is
(..//Program Files(x86)//MATLAB\MATLAB CompilerRuntime\v81\toolbox\dotnetbuilder\bin\win32\v2.0)

how can i access C# dll from VBScript on client machine

i have created a C# dll file on my machine as shown below:
namespace myDLL
{
public class myClass
{
public string myFunction()
{
return "I am Here";
}
}
}
then i created a tlb file with "tlbexp" command,
then i used the "regasm" command n registered this dll on my machine.
When i created an object of type myClass on my machine using VBScript, everything is working fine... here i used the CreateObject() method as shown below:
Set myObj = CreateObject("myDll.myClass")
Now i want to create an object of type myClass from VBScript that is running on another machine, how can i do this. please help me how can i access that dll file, am using the CreateObject() function as shown below:
Set HD = CreateObject("myDll.myClass","myMachineName")
now am getting error as "permission denied".
It appears that this is supported if the assembly is built with as COM visibility enabled.
Is it possible to execute a .NET assembly(dll) from vbscript?
By the way, I was pretty delighted to find out that there is a JScript compiler for .NET which allows one to write .NET code using JScript and also to target other .NET assemblies but unfortunately I haven't found anything similar for VBScript.

Categories