|
/***************************************************************************************************************
*
* Filename: ScopedHandleExample.cpp
*
* Author: Miki Rozloznik
*
* Date: 2010/07/21
*
* Implementation Description:
*//** \file
* The example of usage of scoped handle for Windows objects.
*//*
*
**************************************************************************************************************/
#include <windows.h>
#include <tchar.h>
#include "ScopedHandle.h"
int _tmain(int argc, _TCHAR* argv[])
{
// open some file
const _TCHAR* pFileName = _T("c:\\windows\\Coffee Bean.bmp");
FileScopedHandle File( CreateFile(pFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL) );
if (!File)
{
OutputDebugString(_T("File open failure!\n"));
return -1;
}
// get file size in bytes
const DWORD FileSize = GetFileSize(File.Get(), NULL);
if (FileSize == INVALID_FILE_SIZE)
{
OutputDebugString(_T("File has unknown size!\n"));
return -1;
}
// print result
const unsigned int MaxMessageLen = 64;
_TCHAR Message[MaxMessageLen + 1];
Message[MaxMessageLen] = 0;
_sntprintf(Message, MaxMessageLen, _T("The file %s contains %ld bytes.\n"), pFileName, FileSize);
OutputDebugString(Message);
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.