C++ 在C+中获取windows系统应用程序的文件大小+;

C++ 在C+中获取windows系统应用程序的文件大小+;,c++,windows,C++,Windows,我正在尝试获取windows上系统应用程序的文件大小。为了测试这一点,我创建了一个测试应用程序,尝试获取C:\Windows\System32\smss.exe中smss.exe的文件大小,但失败并出现错误:错误\未找到文件\未找到文件。该文件确实存在(我已检查)。我还尝试了不同的方法来获取文件大小,包括:FindFirstFile、CreateFile和GetFileSizeEx。但都返回相同的错误。我还想阅读文件内容 我做错了什么 守则: // Test.cpp : Defines the

我正在尝试获取windows上系统应用程序的文件大小。为了测试这一点,我创建了一个测试应用程序,尝试获取C:\Windows\System32\smss.exe中smss.exe的文件大小,但失败并出现错误:错误\未找到文件\未找到文件。该文件确实存在(我已检查)。我还尝试了不同的方法来获取文件大小,包括: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"));
}
//Test.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括
__int64 getFileSize(LPWSTR文件路径)
{
WIN32_文件_属性_数据时尚;
如果(!GetFileAttributesEx(文件路径、GetFileExInfo标准和fad))
{
_tprintf(文本(“\n无法获取文件%s错误%d的文件大小”)、文件路径、GetLastError();
返回0;
}
大整数大小;
size.HighPart=fad.nFileSizeHigh;
size.LowPart=fad.nFileSizeLow;
返回大小。四分之一部分;
}
int _tmain(int argc,_TCHAR*argv[]
{
_tprintf(文本(“文件大小%d”)、getFileSize(L“C:\\Windows\\System32\\smss.exe”);
}

这里已经回答了获取文件大小的问题(还不能为您的问题添加注释,因此我需要将其作为答案编写):


由于您的应用程序是32位的,因此系统会选择转到SysWOW64的路径,因为那里没有
smss.exe
。当你发现禁用这个重定向时,也要考虑有一个64位的程序也会起作用。

我猜是<代码> SMS.exe 。禁用WOW64 DISABLE WOW64 FSReDebug()的重定向解决了它!谢谢,如果你发布一个完整的答案,我很乐意接受。这仍然不会让文件被找到。这需要打开文件。
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(); 
}