操作环境:windwos10 & vs2017 & EasyX Library for C++ (Ver:20220116)
我希望可以在dll中进行绘制,我可以将点坐标传给参数,绘图与保存完全在dll中实现,然后通过接口去调用
在EasyX中,设备分两种,分别做了一种是默认的绘图窗口,另一种是IMAGE对象的测试
在同一台设备相同环境下,我将这段代码在dll导出程序export工程里以dll方式导出,在控制台应用工程load里进行调用时,可以成功执行绘图并保存,
但是执行完后却无法释放dll导致程序阻塞或者崩溃,我猜测可能是在绘图结束后有内存没有释放,但是无法找出问题点在哪里
//windwos10 && vs2017 && EasyX Library for C++ (Ver:20220116)
//在EasyX中,设备分两种,分别做了一种是默认的绘图窗口,另一种是IMAGE对象的测试
//工程A 动态导出库ExportApi
//ExportApi.h
#include <vector>
extern "C" __declspec(dllexport) int ExportEasyXImg(std::vector<POINT>& points);
extern "C" __declspec(dllexport) int ExportEasyXWind(std::vector<POINT>& points);
//ExportApi.cpp
//#include "ExportApi.h"
#include "graphics.h"
extern "C" __declspec(dllexport) int ExportEasyXImg(std::vector<POINT>& points)//IMAGE对象
{
IMAGE img(600, 600);
SetWorkingImage(&img);
ellipse(200, 200, 100, 100);
//polygon(&points[0], points.size());//我希望如此,demo仅作简单测试,并没有传参,但依然有问题
saveimage(_T("E://ouput.png"), &img);
return 1;
}
extern "C" __declspec(dllexport) int ExportEasyXWind(std::vector<POINT>& points)//默认的绘图窗口
{
initgraph(640, 600);
rectangle(200, 200, 100, 100);
saveimage(_T("wind.png"));
closegraph();
return 1;
}
//工程B 控制台应用程序Load
//load.cpp
#include <iostream>
#include <Windows.h>
#include <vector>
using std::cout;
using std::endl;
typedef int(*Func_EasyXImage)(std::vector<POINT>& points);
typedef int(*Func_EasyXWind)(std::vector<POINT>& points);
int LoadApiFunc_EasyXImage()
{
std::vector<POINT> points;
HINSTANCE h = LoadLibraryA("ExportApi.dll");
if (h)
{
Func_EasyXImage func_byimage = (Func_EasyXImage)GetProcAddress(h, "ExportEasyXImg");
if (func_byimage)
{
int i_rec = func_byimage(points);
if (i_rec == 1)
{
cout << "load draw by image success!" << endl;
}
}
else
{
cout << "ExportEasyXImg Interface is error!" << endl;
}
}
else {
cout << "load dll error!" << endl;
}
FreeLibrary(h);
//问题是卡在了dll的释放这里
//应该是EasyX在绘图结束后有内存没有释放掉,但是追不到到底哪里没有释放
cout << "free dll is success!";
return 1;
}
int LoadApiFunc_EasyXWind()
{
std::vector<POINT> points;
HINSTANCE h = LoadLibraryA("ExportApi.dll");
if (h)
{
Func_EasyXWind func_bywind = (Func_EasyXWind)GetProcAddress(h, "ExportEasyXWind");
if (func_bywind)
{
int i_rec = func_bywind(points);
if (i_rec == 1)
{
cout << "load draw by Wind success!" << endl;
}
}
else
{
cout << "ExportEasyXWind Interface is error!" << endl;
}
}
else {
cout << "load dll error!" << endl;
}
FreeLibrary(h);
//问题也是卡在了dll的释放这里
cout << "free dll is success!";
return 1;
}
int main()
{
int recode = LoadApiFunc_EasyXImage();
if (recode != 1)
{
cout << "FreeDll is error!";
}
int recode2 = LoadApiFunc_EasyXWind();
if (recode2 != 1)
{
cout << "FreeDll is error!";
}
return 1;
}