写一段利用MFC的代码通过加载DLL7000dll实现文本框输入数值点击开始按钮三相源升源到该数值点击停止按钮源停止输出该过程输出的日志会在文本框输出展示
// 首先定义一个指向DLL中函数的指针 typedef void(*Start)(int val); typedef void(*Stop)();
// 在ButtonClick事件中加载DLL,并获取需要调用的函数指针 void CMyDlg::OnBtnClick() { HMODULE hModule = LoadLibrary(_T("DLL7000.dll")); if (hModule == NULL) { AfxMessageBox(_T("无法加载DLL文件")); return; }
Start startFunc = (Start)GetProcAddress(hModule, "Start");
Stop stopFunc = (Stop)GetProcAddress(hModule, "Stop");
if (startFunc == NULL || stopFunc == NULL) {
AfxMessageBox(_T("无法获取DLL中需要调用的函数"));
FreeLibrary(hModule);
return;
}
// 从文本框中获取需要设置的数值
CString strVal;
m_edit.GetWindowText(strVal);
int val = _ttoi(strVal);
// 调用DLL中的函数开始输出
startFunc(val);
// 将日志输出到文本框中
CString strLog = _T("开始输出,输出值为: ");
strLog += strVal;
m_log.SetWindowText(strLog);
// 保存句柄和函数指针
m_hModule = hModule;
m_startFunc = startFunc;
m_stopFunc = stopFunc;
}
// 在StopButtonClick事件中调用DLL中的函数停止输出,并释放DLL文件 void CMyDlg::OnStopBtnClick() { m_stopFunc();
// 将日志输出到文本框中
CString strLog = _T("停止输出");
m_log.SetWindowText(strLog);
// 释放DLL文件
FreeLibrary(m_hModule);
原文地址: http://www.cveoy.top/t/topic/enz8 著作权归作者所有。请勿转载和采集!