Last active
December 9, 2015 17:36
-
-
Save SelvinPL/6afe8ae9d00c98694dc8 to your computer and use it in GitHub Desktop.
DllImport thiscall vc++ and gcc compatible???
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment