Skip to content

Instantly share code, notes, and snippets.

@martinwoodward
Created June 28, 2016 09:32
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save martinwoodward/f5b195aa53b4ed52cabaf701486baa79 to your computer and use it in GitHub Desktop.
Example of PInvoke from Linux with .NET Core
using System.Runtime.InteropServices;
namespace PInvokeSamples {
public static class Program {
// Import the libc and define the method corresponding to the native function.
[DllImport("libc.so.6")]
private static extern int getpid();
public static void Main(string[] args){
// Invoke the function and get the process ID.
int pid = getpid();
Console.WriteLine(pid);
}
}
}
@wiwichu
Copy link

wiwichu commented Jul 25, 2016

How do I call a c++ library I created from asp.net core in windows? I get a BadImageFormatException. In RC1 I was able to do it, but am not sure where to place the dll in asp.net core.

@ms0713
Copy link

ms0713 commented Jan 4, 2018

@martinwoodward What is the alternative for following code:
This code i m using in Windows. I want alternate code for Linux(RHEL7).

`using System.Runtime.InteropServices;

namespace PInvokeSamples {
public static class Program {

    // Import the kernel32.dll and define the method corresponding to the native function.
    [DllImport("kernel32.dll")]
    private static extern uint GetCurrentThreadId();

    public static void Main(string[] args){
        // Invoke the function and get the process Thread ID.
        uint tid = GetCurrentThreadId();
        Console.WriteLine(tid);
    }
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment