c++ - 在C++中获取Windows系统应用程序的文件大小

标签 c++ windows

我正在尝试获取 Windows 系统应用程序的文件大小。为了对此进行测试,我创建了一个测试应用程序,它试图在 C:\Windows\System32\smss.exe 中获取 smss.exe 的文件大小,但它失败并出现错误:ERROR_FILE_NOT_FOUND。该文件确实存在(我已经检查过)。我还尝试了不同的方法来获取文件大小,包括:FindFirstFile、CreateFile 和 GetFileSizeEx。但都返回相同的错误。我还想阅读文件内容。

我做错了什么?

代码:

 // 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"));
}

最佳答案

由于您的应用程序是 32 位的,系统 redirects您转到 SysWOW64 的路径,那里没有 smss.exe。当你发现 Wow64DisableWow64FsRedirection禁用此重定向,还要考虑使用 64 位程序也可以解决问题。

关于c++ - 在C++中获取Windows系统应用程序的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24344454/

相关文章:

c++ - 使用 cmake 交叉编译 libwebsockets

c++ - 避免采用 int 参数的模板类的 C++ 爆炸式实例化

C++ 错误 : 'No suitable conversion from [class name] to "int"'

c++ - 静态函数和类变量?

我研究过的 C++ SDL 错误,仍然无法修复

windows - 用于解码 RSA 的 Crypto++ 的小型 Windows 替代方案?

windows - 通过批处理语法查询Windows注册表

python - 使用pyw文件的Windows启动不会关闭cmd

c++ - 如何创建一个类似于 RAID1(镜像)的程序?

windows - 当我们更改文件的扩展名时究竟会发生什么?