Wrapping using an unmanaged interface

I have an unmanaged library displaying some interfaces. Users can implement interfaces and insert them into the library with their own implementation.

I would like to provide a managed wrapper for this library. Facilitating an unmanaged interface with a manageable one is easy. But in my case, I would like to support custom implementations of various interfaces, which means that I need to take a managed implementation of the interface and wrap it with its unmanaged instance before I send it to the depths of the unmanaged part of the library.

I tried something like:

class UnmanagedWrapper {
DoSomething() {m_clr.DoSomething();}
IManaged^ m_clr;
}

But I cannot manage members in an unmanaged class, the compiler rightfully claimed.

Can I do something elgant here?

+5
source share
3 answers

Here is some related information to work around when one library is unmanaged and the managed language uses these libraries.

The context of this information is a way to use GoogleTest in Visual Studio:

Getting Started with Google C ++ Testing Framework

Visual ++. main() .exe, . - Visual ++. , Google Test . , . Visual ++ , . , . . - :

__declspec (dllexport) int PullInMyLibrary() { return 0; }

( DLL), __declspec (dllexport)

. , , , :

   int PullInMyLibrary(); 
   static int dummy = PullInMyLibrary();

.

, , /OPT: NOREF . MSV++ IDE, .exe/ // Keep (/OPT: NOREF). , Visual ++ , , .

. Google Test (, gtest.vcproj), . DLL, Google Test DLL. . : - !

0

gcroot < > - , :

class UnmanagedWrapper {
    DoSomething() {m_clr.DoSomething();}
    gcroot<IManaged^> m_clr;
}
0

dll #.Net. , , . dumpbin.exe/EXPORTS, / EntryPoint dll. :

using System;
using System.Runtime.InteropServices;

namespace aviationLib
{
    public struct MAGtype_GeoMagneticElements
    {
        public double Decl; /* 1. Angle between the magnetic field vector and true north, positive east*/
        public double Incl; /*2. Angle between the magnetic field vector and the horizontal plane, positive down*/
        public double F; /*3. Magnetic Field Strength*/
        public double H; /*4. Horizontal Magnetic Field Strength*/
        public double X; /*5. Northern component of the magnetic field vector*/
        public double Y; /*6. Eastern component of the magnetic field vector*/
        public double Z; /*7. Downward component of the magnetic field vector*/
        public double GV; /*8. The Grid Variation*/
        public double Decldot; /*9. Yearly Rate of change in declination*/
        public double Incldot; /*10. Yearly Rate of change in inclination*/
        public double Fdot; /*11. Yearly rate of change in Magnetic field strength*/
        public double Hdot; /*12. Yearly rate of change in horizontal field strength*/
        public double Xdot; /*13. Yearly rate of change in the northern component*/
        public double Ydot; /*14. Yearly rate of change in the eastern component*/
        public double Zdot; /*15. Yearly rate of change in the downward component*/
        public double GVdot; /*16. Yearly rate of change in grid variation*/
    };

    public class Declination
    {
        [DllImport("wmm.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "?GeoMagneticElements@@YAPAUMAGtype_GeoMagneticElements@@MHHMMM@Z")]
        public static extern IntPtr GeoMagneticElements(float sdate, int igdgc, int units, float alt, float latitude, float longitude);

        public MAGtype_GeoMagneticElements e;

        public MAGtype_GeoMagneticElements MagDeclination(float decimalLat, float decimalLon)
        {

           try
            {
                String d = DateTime.Now.Year.ToString("D4") + '.' + DateTime.Now.Day.ToString("D1");
                IntPtr pnt = GeoMagneticElements((float)Convert.ToDouble(d), 1, 3, 3000.0f, decimalLat, decimalLon);
                e = Marshal.PtrToStructure<MAGtype_GeoMagneticElements>(pnt);
            }
            catch (System.EntryPointNotFoundException se)
            {
                Console.WriteLine(se.Message);
            }

            return e;
        }
    }
}

The meaning of the word should be the same for both managed and unmanaged, so if the dll is 32-bit, then .NET should also be.

0
source

All Articles