Lab 7 Guide: 멀티스레딩 예제 1 * 멀티스레딩을 이용한 파일 복사 프로그램 만들기 (교재 15장, 820-824쪽)
실습 목적 및 장비 실습 목적 장비: PC, 윈도우 운영체제, Visual Studio (VC++ 6.0) 멀티스레드 프로그램의 작성 방법 스레드의 생성 Shell API의 이해 및 사용 방법 장비: PC, 윈도우 운영체제, Visual Studio (VC++ 6.0)
* Shell Programming Programming the Shell (using the Shell API) The Microsoft® Windows® user interface (UI) provides users with access to a wide variety of objects necessary for running applications and managing the operating system. Programming the Shell (using the Shell API) How to navigate the namespace and find files and folders. How to launch applications. How to transfer Shell objects with Clipboard and drag-and-drop operations. Extending the Shell (customizing or extending the behavior of the Shell by modifying the registry or creating special files) How to define a file name extension as a file class and associate it with an application. How to customize a file class's icon and shortcut menu. How to customize the appearance of a folder. How to use Autorun to automatically launch a CD-ROM application.
Shell Programming Intermediate Shell Techniques How to create links and shortcuts. How to use the taskbar. How to use application desktop toolbars. How to create Shell extension handlers to dynamically modify a variety of Shell behaviors, including the icons and shortcut menus associated with a class of files. How to integrate your application's cleanup procedures with the Shell's disk cleanup manager. How to customize the way Webview displays the contents of a folder. Advanced Shell Techniques How to create a custom virtual folder, or namespace extension, and integrate it into the Shell's namespace. Creating custom explorer bars, tool bands, and desk bands. Using the Active Desktop object. Creating Control Panel applications. Using the Shell API with scripting languages and Visual Basic®.
Shell Programming Shell API functions and data constructs References There are many functions, enum constants, and structures to support Shell Programming. Our text only uses SHBrowserForFolder(), SHFileOperation(), SHGetPathFromIDList(), SHFILEOPSTRUCT struct, and BROWSEINFO struct. References Shell Programmers Guide in MSDN
Shell Programming int SHFileOperation(LPSHFILEOPSTRUCT lpFileOp); Copies, moves, renames, or deletes a file system object. Parameters lpFileOp [in] Address of an SHFILEOPSTRUCT structure that contains information this function needs to carry out the specified operation. Return Values Returns zero if successful, or nonzero otherwise. Remarks File deletion is recursive unless you set the FOF_NORECURSION flag in lpFileOp.
Shell Programming SHFILEOPSTRUCT typedef struct _SHFILEOPSTRUCT { HWND hwnd; UINT wFunc; LPCTSTR pFrom; LPCTSTR pTo; FILEOP_FLAGS fFlags; BOOL fAnyOperationsAborted; LPVOID hNameMappings; LPCSTR lpszProgressTitle; } SHFILEOPSTRUCT, *LPSHFILEOPSTRUCT; SHFILEOPSTRUCT Contains information that the SHFileOperation function uses to perform file operations. Members hwnd Window handle to the dialog box to display information about the status of the file operation. wFunc Value that indicates which operation to perform. One of {FO_COPY, FO_DELETE, FO_MOVE, FO_RENAME} pFrom Address of a buffer to specify one or more source file names. pTo Address of a buffer to contain the name of the destination file or directory.
Shell Programming LPITEMIDLIST SHBrowseForFolder( LPBROWSEINFO lpbi ); Displays a dialog box that enables the user to select a Shell folder. Parameters lpbi [in] Pointer to a BROWSEINFO structure that contains information used to display the dialog box. Return Values Returns a pointer to an ITEMIDLIST structure (PIDL) that specifies the location of the selected folder relative to the root of the namespace. If the user chooses the Cancel button in the dialog box, the return value is NULL.
Shell Programming BROWSEINFO Members typedef struct _browseinfo { HWND hwndOwner; LPCITEMIDLIST pidlRoot; LPTSTR pszDisplayName; LPCTSTR lpszTitle; UINT ulFlags; BFFCALLBACK lpfn; LPARAM lParam; int iImage; } BROWSEINFO, *LPBROWSEINFO; BROWSEINFO Members hwndOwner Handle to the owner window for the dialog box. pidlRoot Pointer to an ITEMIDLIST structure (PIDL) specifying the location of the root folder from which to start browsing. Only the specified folder and any subfolders that are beneath it in the namespace hierarchy will appear in the dialog box. This member can be NULL; in that case, the namespace root (the desktop folder) is used. pszDisplayName Address of a buffer to receive the display name of the folder selected by the user. The size of this buffer is assumed to be MAX_PATH bytes. ulFlags Flags specifying the options for the dialog box.
파일 복사 프로그램 예제 프로그램 개요 다이얼로그 기반 애플리케이션 다이얼로그 기반 애플리케이션 CFileDialog를 이용하여 한 개 이상의 파일을 선택한 다음 선택된 디렉토리에 복사 디렉토리의 선택에는 SHBrowserForFolder 함수를 사용 파일 복사에는 SHFileOperation 함수를 사용하며, 별도의 스레드로 실행되도록 함 실행 파일(http://cs.sookmyung.ac.kr/class/06395/Lab/FileCopy.exe) Button (OnFrom()) List (m_ctrlListBox) Edit (m_strDest) Button (OnTo()) Button (OnStartCopy())
파일 복사 프로그램 예제 From… 버튼의 처리 함수 조사하기 CFileDialog() 파라미터의 종류와 의미 void CFileCopyDlg::OnFrom() { m_ctrlListBox.ResetContent(); char szFilter[] = "All Files(*.*)|*.*||"; CFileDialog dlg(TRUE, NULL, NULL,OFN_ALLOWMULTISELECT, szFilter); if (IDOK == dlg.DoModal()) for (POSITION pos=dlg.GetStartPosition(); pos!=NULL; ) m_ctrlListBox.AddString(dlg.GetNextPathName(pos)); }
파일 복사 프로그램 예제 To… 버튼의 처리 함수 void CFileCopyDlg::OnTo() { ITEMIDLIST *pidlBrowse; char pszPathname[MAX_PATH]; BROWSEINFO BrInfo; BrInfo.hwndOwner = GetSafeHwnd(); BrInfo.pidlRoot = NULL; memset( &BrInfo, 0, sizeof(BrInfo) ); BrInfo.pszDisplayName = pszPathname; BrInfo.lpszTitle = "복사할 디렉터리를 선택하세요"; BrInfo.ulFlags = BIF_RETURNONLYFSDIRS; pidlBrowse = ::SHBrowseForFolder(&BrInfo); // 대화상자를 띄우기 if( pidlBrowse != NULL) { // 패스를 얻어옴 SHGetPathFromIDList(pidlBrowse, pszPathname); m_strDest = pszPathname; UpdateData(FALSE); }
파일 복사 프로그램 예제 복사시작 버튼의 처리 함수 (스레드를 이용한 파일 복사) void CFileCopyDlg::OnStartCopy() { UpdateData(); SHFILEOPSTRUCT *pFO = new SHFILEOPSTRUCT; pFO->hwnd = NULL; pFO->wFunc = FO_COPY; pFO->fFlags = FOF_NOCONFIRMMKDIR; pFO->fAnyOperationsAborted = TRUE; pFO->lpszProgressTitle = _T("파일 복사중"); CMemFile file; CArchive ar(&file, CArchive::store); CString str; int nCount = m_ctrlListBox.GetCount(); char null = 0;
for(int i=0 ; i<nCount ; i++) { m_ctrlListBox.GetText(i, str); ar.WriteString(str); ar.Write(&null, 1); } ar.Close(); pFO->pFrom = (char *)file.Detach(); // 복사될 디렉토리명 (Destination) pFO->pTo = new char [m_strDest.GetLength()+1]; strcpy((LPSTR)pFO->pTo, (LPSTR)(LPCTSTR)m_strDest); ((LPSTR)pFO->pTo)[m_strDest.GetLength()]=0; AfxBeginThread(ThreadFunc, pFO);
파일 복사 프로그램 예제 복사시작 버튼의 처리 함수 (스레드를 이용한 파일 복사) (계속) 다음 함수 프로토타입 선언을 FileCopyDlg.h에 포함시킴 UINT ThreadFunc(LPVOID pParam) { SHFILEOPSTRUCT *pFO = (SHFILEOPSTRUCT *)pParam; ::SHFileOperation(pFO); delete [] (char *)pFO->pFrom; delete [] (char *)pFO->pTo; delete pFO; return 0; } UINT ThreadFunc(LPVOID pParam);
보고서 작성 요령 Dialog 클래스 부분의 프로그램 소스 프로그램 설명 (AppWizard에 의해 생성된 부분은 제외) 주요 함수 설명 주요 구조체 설명 프로그램의 각 명령문 해설