언젠가 유용하게 쓰겠지...
The temporary file handling class for Win32.
class TempFileUtility {
public:
TempFileUtility()
{
ResetData();
}
virtual ~TempFileUtility()
{
CloseTempFile();
}
int OpenTempFile( BOOL bCloseAfterCreated )
{
if( m_bInitialized==TRUE )
return ERROR_ALREADY_INITIALIZED;
const int cMaxTempDir = MAX_PATH-14;
TCHAR szTempDir[cMaxTempDir];
int nResult = 0;
nResult = GetTempPath( cMaxTempDir, szTempDir );
if( nResult==0 || nResult>cMaxTempDir )
{
nResult = SHGetSpecialFolderPath( NULL, szTempDir, CSIDL_MYDOCUMENTS, TRUE );
if( nResult==FALSE )
return ERROR_CANNOT_MAKE;
}
nResult = GetTempFileName( szTempDir, _T(""), 0, m_szTempFile );
if( nResult==FALSE )
{
ResetData();
return ERROR_CANNOT_MAKE;
}
if( bCloseAfterCreated==TRUE )
{
m_hFile = CreateFile( m_szTempFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL );
if( m_hFile==INVALID_HANDLE_VALUE )
{
DeleteFile( m_szTempFile );
ResetData();
return ERROR_OPEN_FAILED;
}
}
m_bInitialized = TRUE;
return ERROR_SUCCESS;
}
int WriteTempFile(
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite
)
{
if( m_bInitialized==FALSE )
return ERROR_NOT_READY;
if( m_hFile==INVALID_HANDLE_VALUE )
return ERROR_NOT_READY;
BOOL bSuccess;
DWORD dwWritten = 0;
bSuccess = WriteFile( m_hFile, lpBuffer, nNumberOfBytesToWrite, &dwWritten, NULL );
if( bSuccess==FALSE )
return GetLastError();
return ERROR_SUCCESS;
}
int CloseTempFile()
{
if( m_bInitialized==FALSE )
return ERROR_NOT_READY;
if( m_hFile!=INVALID_HANDLE_VALUE )
CloseHandle( m_hFile );
DeleteFile( m_szTempFile );
ResetData();
return ERROR_SUCCESS;
}
LPCTSTR GetTempFilePath() {return m_szTempFile;}
HANDLE GetTempFileHandle() {return m_hFile;}
protected:
BOOL m_bInitialized;
TCHAR m_szTempFile[MAX_PATH];
HANDLE m_hFile;
void ResetData()
{
m_bInitialized = FALSE;
memset( m_szTempFile, 0, MAX_PATH );
m_hFile = INVALID_HANDLE_VALUE;
}
};