2007년 4월 3일 화요일

MFC FTP 사용 예제

먼저 다음을 선언

CInternetSession m_Session;
CFtpConnection *m_pConnection;
CFtpFileFind *m_pFileFind;


생성자 함수에서 포인터 초기화

m_pFileFind = NULL;
m_pConnection = NULL;


Stdafx.h 제일 아래에 다음 추가(direct.h는 디렉토리 관련 함수 호출을 위한 것임.)

#include "afxsock.h"
#include "afxinet.h"
#include "direct.h"


InitInstance 함수에서 AfxSocketInit() 호출

CWinApp::InitInstance();
  if (!AfxSocketInit()) {
  AfxMessageBox(_T("소켓 초기화 실패"));
  return FALSE;
}
...


이제 연결을 합니다.

BeginWaitCursor();

// 이미 연결된 것이 있으면 해제
if (m_pConnection != NULL) {
  m_pConnection->Close();
  delete m_pConnection;
  m_pConnection = NULL;
}
...


// 연결하고자 하는 FPT 서버의 IP 주소와 사용자명, 암호 입력
m_pConnection = m_Session.GetFtpConnection(sIpAddress, sUsername, sPassword);

// 연결이 안됐을 경우
if (!m_pConnection) {
  AfxMessageBox(_T("Error"));
  m_pConnection = NULL;
  return;
}

// 현재 디렉토리를 얻음
m_pConnection->GetCurrentDirectory(sRomoteDir);

// 이미 사용하고 있으면 해제
if (m_pFileFind) delete m_pFileFind;

// FTP 서버의 파일명을 읽어 옴
m_pFileFind = new CFtpFileFind(m_pConnection);

...

EndWaitCursor();


m_pFileFind는 다음과 같이 사용함.
먼저 디렉토리를 넘겨 주고 FileFind 한 번 호출, 그런 다음 FindNextFile 함수가 FALSE를 넘겨 줄 때까지 GetFileName 함수를 반복해서 호출한다.

m_pFileFind->FindFile(sRemoteDir);
...
while (bContinue) {
  bContinue = m_pFileFind->FindNextFile();
  sFileName = m_pFileFind->GetFileName();
...
}

m_pFileFind->IsDirectory()를 호출하면 디렉토리인지 폴더인지 확인 가능하다.

기타

m_pConnection->SetCurrentDirectory(sRemoteDir);
m_pConnection->GetCurrentDirectory(sRemoteDir);


등이 사용된다.
파일의 업로드/다운로드는

m_pConnection->PutFile(sLocalFile, sRemotePath);
m_pConnection->GetFile(sPath, sRemotePath);

로 된다.

댓글 1개:

Unknown :

sRemoteDir은 어디에 선언되있나요..
sRemote로 되어있는 변수의 선언이 어디되있는지 알고싶습니다.