dotnet / dotnet/core

How to Load C Shared object file (.so) file in .Net core

Open
#4,347 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
22k
Forks
4.9k
Avg merge
5d 5h
Merged PRs (30d)
29

Description

I am having one .so file which I want to use in .Net core.

Once I use the same library in c with below code it is working fine but I don't know how to add these references in .Net Core.

**Code in C**

add_library(capi SHARED IMPORTED)
set_target_properties(capi PROPERTIES
IMPORTED_LOCATION "/opt/lib/libcapi.so"
INTERFACE_INCLUDE_DIRECTORIES "/home/src/api/capi/include;/home/src/api/capi/include/Linux";
C_STANDARD 11 )

Note: INTERFACE_INCLUDE_DIRECTORIES is having header files.

In .Net core I am using the code as mentioned below. I added the reference of .so file only, Please let me know how I will add the references to INTERFACE_INCLUDE_DIRECTORIES in .net Core.

class Program
{
[DllImport(@"\TestApp\libcapi.so")]
public static extern bool CreateDomain(object hProvider, string nHndType, string szName, string szName1, bool test);

static void Main(string[] args)
{
var d = CreateDomain(null, "", "", "", true);
Console.ReadKey();
}
}

**Current Error:**
System.BadImageFormatException
HResult=0x8007000B
Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
Source=TestApp
StackTrace:
at TestApp.Program.SL_CreateDomain(Object hProvider, String nHndType, String szName, String szName1, Boolean test)
at TestApp.Program.Main(String[] args) in D:\Projects\R&D\.Net Core\TestApp\TestApp\Program.cs:line 13

I am not having any idea about how to add INTERFACE_INCLUDE_DIRECTORIES in .net core.
If anything else required please let me know.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.