Hi, I am trying to get the file size of a .exe file in C, I have tried using the GetFileSize(); function using the CreateFile(); function to get a handle for it, but the value returned is a DWORD and doesnt seem to work when i output it. I want to just retrieve the filesize of a .exe file and store this value in bytes in a long integer, does anyone have any other methods on this or can you help to correct my problem, here is my current code for this part:

HANDLE MF = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
DWORD Size;
GetFileSize(MF,&Size);
                                         
SendMessage(LogListBox,LB_ADDSTRING,0,(LPARAM)Size);

Any suggestions ?

Recommended Answers

All 2 Replies

Look up GetFileSize .

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

int main(void)
{
   const char FilePath[] = "main.exe";
   HANDLE MF = CreateFile(FilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
                          OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
   if ( MF != INVALID_HANDLE_VALUE )
   {
      DWORD High, Low = GetFileSize(MF, &High);
      if ( Low != 0xFFFFFFFF )
      {
         printf("Size : High = %lu, Low = %lu\n", High, Low);
      }
   }
   return 0;
}

/* my output
Size : High = 0, Low = 52736
*/

if the file isn't too big, stat() will return its size, among other file info. you don't have to open the file to use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.