标定等算法应该要注意保密和不被随意修改,所以用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);
}
}
多谢,亲爱的美美。