Accessing C++ .so file from C# within WSL ubuntu.

HYUNBIN PARK 0 Reputation points
2025-04-11T03:10:20.5666667+00:00

Hello, . I have some questions regarding creating a dynamic library using c++ and use it in C# on linux machine. In the linux machine, I believe it runs on top of x64 and here is the base code for C++.


#include <iostream>

extern "C" void TestingHelloworld(){

 std::cout << "Hello world" << std::endl;

}

And I create it as .so(shared object) file.

in C#,


internal class Program {

// I use it using static extern method such as:

[DllImport("liblinuxprojectconsole.so", CallingConvention.Cdecl)]

public static extern int TestingHelloworld();

static void Main(string[] args) {

  Console.WriteLine(TestingHelloworld();

}

Something like that.

And Error message is like:

"System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x800700B)" and people are saying it could happen due to version mismatches (x64 vs x32) but I am pretty sure I am using x64 for everything. (I created the .so file using visual studio C++ project with linux).

Can you give me any hints or some direction I have to dig into? Checking on this matter for a day but haven't got any idea. I tried it out using dll with windows but it works perfectly fine. Just not in linux with WSL.

Thank you in advance.

Developer technologies C#
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.