上位机MFC检查动态库或程序是否被占用锁定源代码
例程界面如上,
点击浏览按钮可以打开指定的程序或动态库来检查是否文件被占用。
被占用则显示相关信息,如图。
下面是关键代码。
- 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);
- }
- }
复制代码 源代码下载地址:
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|