CSharp调用c++的标定库DLL方式

标定等算法应该要注意保密和不被随意修改,所以用C++写标定库算法,

再打包成DLL给CSharp用是一种比较好的方法。

--------------------------------------------------------------------------------------------

参考

---------

DllImport使用详解

https://blog.csdn.net/cyj380236628/article/details/49825007

C#调用DLL各种传参

https://www.cnblogs.com/ahuo/p/5457420.html

--------------------------------------------------------------------------------------------

把接口放在myDLL_interface类中,一定要有myDLL_interface.cpp

接口用C形式的,

# define C_TYPE_EXPORT extern "C" __declspec(dllexport)
#ifndef MYDLL_GLOBAL_H
#define MYDLL_GLOBAL_H

#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#  define Q_DECL_EXPORT __declspec(dllexport)
#  define Q_DECL_IMPORT __declspec(dllimport)
#else
#  define Q_DECL_EXPORT     __attribute__((visibility("default")))
#  define Q_DECL_IMPORT     __attribute__((visibility("default")))
#endif

#if defined(MYDLL_LIBRARY)
#  define MYDLL_EXPORT Q_DECL_EXPORT
#else
#  define MYDLL_EXPORT Q_DECL_IMPORT
#endif

# define C_TYPE_EXPORT extern "C" __declspec(dllexport)

#endif // MYDLL_GLOBAL_H

接口函数例子:

C_TYPE_EXPORT bool initSimpleCalibModule(char* strIdentification)
{
    for(int i=0; i<simpleNum; i++)
    {
        SCM[i].setName("");
        SCM[i].setIdentification(strIdentification);
    }

    mapSCM.clear();

    return true;
}

C_TYPE_EXPORT bool initSimpleCalib(char* strName)
{
    for(int i=0; i<simpleNum; i++)
    {
        if(SCM[i].getName() == "")
        {
            SCM[i].setName(strName);
            mapSCM.insert(std::make_pair(i, SCM[i].getName()));
            return true;
        }

    }

    return false;
}

C_TYPE_EXPORT bool resetName(char* strOldName, char* strNewName)
{
    std::map<int, std::string>::iterator iter;
    iter = mapSCM.begin();
    for(; iter!=mapSCM.end(); iter++)
    {
        if(iter->second == strOldName)
        {
            SCM[iter->first].setName(strNewName);
            mapSCM[iter->first] = strNewName;
            return true;
        }
    }

    return false;
}

C_TYPE_EXPORT  bool setCalibPoints(char* strName, CALIBPOINTS *src)
{
    std::map<int, std::string>::iterator iter;
    iter = mapSCM.begin();
    for(; iter!=mapSCM.end(); iter++)
    {
        if(iter->second == strName)
        {
            return SCM[iter->first].setCalibPoints(src);
        }
    }

    return false;
}

C_TYPE_EXPORT  bool getCalibPoints(char* strName, CALIBPOINTS &src)
{
    std::map<int, std::string>::iterator iter;
    iter = mapSCM.begin();
    for(; iter!=mapSCM.end(); iter++)
    {
        if(iter->second == strName)
        {
            return SCM[iter->first].getCalibPoints(src);
        }
    }

    return false;
}

C_TYPE_EXPORT  bool calcPos(char* strName, CALIBPOINT &src)
{
    std::map<int, std::string>::iterator iter;
    iter = mapSCM.begin();
    for(; iter!=mapSCM.end(); iter++)
    {
        if(iter->second == strName)
        {
            return SCM[iter->first].calcPos(src);
        }
    }

    return false;
}

数组,字符,double int等等,都可以方便传递。注意引用 和指针的区别和使用。

typedef struct
{
    double Mx;
    double My;
    double Mr;
    double Px;
    double Py;
}CALIBPOINT;

数组也是C的形式的。

在代码中其实都可以随意用C++的,只要在接口相关的地方换用C的就可以了。

之后把生成的DLL放到CSharp中用就可以了。

namespace SimpleCalculate
{
    public class SimpleCalibInterface
    {
        [DllImport("myDLL.dll", EntryPoint = "initSimpleCalibModule", CharSet = CharSet.Ansi)]
        public static extern bool initSimpleCalibModule(string passWord);
        [DllImport("myDLL.dll", EntryPoint = "initSimpleCalib", CharSet = CharSet.Ansi)]
        public static extern bool initSimpleCalib(string strName);
        [DllImport("myDLL.dll", EntryPoint = "resetName", CharSet = CharSet.Ansi)]
        public static extern bool resetName(string strOldName, string strNewName);
        [DllImport("myDLL.dll", EntryPoint = "setCalibPoints", CharSet = CharSet.Ansi)]
        public static extern bool setCalibPoints(string strName, ref CalibPoints src);
        [DllImport("myDLL.dll", EntryPoint = "getCalibPoints", CharSet = CharSet.Ansi)]
        public static extern bool getCalibPoints(string strName, ref CalibPoints src);
        [DllImport("myDLL.dll", EntryPoint = "calcPos", CharSet = CharSet.Ansi)]
        public static extern bool calcPos(string strName, ref CalibPoint src);
        [DllImport("myDLL.dll", EntryPoint = "getMaximum", CharSet = CharSet.Ansi)]
        public static extern double getMaximum(string strName);
        [DllImport("myDLL.dll", EntryPoint = "getRationX", CharSet = CharSet.Ansi)]
        public static extern double getRationX(string strName);
        [DllImport("myDLL.dll", EntryPoint = "getRationY", CharSet = CharSet.Ansi)]
        public static extern double getRationY(string strName);
        [DllImport("myDLL.dll", EntryPoint = "getCrossX", CharSet = CharSet.Ansi)]
        public static extern double getCrossX(string strName);
        [DllImport("myDLL.dll", EntryPoint = "getCrossY", CharSet = CharSet.Ansi)]
        public static extern double getCrossY(string strName);
    }
}

多谢,亲爱的美美。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值