工控编程吧
标题:
上位机MFC检查动态库或程序是否被占用锁定源代码
[打印本页]
作者:
qq263946146
时间:
2019-10-9 11:18
标题:
上位机MFC检查动态库或程序是否被占用锁定源代码
(, 下载次数: 2)
上传
点击文件名下载附件
例程界面如上,
点击浏览按钮可以打开指定的程序或动态库来检查是否文件被占用。
被占用则显示相关信息,如图。
下面是关键代码。
void CWhoSLockingDlg::OnBrowseDllName()
{
CFileDialog oFileDialog(TRUE, // Open...
NULL, // default filename extension
GetDefaultFileName(), // initial filename
OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST, // options
"Executable Files (*.exe;*.dll;*.ocx)|*.exe; *.dll; *.ocx|All Files (*.*)|*.*||", // two filters for modules
this);
if ( oFileDialog.DoModal() == IDOK ) {
CString szModuleName=oFileDialog.GetPathName();
SetDefaultFileName(szModuleName);
(GetDlgItem(IDC_EDIT_MODULE_NAME))->SetWindowText(m_szDefaultFileName);
// Force refresh list
OnRefresh();
}
}
void CWhoSLockingDlg::OnRefresh()
{
// Store filename for next call
(GetDlgItem(IDC_EDIT_MODULE_NAME))->GetWindowText(m_szDefaultFileName);
CString szModuleName = m_szDefaultFileName;
// Find list of processes which loaded this module
DWORD dwFoundPID = 0;
int nRecords =0;
int nItemPosition = -1;
if (szModuleName.GetLength()) {
CMapStringToString PIDNameMap;
// Refresh GUI
m_oListOfProcesses.DeleteAllItems();
(GetDlgItem(IDC_MESSAGE_TEXT))->SetWindowText("Searching for process(es)...");
CWaitCursor oWaiting;
if (m_oProcessAPI.BuildProcessList(PIDNameMap)) {
// Find this module in all processes
CMapStringToString oLoadingProcessMap;
if (m_oProcessAPI.GetProcessesLockingModule(szModuleName, PIDNameMap, oLoadingProcessMap)) {
POSITION hProcessPosition = NULL;
CString ProcessName;
CString PIDString;
CString szExecutablePath;
CString szServiceName;
hProcessPosition = oLoadingProcessMap.GetStartPosition();
while ( hProcessPosition != NULL ) {
// Get key ( PIDString ) and value ( ProcessName )
oLoadingProcessMap.GetNextAssoc( hProcessPosition, PIDString, ProcessName );
// Get executable path
szExecutablePath = m_oProcessAPI.GetProcessExecutableName(atol(PIDString));
// Get service name (if applicable)
szServiceName = GetServiceName(szExecutablePath);
// Add process entry in the listbox
nItemPosition = m_oListOfProcesses.AddItem(nRecords,0,ProcessName);
if (nItemPosition != -1) {
m_oListOfProcesses.AddItem(nItemPosition,1,PIDString);
m_oListOfProcesses.AddItem(nItemPosition,2,szExecutablePath);
m_oListOfProcesses.AddItem(nItemPosition,3,szServiceName);
}
nRecords++;
}
}
} else {
AfxMessageBox("Fatal error: cannot build list of processes",
MB_OK|MB_ICONERROR);
}
CString szMessageText;
if (nRecords == 0) {
szMessageText = "No process found: This DLL is not locked.";
} else {
szMessageText.Format("%d process%s found.", nRecords, (nRecords<2?"":"es"));
}
(GetDlgItem(IDC_MESSAGE_TEXT))->SetWindowText(szMessageText);
}
}
复制代码
源代码下载地址:
(, 下载次数: 0)
上传
点击文件名下载附件
[MFC408]1[/MFC408]
[halcon]1[/halcon]
[weixinlianxi]1[/weixinlianxi]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4