blejzz
blejzz

Reputation: 3349

Geting the file size of a system application on windows in C++

I am trying to get the file size of a system application on windows. To test this i have created a test application that tries to get the file size of smss.exe in C:\Windows\System32\smss.exe but it fails with error: ERROR_FILE_NOT_FOUND. The file does actually exist (i have checked). I've also tried different methods for getting the file size, with: FindFirstFile, CreateFile and GetFileSizeEx. But all return the same error. I would also like to read the file contents.

What am i doing wrong?

The code:

 // Test.cpp : Defines the entry point for the console application.
 //

 #include "stdafx.h"

 #include <windows.h>
 #include <stdio.h>
 #include <tchar.h>
 #include <iostream>

 __int64 getFileSize(LPWSTR filePath)
{
     WIN32_FILE_ATTRIBUTE_DATA fad;
      if (!GetFileAttributesEx(filePath, GetFileExInfoStandard, &fad))
      {
          _tprintf(TEXT("\n CAnt get file size for file %s error %d"), filePath, GetLastError());
          return 0; 
      }
      LARGE_INTEGER size;
      size.HighPart = fad.nFileSizeHigh;
      size.LowPart = fad.nFileSizeLow;
     return size.QuadPart;
}

int _tmain(int argc, _TCHAR* argv[])
{
     _tprintf(TEXT("File size %d "), getFileSize(L"C:\\Windows\\System32\\smss.exe"));
}

Upvotes: 0

Views: 327

Answers (2)

Qaz
Qaz

Reputation: 61910

As your application is 32-bit, the system redirects your path to go to SysWOW64 instead, where there is no smss.exe. While you have discovered that Wow64DisableWow64FsRedirection disables this redirection, also consider that having a 64-bit program would also do the trick.

Upvotes: 2

Zsolt
Zsolt

Reputation: 582

Getting the size of a file is already answered here (can't yet add a comment to your question, so I need to write it as an answer): How can I get a file's size in C++?

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::in | std::ifstream::binary);
    in.seekg(0, std::ifstream::end);
    return in.tellg(); 
}

Upvotes: -1

Related Questions