当前位置:网站首页>调用DLL开启线程的问题
调用DLL开启线程的问题
2022-07-26 09:25:00 【asiwxy】
错误的编写方式:(注意用感叹号夹起来的两行)在 ::FreeLibrary(hPro);这一行代码执行之后,dll里面的分配的内存空间就会全部被释放掉,所以会报“帧不在模块之中”,“内存访问错误”之类的错误。
void CMFCApplication2Dlg::OnBnClickedOk()
{
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
HINSTANCE hPro = ::LoadLibrary("MFCLibrary1.dll");//加载一个自己写的DLL,提供了一
//getCFindFile用于获取一个类指
//针;
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (hPro == NULL)
{
return ;
}
else
{
typedef MyCFindFile* (*MENU_EXECUTE)();
MENU_EXECUTE getCFindFile = NULL;
getCFindFile = (MENU_EXECUTE)::GetProcAddress(hPro, "getCFindFile");//MenuExecute 函数名字
if (getCFindFile)
{
MyCFindFile * pItem = getCFindFile();
CString s;
GetDlgItemText(IDC_EDIT4,s);
s.Format(_T("%s"), s);
std::string strStr;
strStr = s.GetBuffer(0);
HWND hdlg = GetSafeHwnd();
pItem->funcFindFile(strStr, hdlg, 2);
//pItem->m_hMainDlg = GetSafeHwnd();//把当前主对话框的窗口句柄传过去,这样就可以在线程函数中使用
//m_ResultList.DeleteAllItems();//清空上一次执行完之后的信息
//std::string strStr;
//strStr = pItem->m_PathFind.GetBuffer(0);
//funcFindFile(strStr, pItem->m_hMainDlg);
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
::FreeLibrary(hPro);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
return;
}
正确的写法:
1,将HINSTANCE hPro;添加到类成员变量之中
2,将FreeLibrary(hPro);函数放到MFC退出按钮里面,反正要保证在使用完hpro之前不要释放掉,也可以直接用静态加载
边栏推荐
猜你喜欢
Sending and receiving of C serialport
C# Serialport的发送和接收
Redis principle and use - Basic Features
arc-gis的基本使用2
The child and binary tree- open root inversion of polynomials
Li Mu D2L (VI) -- model selection
【线上问题】Timeout waiting for connection from pool 问题排查
神经网络与深度学习-6- 支持向量机1 -PyTorch
【Mysql】redo log,undo log 和binlog详解(四)
[MySQL] how to execute an SQL statement (2)
随机推荐
What is asynchronous operation
Redis principle and usage - installation and distributed configuration
2022 chemical automation control instrument operation certificate test question simulation test platform operation
PHP一次请求生命周期
滑动窗口、双指针、单调队列、单调栈
微信小程序学习笔记1
大二上第二周学习笔记
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
JVM触发minor gc的条件
Windows backs up the database locally by command
arcgis的基本使用4
JS output diamond on the console
2B and 2C
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Your login IP is not within the login mask configured by the administrator
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
(2006, MySQL server has gone away) problem handling
When you click input, the border is not displayed!
jvm命令归纳
Personality test system V1.0