mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.14k stars 518 forks source link

Wrapping 64bit DLLs #139

Closed tomspilman closed 10 years ago

tomspilman commented 10 years ago

So when trying to wrap a 64bit DLL the generator made the wrapper like this:

        [StructLayout(LayoutKind.Explicit, Size = 4)]
        public struct Internal
        {
           ...
        }

        int CppSharp.Runtime.ICppMarshal.NativeDataSize
        {
            get { return 4; }
        }

        public MyClass()
        {
            __Instance = Marshal.AllocHGlobal(4);
            Internal.MyClass_0(__Instance);
        }

Causing memory corruption when calling the 64bit library. Manually replacing all the '4's with '8's fixed it.

How do I define for my ILibrary that my DLL is 64bit?

tritao commented 10 years ago

You need to change the Clang target, IIRC the compiled builds use "i686-pc-win32" by default. Try with "x86_64-pc-win64" if you want to target the MSVC-like 64-bit subset or replace with "mingw32" or "mingw64" for MinGW toolset.

Let me know if it works, we should improve the documentation on this point.

tomspilman commented 10 years ago

So it looks like adding the following to my ILibrary...

options.TargetTriple = "x86_64";

... is enough and it figures out the rest. I can verify that sizeof(MyClass) in C++ matches what the wrapper generates.

Thanks!