PInvoke Hello World

1 minute read

the library: hello.c

  #include <stdio.h>;

  void say_hello( char* name ) {
    printf( "hello %s, welcome to the unmanaged world\n", name );
  }

the header: hello.h

  void say_hello( char* );

the C caller: caller.c

  #include <stdio.h>
  #include "hello.h"

  int main(int argc, char* argv[]) {
    if( argc > 1 ) {
      say_hello( argv[1] );
    } else {
      say_hello( "Stranger" );
    }
    return 0;
  }

the C# caller: caller.cs

  using System;
  using System.IO;
  using System.Runtime.InteropServices;

  struct my_wrapper {
    [DllImport ("libhello.so.1.0.1")]
    public static extern void say_hello( String name );
  };

  class caller {

    public static void Main( String[] args ) {
      if( args.Length > 0 ) {
        my_wrapper.say_hello( args[0] );
      } else {
        my_wrapper.say_hello( "Stranger" );
      }
    }
  };

the Makefile

  CC=gcc
  CFLAGS=-fPIC

  all: clean libs mono

  mono: caller.exe
          mono $<
          mono $< "Mr. Mono"

  %.exe: %.cs
          mcs $<

  libs: static_caller dynamic_caller
          ./static_caller
          ./static_caller "Mr. Static"
          ./dynamic_caller
          ./dynamic_caller "Mr. Dynamic"

  static_caller: hello.a caller.c
          ${CC} -static caller.c -L. -lhello -o $@

  dynamic_caller: hello.so caller.c
          gcc caller.c -o $@ -L. -lhello

  %.a: %.o
          ar rcs lib$@ $<

  %.o: %.c
          ${CC} ${CFLAGS} -c $< -o $@

  %.so: %.o
          ${CC} -shared -Wl,-soname,lib$@.1 -o lib$@.1.0.1  $<

  .PHONY: clean
  clean:
          @rm -f *~
          @rm -f *.o *.a

  dist: clean
          @rm -f *.so*
          @rm -f *.exe
          @rm -f *_caller

Action!

$ ls
caller.c  caller.cs  hello.c  hello.h  Makefile  README

$ make
gcc -fPIC -c hello.c -o hello.o
ar rcs libhello.a hello.o
gcc -static caller.c -L. -lhello -o static_caller
gcc -shared -Wl,-soname,libhello.so.1 -o libhello.so.1.0.1  hello.o
gcc caller.c -o dynamic_caller -L. -lhello
./static_caller
hello Stranger, welcome to the unmanaged world
./static_caller "Mr. Static"
hello Mr. Static, welcome to the unmanaged world
./dynamic_caller
hello Stranger, welcome to the unmanaged world
./dynamic_caller "Mr. Dynamic"
hello Mr. Dynamic, welcome to the unmanaged world
mcs caller.cs
mono caller.exe
hello Stranger, welcome to the unmanaged world
mono caller.exe "Mr. Mono"
hello Mr. Mono, welcome to the unmanaged world
rm hello.o

$ ls
caller.c   caller.exe      hello.c  libhello.a        Makefile  static_caller
caller.cs  dynamic_caller  hello.h  libhello.so.1.0.1  README

Categories: