Reputation: 29387
I've tried to use this approach:
#include <windows.h>
#include <iostream>
int main() {
LARGE_INTEGER size;
HANDLE hFile = CreateFile("c:\\pagefile.sys", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) return(1);
GetFileSizeEx(hFile, &size);
CloseHandle(hFile);
std::cout << size.QuadPart << std::endl;
}
But as you see I point to "pagefile.sys" which is locked, and program encounters INVALID_HANDLE_VALUE. But non system apps can see sizes of locked files. For example total commander gives me about 1GB and it must get this value from somewhere (not to mention simple right-clicking on that file, but that is system process so file is not locked to it). So, are there any winapi calls for that case ?
I've updated code to included suggested corrections, but it still doesn't work:
#include <windows.h>
#include <iostream>
int main() {
LARGE_INTEGER size;
HANDLE hFile = CreateFile("c:\\pagefile.sys", 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
std::cout << "GetLastError: " << GetLastError() << std::endl;
//says: 5 (0x5) ERROR_ACCESS_DENIED
if (hFile == INVALID_HANDLE_VALUE) return(1);
GetFileSizeEx(hFile, &size);
CloseHandle(hFile);
std::cout << size.QuadPart << std::endl;
}
Upvotes: 0
Views: 947
Reputation: 941645
You can get info out of the directory entry for a file, there is no mechanism to lock that. Which requires FindFirstFile/FindNextFile to iterate the directory. The returned WIN32_FIND_DATA.nFileSizeHigh/Low gives you the info you want.
The actual number you get is not reliable, it is merely a snapshot and it is likely to be stale. Especially so for the paging file, Windows can rapidly change its size. Getting a reliable size requires locking the file so nobody can change it, like you did. Which will not work for the paging file, the operating system keeps a hard lock on it so nobody can mess with the file content or read security sensitive data from the file.
Upvotes: 6
Reputation: 66371
According to MSDN, you should set the dwDesiredAccess
parameter to 0 (zero) if you only want information without opening the file.
Upvotes: 0