C# calls the method of C++ class library

Reference: https://www.cnblogs.com/lgyup/p/7116162.html

1. Directly call the public methods in the C++ class library

Use the DllImport feature to call the method, such as a public method in the C++ class library SampleCppWrapper.dll:

extern "C" __declspec(dllexport) int __stdcall Add(int n1, int n2);

__stdcall indicates the calling convention: parameters are passed  through the stack from right to left , and the function call must be cleaned up by the callee before returning.

In C#, the call is as follows:

[DllImport("SampleCppWrapper.dll")]
private static extern int Add(int n1, int n2);

Pay attention to the type of the parameter, after that, you can use this method directly in C# programming.

2. Call the method of the class in the C++ class library

C# cannot directly call the classes in the C++ class library. A flexible solution is needed. By creating another C++ class library, the member methods of the class to be called are exposed (converted into public methods), such as the following C++ class:

SampleCppClass.h

#pragma once

class __declspec(dllexport) SampleCppClass
{
public:
    SampleCppClass(void);
    ~SampleCppClass(void);
    
    int Add(int n1, int n2);
    int Sub(int n1, int n2);
};

SampleCppClass.cpp

#include "SampleCppClass.h"

SampleCppClass::SampleCppClass(void)
{
}

SampleCppClass::~SampleCppClass(void)
{
}

int SampleCppClass::Add(int n1, int n2)
{
    return n1 + n2;
}

int SampleCppClass::Sub(int n1, int n2)
{
    return n1 - n2;
}

We want to call the Add and Sub methods in SampleCppClass, so we write another C++ class library to indirectly call class member methods through public methods:

SampleCppWrapper.h

#pragma once

#include "..\SampleCppClass\SampleCppClass.h"

namespace SampleCppWrapper
{
    extern "C" __declspec(dllexport) int __stdcall Add(int n1, int n2);
    extern "C" __declspec(dllexport) int __stdcall Sub(int n1, int n2);
}

SampleCppWrapper.cpp

#include "SampleCppWrapper.h"

namespace SampleCppWrapper
{
    SampleCppClass* g_pObj = new SampleCppClass();

    int __stdcall Add(int n1, int n2)
    {
        return g_pObj->Add(n1, n2);
    }

    int __stdcall Sub(int n1, int n2)
    {
        return g_pObj->Sub(n1, n2);
    }
}

In C#, call the public method in SampleCppWrapper.dll:

[DllImport("SampleCppWrapper.dll")]
private static extern int Add(int n1, int n2);
[DllImport("SampleCppWrapper.dll")]
private static extern int Sub(int n1, int n2);

3. Use the callback function in the C++ class library

The C++ callback function is an event response mechanism, similar to the C# delegate, such as a callback function in a C++ class:

SampleCppClass.h

#pragma once

typedef void (*LoopCallback)(void* pContext);

class __declspec(dllexport) SampleCppClass
{
public:
    SampleCppClass(void);
    ~SampleCppClass(void);
    
    void SetCallbackFunc(LoopCallback callback);
    void SetCallbackContext(void* pContext);
    void Loop();
private:
    LoopCallback m_callback;
    void* m_pContext;
};

SampleCppClass.cpp

#include "SampleCppClass.h"

SampleCppClass::SampleCppClass(void)
{
}

SampleCppClass::~SampleCppClass(void)
{
}

void SampleCppClass::SetCallbackFunc(LoopCallback callback)
{
    m_callback = callback;
}

void SampleCppClass::SetCallbackContext(void* pContext)
{
    m_pContext = pContext;
}

void SampleCppClass::Loop()
{
    for (int i=0; i<10; i++)
    {
        if (m_callback != NULL)
        {
            m_callback(m_pContext);
        }
    }
}

We write another class library in C++ for encapsulation, and expose the methods in the class:

SampleCppWrapper.h

#pragma once

#include "..\SampleCppClass\SampleCppClass.h"

namespace SampleCppWrapper
{
    typedef void (__stdcall *LoopCallbackWrapper)(void* pContext);

    extern "C" __declspec(dllexport) void __stdcall SetCallbackFunc(LoopCallbackWrapper callback);
    extern "C" __declspec(dllexport) void __stdcall SetCallbackContext(void* pContext);
    extern "C" __declspec(dllexport) void __stdcall Loop();
}

SampleCppWrapper.cpp

#include "SampleCppWrapper.h"

namespace SampleCppWrapper
{
    LoopCallbackWrapper g_callbackWrapper;
    SampleCppClass* g_pObj = new SampleCppClass();

    void LoopCallbackFunc(void* pContext);

    void __stdcall SetCallbackFunc(LoopCallbackWrapper callback)
    {
        g_callbackWrapper = callback;
        g_pObj->SetCallbackFunc(LoopCallbackFunc);
    }

    void __stdcall SetCallbackContext(void* pContext)
    {    
        g_pObj->SetCallbackContext(pContext);
    }

    void __stdcall Loop()
    {
        g_pObj->Loop();
    }

    void LoopCallbackFunc(void* pContext)
    {
        if (g_callbackWrapper != NULL)
        {
            g_callbackWrapper(pContext);
        }
    }
}

Then, call it in C#:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SampleCsTest
{
    public partial class Form1 : Form
    {
        [StructLayout(LayoutKind.Sequential)]
        private class Context
        {
            public Form1 Form { get; set; }
        }

        private delegate void LoopCallbackHandler(IntPtr pContext);
        private static LoopCallbackHandler callback = LoopCallback;

        [DllImport("SampleCppWrapper.dll")]
        private static extern void SetCallbackFunc(LoopCallbackHandler callback);
        [DllImport("SampleCppWrapper.dll")]
        private static extern void SetCallbackContext(IntPtr pContext);
        [DllImport("SampleCppWrapper.dll")]
        private static extern void Loop();

        private Context ctx = new Context();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SetCallbackFunc(callback);
            ctx.Form = this;
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ctx));
            Marshal.StructureToPtr(ctx, ptr, false);
            SetCallbackContext(ptr);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Loop();
        }

        private static void LoopCallback(IntPtr pContext)
        {
            Context ctx = (Context)Marshal.PtrToStructure(pContext, typeof(Context));
            ctx.Form.textBox1.Text += "callback" + Environment.NewLine;
        }
    }
}

Guess you like

Origin blog.csdn.net/u014090257/article/details/124342382