Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active December 9, 2015 17:36
Show Gist options
  • Select an option

  • Save SelvinPL/6afe8ae9d00c98694dc8 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/6afe8ae9d00c98694dc8 to your computer and use it in GitHub Desktop.
DllImport thiscall vc++ and gcc compatible???
using System;
using System.Runtime.InteropServices;
namespace SharedObject1Test
{
class Program
{
public class SharedObject1Proxy : IDisposable
{
#if GCC
private const string LIBRARY_NAME = "libSharedObject1.so";
private const string ADD_ENTRY_POINT = "_ZN13SharedObject13AddEii";
private const string MULTIPLY_ENTRY_POINT = "_ZN13SharedObject18MultiplyEii";
//for gcc use "nm -D libSharedObject1.so" to get exports
#else
private const string LIBRARY_NAME = "SharedObject1.dll";
private const string ADD_ENTRY_POINT = "?Add@SharedObject1@@QAEHHH@Z";
private const string MULTIPLY_ENTRY_POINT = "?Multiply@SharedObject1@@QAEHHH@Z";
//for vc++ use "dumpbin /EXPORTS SharedObject1.dll" to get exports
#endif
[DllImport(LIBRARY_NAME, EntryPoint = ADD_ENTRY_POINT, CallingConvention = CallingConvention.ThisCall)]
private static extern int AddImport(IntPtr obj, int a, int b);
[DllImport(LIBRARY_NAME, EntryPoint = MULTIPLY_ENTRY_POINT, CallingConvention = CallingConvention.ThisCall)]
private static extern int MultiplyImport(IntPtr obj, int a, int b);
[DllImport(LIBRARY_NAME)]
private static extern IntPtr NewSharedObject1();
private readonly IntPtr thizz;
public SharedObject1Proxy()
{
thizz = NewSharedObject1();
}
[DllImport(LIBRARY_NAME)]
private static extern int DeleteSharedObject1(IntPtr obj);
public void Dispose()
{
DeleteSharedObject1(thizz);
}
public int Multiply(int a, int b)
{
return MultiplyImport(thizz, a, b);
}
public int Add(int a, int b)
{
return AddImport(thizz, a, b);
}
}
static void Main(string[] args)
{
using (var sharedObject1 = new SharedObject1Proxy())
{
Console.WriteLine("3*4={0}", sharedObject1.Multiply(3, 4));
Console.WriteLine("3+4={0}", sharedObject1.Add(3, 4));
}
}
}
}
#define SHAREOBJECT_EXPORTS
#include "SharedObject1.h"
SharedObject1::SharedObject1()
{
}
SharedObject1::~SharedObject1()
{
}
int SharedObject1::Multiply(int a, int b)
{
return (a*b);
}
int SharedObject1::Add(int a, int b)
{
return (a+b);
}
SharedObject1* NewSharedObject1()
{
return new SharedObject1();
}
void DeleteSharedObject1(SharedObject1* obj)
{
delete obj;
}
#ifndef SHAREDOBJECT1_H
#define SHAREDOBJECT1_H
#if defined _WIN32 || defined __CYGWIN__
#ifdef SHAREOBJECT_EXPORTS
#ifdef __GNUC__
#define SHAREOBJECT_API_PUBLIC __attribute__ ((dllexport))
#else
#define SHAREOBJECT_API_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
#endif
#else
#ifdef __GNUC__
#define SHAREOBJECT_API_PUBLIC __attribute__ ((dllimport))
#else
#define SHAREOBJECT_API_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
#endif
#endif
#define SHAREOBJECT_API_LOCAL
#else
#if __GNUC__ >= 4
#define SHAREOBJECT_API_PUBLIC __attribute__ ((visibility ("default")))
#define SHAREOBJECT_API_LOCAL __attribute__ ((visibility ("hidden")))
#else
#define SHAREOBJECT_API_PUBLIC
#define SHAREOBJECT_API_LOCAL
#endif
#endif
class SHAREOBJECT_API_PUBLIC SharedObject1
{
public:
SharedObject1();
~SharedObject1();
int Add(int a, int b);
int Multiply(int a, int b);
};
extern "C"
{
SHAREOBJECT_API_PUBLIC SharedObject1* NewSharedObject1();
SHAREOBJECT_API_PUBLIC void DeleteSharedObject1(SharedObject1*);
}
#endif /* !SHAREDOBJECT1_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment