BOOL CHttpRequestSender::CalcAttachmentPartSize(CString sName, LONGLONG& lSize) { lSize = 0; CString sPartHeader; BOOL bFormat = FormatAttachmentPartHeader(sName, sPartHeader); if(!bFormat) return FALSE; lSize += sPartHeader.GetLength(); std::map<CString, CHttpRequestFile>::iterator it = m_Request.m_aIncludedFiles.find(sName); if(it==m_Request.m_aIncludedFiles.end()) return FALSE; CString sFileName = it->second.m_sSrcFileName.GetBuffer(0); HANDLE hFile = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if(hFile==INVALID_HANDLE_VALUE) { return FALSE; } LARGE_INTEGER lFileSize; BOOL bGetSize = GetFileSizeEx(hFile, &lFileSize); if(!bGetSize) { CloseHandle(hFile); return FALSE; } lSize += lFileSize.QuadPart; CloseHandle(hFile); CString sPartFooter; bFormat = FormatAttachmentPartFooter(sName, sPartFooter); if(!bFormat) return FALSE; lSize += sPartFooter.GetLength(); return TRUE; }
LONG64 CCrashInfoReader::GetUncompressedReportSize(CErrorReportInfo& eri) { // Calculate summary size of all files included into crash report LONG64 lTotalSize = 0; std::map<CString, ERIFileItem>::iterator it; HANDLE hFile = INVALID_HANDLE_VALUE; CString sMsg; BOOL bGetSize = FALSE; LARGE_INTEGER lFileSize; // Walk through all files in the crash report for(it=eri.m_FileItems.begin(); it!=eri.m_FileItems.end(); it++) { // Get file name CString sFileName = it->second.m_sSrcFile.GetBuffer(0); // Check file exists hFile = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if(hFile==INVALID_HANDLE_VALUE) continue; // File does not exist // Get file size bGetSize = GetFileSizeEx(hFile, &lFileSize); if(!bGetSize) { CloseHandle(hFile); continue; } // Add to the sum lTotalSize += lFileSize.QuadPart; // Clean up CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE; } // Return summary size return lTotalSize; }
static int DOKAN_CALLBACK MirrorSetAllocationSize( LPCWSTR FileName, LONGLONG AllocSize, PDOKAN_FILE_INFO DokanFileInfo) { WCHAR filePath[MAX_PATH]; HANDLE handle; LARGE_INTEGER fileSize; GetFilePath(filePath, MAX_PATH, FileName); DbgPrint(L"SetAllocationSize %s, %I64d\n", filePath, AllocSize); handle = (HANDLE)DokanFileInfo->Context; if (!handle || handle == INVALID_HANDLE_VALUE) { DbgPrint(L"\tinvalid handle\n\n"); return -1; } if (GetFileSizeEx(handle, &fileSize)) { if (AllocSize < fileSize.QuadPart) { fileSize.QuadPart = AllocSize; if (!SetFilePointerEx(handle, fileSize, NULL, FILE_BEGIN)) { DbgPrint(L"\tSetAllocationSize: SetFilePointer eror: %d, " L"offset = %I64d\n\n", GetLastError(), AllocSize); return GetLastError() * -1; } if (!SetEndOfFile(handle)) { DWORD error = GetLastError(); DbgPrint(L"\terror code = %d\n\n", error); return error * -1; } } } else { DWORD error = GetLastError(); DbgPrint(L"\terror code = %d\n\n", error); return error * -1; } return 0; }
DWORD FindProcessId (DWORD jobNumber) /* Obtain the process ID of the specified job number. */ { HANDLE hJobData; JM_JOB jobRecord; DWORD nXfer, fileSizeLow; TCHAR jobMgtFileName[MAX_PATH+1]; OVERLAPPED regionStart; LARGE_INTEGER fileSize; if ( !GetJobMgtFileName (jobMgtFileName) ) return 0; hJobData = CreateFile (jobMgtFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hJobData == INVALID_HANDLE_VALUE) return 0; /* Position to the correct record, but not past the end of file */ /* As a variation, use GetFileSize to demonstrate its operation. */ if (!GetFileSizeEx (hJobData, &fileSize) || (fileSize.HighPart != 0 || SJM_JOB * (jobNumber - 1) > fileSize.LowPart || fileSize.LowPart > SJM_JOB * MAX_JOBS_ALLOWED)) return 0; fileSizeLow = fileSize.LowPart; /* SetFilePoiner is more convenient here than SetFilePointerEx since the the file is known to be "short" ( < 4 GB). */ SetFilePointer (hJobData, SJM_JOB * (jobNumber - 1), NULL, FILE_BEGIN); /* Get a shared lock on the record. */ regionStart.Offset = SJM_JOB * (jobNumber - 1); regionStart.OffsetHigh = 0; /* Assume a "short" file. */ regionStart.hEvent = (HANDLE)0; LockFileEx (hJobData, 0, 0, SJM_JOB, 0, ®ionStart); if (!ReadFile (hJobData, &jobRecord, SJM_JOB, &nXfer, NULL)) ReportError (_T ("JobData file error"), 0, TRUE); UnlockFileEx (hJobData, 0, SJM_JOB, 0, ®ionStart); CloseHandle (hJobData); return jobRecord.ProcessId; }
static int _zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx) { FILETIME mtimeft; time_t mtime; LARGE_INTEGER size; int regularp; if (!GetFileTime(h, NULL, NULL, &mtimeft)) { zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError())); return -1; } if (_zip_filetime_to_time_t(mtimeft, &mtime) < 0) { zip_error_set(&ctx->error, ZIP_ER_READ, ERANGE); return -1; } regularp = 0; if (GetFileType(h) == FILE_TYPE_DISK) { regularp = 1; } if (!GetFileSizeEx(h, &size)) { zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError())); return -1; } zip_stat_init(st); st->mtime = mtime; st->valid |= ZIP_STAT_MTIME; if (ctx->end != 0) { st->size = ctx->end - ctx->start; st->valid |= ZIP_STAT_SIZE; } else if (regularp) { st->size = (zip_uint64_t)size.QuadPart; st->valid |= ZIP_STAT_SIZE; } return 0; }
int uGetFileSize(UFile fd, __int64 * file_size, sys_call_error_fun fun) { #ifdef _WIN32 LARGE_INTEGER size; BOOL res = GetFileSizeEx(fd, &size); if (res == 0) sys_call_error("GetFileSizeEx"); else *file_size = size.QuadPart; return res; #else struct stat64 buf; if (fstat64(fd, &buf) == -1) { sys_call_error("fstat64"); return 0; } *file_size = buf.st_size; return 1; #endif }
::FLAC__StreamDecoderLengthStatus OurDecoder::length_callback(FLAC__uint64 *stream_length) { #ifdef PRWIN_ENV LARGE_INTEGER lpos; BOOL result = GetFileSizeEx(_fp, &lpos); *stream_length = lpos.QuadPart; return (result ? FLAC__STREAM_DECODER_LENGTH_STATUS_OK : FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR); #else SInt64 fork_size = 0; OSErr result = FSGetForkSize(CAST_REFNUM(_fp), &fork_size); *stream_length = fork_size; return (result == noErr ? FLAC__STREAM_DECODER_LENGTH_STATUS_OK : FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR); #endif }
LONGLONG getFileSize(HANDLE hFile) { LARGE_INTEGER lpFileSize = {0}; if ((hFile != INVALID_HANDLE_VALUE) && (hFile != 0)) { if (!GetFileSizeEx(hFile, &lpFileSize)) { MessageBox(0, TEXT("GetFileSizeEx failed"), TEXT("Error"), MB_ICONERROR); return 0; } else { return lpFileSize.QuadPart; } } else { return 0; } }
size_t File::GetFileSize() { if( !IsValidHandle() ) return 0; size_t Size = 0; #if _WIN32 || _WIN64 LARGE_INTEGER LInt; Memory::Memzero( &LInt, sizeof(LInt) ); GetFileSizeEx( m_file_handle, &LInt ); Size = (size_t) LInt.QuadPart; //DWORD dwSize = GetFileSize( m_hFile, NULL ); #else size_t Pos = ftell(m_file_handle); fseek( m_file_handle, 0, SEEK_END ); Size = ftell(m_file_handle); fseek( m_file_handle, Pos, SEEK_SET ); #endif return Size; }
BOOL CFileMemoryMapping::Init(LPCTSTR szFileName) { if(m_hFile!=INVALID_HANDLE_VALUE) { // If a file mapping already created, destroy it Destroy(); } // Open file handle m_hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if(m_hFile == INVALID_HANDLE_VALUE) return FALSE; // Create file mapping m_hFileMapping = CreateFileMapping(m_hFile, 0, PAGE_READONLY, 0, 0, 0); LARGE_INTEGER size; GetFileSizeEx(m_hFile, &size); m_uFileLength = size.QuadPart; return TRUE; }
//Return the contents of a text file void LoadFile(char *path, DataFile *dataFile) { HANDLE fileHandle = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if (fileHandle != INVALID_HANDLE_VALUE) { LARGE_INTEGER fileSize; if (GetFileSizeEx(fileHandle, &fileSize)) { unsigned int fileSize_32bit = (unsigned int)fileSize.QuadPart; dataFile->Data = VirtualAlloc(0, fileSize_32bit, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (dataFile->Data) { DWORD bytesRead; if (ReadFile(fileHandle, dataFile->Data, fileSize_32bit, &bytesRead, NULL) && fileSize_32bit == bytesRead) { dataFile->Length = fileSize_32bit; } else { //Failed to read file } } else { //Failed to allocate memory } } else { //Failed to get file size } } else { //Failed to create file handle } }
/* return the size of a given file in '*psize'. returns 0 on * success, -1 on failure (error code in errno) */ APosixStatus path_get_size( const char* path, uint64_t *psize ) { #ifdef _WIN32 /* avoid _stat64 which is only defined in MSVCRT.DLL, not CRTDLL.DLL */ /* do not use OpenFile() because it has strange search behaviour that could */ /* result in getting the size of a different file */ LARGE_INTEGER size; HANDLE file = CreateFile( /* lpFilename */ path, /* dwDesiredAccess */ GENERIC_READ, /* dwSharedMode */ FILE_SHARE_READ|FILE_SHARE_WRITE, /* lpSecurityAttributes */ NULL, /* dwCreationDisposition */ OPEN_EXISTING, /* dwFlagsAndAttributes */ 0, /* hTemplateFile */ NULL ); if (file == INVALID_HANDLE_VALUE) { /* ok, just to play fair */ errno = ENOENT; return -1; } if (!GetFileSizeEx(file, &size)) { /* maybe we tried to get the size of a pipe or something like that ? */ *psize = 0; } else { *psize = (uint64_t) size.QuadPart; } CloseHandle(file); return 0; #else int ret; struct stat st; CHECKED(ret, stat(path, &st)); if (ret == 0) { *psize = (uint64_t) st.st_size; } return ret; #endif }
LWS_VISIBLE lws_fop_fd_t _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename, const char *vpath, lws_fop_flags_t *flags) { HANDLE ret; WCHAR buf[MAX_PATH]; lws_fop_fd_t fop_fd; LARGE_INTEGER llFileSize = {0}; MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf)); if (((*flags) & 7) == _O_RDONLY) { ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } else { ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } if (ret == LWS_INVALID_FILE) goto bail; fop_fd = malloc(sizeof(*fop_fd)); if (!fop_fd) goto bail; fop_fd->fops = fops; fop_fd->fd = ret; fop_fd->filesystem_priv = NULL; /* we don't use it */ fop_fd->flags = *flags; fop_fd->len = GetFileSize(ret, NULL); if(GetFileSizeEx(ret, &llFileSize)) fop_fd->len = llFileSize.QuadPart; fop_fd->pos = 0; return fop_fd; bail: return NULL; }
bool LWOFile::LoadFromFile(const char *strFile) { // try to open file HANDLE hFile=CreateFileA(strFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile==INVALID_HANDLE_VALUE) { AddError("Could not open file: " + std::string(strFile)); return false; } // get file size LARGE_INTEGER LI_Size; if(!GetFileSizeEx(hFile,&LI_Size)) { AddError("Could not get file size!"); return false; } unsigned int iFileSize=(unsigned int)LI_Size.QuadPart; // allocate memory to hold the file char *pData=new char[iFileSize]; // read file to memory DWORD iBytesRead=0; if(!ReadFile(hFile,pData,iFileSize,&iBytesRead,NULL) || iBytesRead!=iFileSize) { AddError("Reading file failed!"); delete[] pData; CloseHandle(hFile); return false; } // load if(!LoadFromMemory(pData, iFileSize)) return false; delete[] pData; CloseHandle(hFile); return true; }
BOOL kull_m_file_readData(PCWCHAR fileName, PBYTE * data, PDWORD lenght) // for little files ! { BOOL reussite = FALSE; DWORD dwBytesReaded; LARGE_INTEGER filesize; HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(hFile && hFile != INVALID_HANDLE_VALUE) { if(GetFileSizeEx(hFile, &filesize) && !filesize.HighPart) { *lenght = filesize.LowPart; if(*data = (PBYTE) LocalAlloc(LPTR, *lenght)) { if(!(reussite = ReadFile(hFile, *data, *lenght, &dwBytesReaded, NULL) && (*lenght == dwBytesReaded))) LocalFree(*data); } } CloseHandle(hFile); } return reussite; }
static HRESULT open_file(FileProtocol *This, const WCHAR *path, IInternetProtocolSink *protocol_sink) { LARGE_INTEGER size; HANDLE file; file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(file == INVALID_HANDLE_VALUE) return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError()); if(!GetFileSizeEx(file, &size)) { CloseHandle(file); return report_result(protocol_sink, INET_E_RESOURCE_NOT_FOUND, GetLastError()); } This->file = file; This->size = size.u.LowPart; IInternetProtocolSink_ReportProgress(protocol_sink, BINDSTATUS_CACHEFILENAMEAVAILABLE, path); return S_OK; }
uint64 MFFileNative_GetSize(const char *pFilename) { MFCALLSTACK; uint64 fileSize = 0; #if defined(MF_XBOX) || defined(MF_X360) pFilename = FixXBoxFilename(pFilename); #endif HANDLE hFile = CreateFile(MFString_UFT8AsWChar(pFilename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if(hFile != INVALID_HANDLE_VALUE) { LARGE_INTEGER size; GetFileSizeEx(hFile, &size); CloseHandle(hFile); fileSize = size.QuadPart; } return fileSize; }
u64 fs::file::size() const { g_tls_error = fse::ok; #ifdef _WIN32 LARGE_INTEGER size; if (!GetFileSizeEx((HANDLE)m_fd, &size)) { return -1; } return size.QuadPart; #else struct stat file_info; if (::fstat(m_fd, &file_info) < 0) { return -1; } return file_info.st_size; #endif }
UINT64 CHttpDownloader::isBreakPointFile(std::wstring wcsFile) { UINT64 uI64Num =0; wchar_t wcsNum[255]; UINT64 uI64Return =0 ; LARGE_INTEGER largeSize; largeSize.QuadPart = 0; DWORD dwRegType = 0, dwReturnSize = sizeof(wcsNum); HANDLE hFile = CreateFile(wcsFile.c_str(),MYWIRTEFILE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if( hFile != INVALID_HANDLE_VALUE) { GetFileSizeEx(hFile,&largeSize); uI64Num = largeSize.QuadPart; if( uI64Num > BREAKRETURNBACKBYTE*1024 ) uI64Return = uI64Num - BREAKRETURNBACKBYTE*1024; } CloseHandle(hFile); return uI64Return; }
VOID WriteDebugLog(LPSTR lpFile, UINT iLine, LPSTR lpFunc, LPWSTR lpMsg, ...) { LARGE_INTEGER FileSize, MoveTo, NewPos; WCHAR szMsg[MAX_STR_LEN * 3]; WCHAR szText[MAX_STR_LEN * 4], szTime[MAX_STR_LEN]; DWORD dwBytesWritten; va_list args; if (!hDebugLog || hDebugLog == INVALID_HANDLE_VALUE) return; MoveTo.QuadPart = 0; if (!SetFilePointerEx(hDebugLog, MoveTo, &NewPos, FILE_END)) return; if (!GetFileSizeEx(hDebugLog, &FileSize)) return; LockFile(hDebugLog, (DWORD_PTR)NewPos.QuadPart, 0, (DWORD_PTR)FileSize.QuadPart, 0); GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL, szTime, MAX_STR_LEN); va_start(args, lpMsg); StringCbVPrintf(szMsg, sizeof(szMsg), lpMsg, args); va_end(args); StringCbPrintf(szText, sizeof(szText), L"[%s] %S:%ld %S(): \"%s\"\r\n", szTime, lpFile, iLine, lpFunc, szMsg); WriteFile(hDebugLog, szText, wcslen(szText) * sizeof(WCHAR), &dwBytesWritten, NULL); UnlockFile(hDebugLog, (DWORD_PTR)NewPos.QuadPart, 0, (DWORD_PTR)FileSize.QuadPart, 0); }
static int64_t ALMIXER_CALL windows_file_size(ALmixer_RWops * context) { LARGE_INTEGER size; if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) { ALmixer_SetError("windows_file_size: invalid context/file not opened"); return -1; } if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) { #if 1 return WIN_SetError("windows_file_size"); #else /* There are a lot of dependencies on iconv/UTF16 stuff just for WIN_SetError to print the HRESULT. I'd rather avoid it */ ALmixer_SetError("windows_file_size failed calling GetFileSizeEx()"); return -1; #endif } return size.QuadPart; }
bool ReadFile(String filename, JavaScript::Object options, String& result) { String encoding = TEXT(""); if (options->HasKey("encoding")) options->GetString("encoding"); // text file if (encoding == TEXT("") || encoding == TEXT("utf-8") || encoding == TEXT("text/plain;utf-8")) { HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { DWORD numBytesRead = 0; LARGE_INTEGER fileSize; GetFileSizeEx(hFile, &fileSize); // we don't want to read too large files _ASSERT(fileSize.HighPart == 0); // allocate buffer and read file BYTE* data = new BYTE[fileSize.LowPart]; ::ReadFile(hFile, (LPVOID) data, (DWORD) fileSize.LowPart, &numBytesRead, NULL); // convert the buffer int wcLen = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) data, numBytesRead, NULL, 0); TCHAR* buf = new TCHAR[wcLen + 1]; MultiByteToWideChar(CP_UTF8, 0, (LPCCH) data, numBytesRead, buf, wcLen); buf[wcLen] = 0; result = String(buf, buf + wcLen); delete[] buf; delete[] data; } CloseHandle(hFile); return true; } return false; }
uint64 TFile::GetSize(const TStr& FNm) { // open HANDLE hFile = CreateFile( FNm.CStr(), // file to open GENERIC_READ, // open for reading FILE_SHARE_READ | FILE_SHARE_WRITE, // share for reading NULL, // default security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template // check if we could open it if (hFile == INVALID_HANDLE_VALUE) { TExcept::Throw("Can not open file " + FNm + "!"); } // read file times LARGE_INTEGER lpFileSizeHigh; if (!GetFileSizeEx(hFile, &lpFileSizeHigh)) { TExcept::Throw("Can not read size of file " + FNm + "!"); } // close file CloseHandle(hFile); // convert to uint64 return uint64(lpFileSizeHigh.QuadPart); }
Win32FileContents ReadEntireFile(const char * Filename){ HANDLE File = CreateFileA(Filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); LARGE_INTEGER Size; GetFileSizeEx(File, &Size); Win32FileContents FileContents = {}; FileContents.Size = Size.QuadPart; FileContents.Data = (unsigned char *)VirtualAlloc(0, Size.LowPart, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); DWORD BytesRead; ReadFile(File, FileContents.Data, Size.LowPart, &BytesRead, 0); if (BytesRead != Size.LowPart) { DisplayMessage(GetLastError()); //TODO: Logging, errors etc } return FileContents; };
/** 获得只读文件的句柄。注意只适合于只读文件! * 快速访问文件,用FileMapping方式对大文件的访问 * 将会最为快捷。 * 顺序访问数据 */ FILEMAPHANDLE FileMapOpen(const TCHAR *file_name) { FILEMAPHANDLE handle; handle = (FILEMAPHANDLE) malloc(sizeof(FILEMAPDATA)); if (!handle) return 0; //打开文件 handle->h_file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (handle->h_file == INVALID_HANDLE_VALUE) { free(handle); return 0; } handle->length = 0; GetFileSizeEx(handle->h_file, (PLARGE_INTEGER)&handle->length); //创建映射 handle->h_map = CreateFileMapping(handle->h_file, 0, PAGE_READONLY, 0, 0, 0); if (!handle->h_map) { CloseHandle(handle->h_file); free(handle); return 0; } SYSTEM_INFO sys_info; GetSystemInfo(&sys_info); handle->granularity = (int)sys_info.dwAllocationGranularity; handle->offset = 0; handle->view = 0; return handle; }
void MetroWindow::DiscoverInstallerImage() { wchar_t szImageFile[MAX_UNC_PATH] = { 0 }; if (DiscoverInstallerIMAGE(m_hWnd, szImageFile) == S_OK) { ::SetWindowText(GetDlgItem(IDC_EDIT_IMAGE), szImageFile); ProcessInfo = L"Manager Task Rate:"; JobStatusRate = L"Task not start"; MTNotices = L"Notices> Enviroment Inspection:\n"+envinfo; m_proge.SetPos(0); HANDLE hFile; LARGE_INTEGER FileSize; hFile = CreateFileW(szImageFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { GetFileSizeEx(hFile, &FileSize); swprintf_s(SizeStr, L" %lld Bytes ||%4.1f KB ||%4.2f MB ||%4.2f GB", FileSize.QuadPart, (float)FileSize.QuadPart / 1024, (float)FileSize.QuadPart / (1024 * 1024), (float)FileSize.QuadPart / (1024 * 1024 * 1024)); SendMessage(WM_PAINT, 0, 0); } CloseHandle(hFile); } }
HRESULT MkvFile::Open(const wchar_t* strFileName) { if (strFileName == 0) return E_INVALIDARG; if (m_hFile != INVALID_HANDLE_VALUE) return E_UNEXPECTED; m_hFile = CreateFile( strFileName, GENERIC_READ, FILE_SHARE_READ, 0, //security attributes OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0); if (m_hFile == INVALID_HANDLE_VALUE) { const DWORD e = GetLastError(); return HRESULT_FROM_WIN32(e); } LARGE_INTEGER size; const BOOL b = GetFileSizeEx(m_hFile, &size); if (!b) { const DWORD e = GetLastError(); Close(); return HRESULT_FROM_WIN32(e); } m_length = size.QuadPart; assert(m_length >= 0); return S_OK; }
void* ReadWholeFile( char* filename, int64* bytesRead ) { //Open HANDLE fileHandle; //TODO: look into other options, such as: Overlapped, No_Bufffering, and Random_Access fileHandle = CreateFile( filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); if( fileHandle == INVALID_HANDLE_VALUE ) { assert( false ); } //Determine amount of data LARGE_INTEGER fileSize; GetFileSizeEx( fileHandle, &fileSize ); if( fileSize.QuadPart == 0 ) { assert( false ); } assert( fileSize.QuadPart <= 0xFFFFFFFF ); //Reserve Space void* data = 0; data = malloc( fileSize.QuadPart ); memset( data, 0, fileSize.QuadPart ); assert( data != 0 ); //Read data DWORD dataRead = 0; //TODO: adjust for overlapped or async stuff? BOOL readSuccess = ReadFile( fileHandle, data, fileSize.QuadPart, &dataRead, 0 ); if( !readSuccess || dataRead != fileSize.QuadPart ) { } //Close BOOL closeReturnValue = CloseHandle( fileHandle ); assert( closeReturnValue ); *bytesRead = fileSize.QuadPart; return data; }
static int _file_load(const char *file, void **data, u32 * len) { HANDLE h, fmo; LARGE_INTEGER size; void *buf; h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (h == INVALID_HANDLE_VALUE) { fprintf(stderr, "ERROR: failed to open file %s\n", file); return -1; } // figure out how big the file is. if (GetFileSizeEx(h, &size) == 0) { fprintf(stderr, "ERROR: failed to get size of file %s\n", file); return -1; } fmo = CreateFileMapping(h, NULL, PAGE_READONLY, size.u.HighPart, size.u.LowPart, NULL); if (fmo == NULL) { fprintf(stderr, "ERROR: failed to create file mapping for file %s: %d\n", file, GetLastError()); return -1; } buf = MapViewOfFile(fmo, FILE_MAP_READ, 0, 0, size.u.LowPart); if (buf == NULL) { fprintf(stderr, "ERROR: failed to create a file view for file %s: %d\n", file, GetLastError()); return -1; } printf("file %s (size=%ld) mapped at %p\n", file, size.u.LowPart, buf); *data = buf; *len = size.u.LowPart; return 0; }
size_t get_filesize(const char *filepath) { #ifdef WINDOWS HANDLE hFile = CreateFile( filepath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hFile == INVALID_HANDLE_VALUE) return 0; LARGE_INTEGER size; if (!GetFileSizeEx(hFile, &size)) { CloseHandle(hFile); return 0; } CloseHandle(hFile); return (size_t)size.QuadPart; #else size_t size = 0; struct stat fileStat; if ((stat(filepath, &fileStat)) >= 0) { size = (size_t)fileStat.st_size; } return size; #endif }