C++ package Halcon algorithm dynamic link library Dll supplement

I wrote an article about C++ package DLL earlier, here is a supplement.
1. I won’t say more about how to configure the halcon and opencv libraries. The previous article introduced it in detail. The following describes what is added to the package.
1.1 First create the class function1, and write function.h and function1.cpp codes.
insert image description here

function1.h code

#pragma once
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <windows.h>
#include <thread>
#include <vector>
#include <algorithm>
#include <Halcon.h>
#include <HalconCpp.h>

using namespace std;
using namespace HalconCpp;


enum ErrorType
{
    
    
	Intrusion = 0,//缺口
	Protrusion,//突起
	CornerMiss//缺角

};
struct mStruct
{
    
    
	//输入参数
	int id;
	HObject Img;
	int threshold_Min;
	int threshold_Max;
	//输出参数
	double &Area;
};
class function1
{
    
    
public:
	function1();
	~function1();
public:
	int add(int a, int b);
	int subtract(int a, int b);
	//Halcon算法测试
	int TransferHobj_Test(mStruct &Struct1);

};

function1.cpp code

#include "pch.h"
#include "function1.h"
function1::function1(){
    
    }
function1::~function1(){
    
    }
int function1::add(int a,int b)
{
    
    
	return a + b;
}
int function1::subtract(int a, int b)
{
    
    
	return a - b;
}
//传递Hobject图像测试函数
int function1::TransferHobj_Test(mStruct &Struct1)
{
    
    
	try
	{
    
    
		HObject  ho_Regions, ho_ConnectedRegions;
		HObject  ho_SelectedRegions;
		HTuple  hv_Number, hv_Area, hv_Row, hv_Column;
		Threshold(Struct1.Img, &ho_Regions, Struct1.threshold_Min, Struct1.threshold_Max);
		Connection(ho_Regions, &ho_ConnectedRegions);
		SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 1.19836e+007,
			2e+007);
		CountObj(ho_SelectedRegions, &hv_Number);
		if (0 != (hv_Number == 1))
		{
    
    
			AreaCenter(ho_SelectedRegions, &hv_Area, &hv_Row, &hv_Column);
			Struct1.Area = hv_Area.D();
		}
		else
		{
    
    
			Struct1.Area = 0;
		}
		return 0;
	}
	catch (...)
	{
    
    
		return 1;
	}
}

1.2 Create DllEntry.h and DllEntry.cpp respectively as host computer call interface classes, open the project properties, find the C/C++ preprocessor, and enter the preprocessor definition. Add MYDLL at the bottom. Then edit the DllEntry.h and DllEntry.cpp codes. Add the following code to the header file, and then write other code

#ifdef MYDLL // 用来导出函数
#define DLLVLC_API __declspec(dllexport)
#else // 用来标识为导入函数,对于引用该头文件的外部模块来说dllimport这个标记对编译优化有作用
#define DLLVLC_API __declspec(dllimport)
#endif

insert image description here

DllEntry.h code

#pragma once
#ifdef MYDLL // 用来导出函数
#define DLLVLC_API __declspec(dllexport)
#else // 用来标识为导入函数,对于引用该头文件的外部模块来说dllimport这个标记对编译优化有作用
#define DLLVLC_API __declspec(dllimport)
#endif

#include <iostream>
#include <vector>
#include <stdio.h>
#include "string"
#include "function1.h"
#include <Halcon.h>
#include <HalconCpp.h>

using namespace HalconCpp;

namespace TestVLCDLL 
{
    
    
	extern "C" 
	{
    
    
		DLLVLC_API int myAddFunction_interface(int a, int b);
		DLLVLC_API int mySubtractFunction_interface(int a, int b);
		DLLVLC_API int TransferHobj_Test_interface(mStruct Struct1);
	}
}

DllEntry.cpp code

#include "pch.h"
#include "DllEntry.h"
#include <fstream>
#include <thread>
#include <mutex>
using namespace std;
//声明一个线程锁,用于做线程同步
std::mutex mutexx1, mutexx2;
DLLVLC_API int TestVLCDLL::myAddFunction_interface(int a, int b)
{
    
    
	mutexx1.lock();
	function1 f;
	return f.add(a, b);
	mutexx1.unlock();
}
DLLVLC_API int TestVLCDLL::mySubtractFunction_interface(int a, int b)
{
    
    
	mutexx2.lock();
	function1 f;
	return f.subtract(a, b);
	mutexx2.unlock();
}
DLLVLC_API int TestVLCDLL::TransferHobj_Test_interface(mStruct Struct1)
{
    
    
	function1 f;
	return f.TransferHobj_Test(Struct1);
}

The meaning of this code is that MYDLL is defined in advance in my own project properties, so DLLVLC_API __declspec(dllexport) will be compiled when the project is compiled, but MYDLL is not defined in advance when the host computer calls, when the host computer code is compiled DLLVLC_API __declspec(dllimport) will be compiled. It is said that the dllimport flag has an effect on compilation optimization. However, after the actual measurement of my project, in fact, I don’t write the code in the header file, and write it directly according to the following code, which is also
extern “C” _declspec (dllexport) int TransferHobj_Test_interface(mStruct Struct1);
2. Create a new C++ console The program TestDll tests the packaged dll.
2.1 Create a project solution
insert image description here
2.2 Put all the packaged things into the execution directory of TestDll, including the following files Dll1.dll, Dll1.lib, DllEntry.h, function1.h.
Then configure C/C++ general additional include directories, linker general additional library directories, and linker input additional dependencies.
insert image description here
insert image description here
insert image description here

main() calls the dll code

int main()
{
    
    
	//引用Dll封装库的命名空间
	using namespace TestVLCDLL;
	int ret = TestVLCDLL::myAddFunction_interface(1,2);
	ret = mySubtractFunction_interface(10,5);

	//测试C++封装的函数
	int a = 0;

	HObject img;
	ReadImage(&img,"F:\\SanYuan_Image\\DaTu20221102-1\\1\\0.bmp");
	double Area = 1;
	mStruct struct1 =
	{
    
    
		//输入参数
		10,
		img,
		65,
		100,
		//输出参数
		Area
	};
	a = TestVLCDLL::TransferHobj_Test_interface(struct1);
}

Guess you like

Origin blog.csdn.net/Douhaoyu/article/details/128790601