tags:

views:

71

answers:

1

Please see the code:

int main(int argc,LPTSTR argv[])
{
HANDLE hinFile;
BOOL check;
PLARGE_INTEGER file_size;
hinFile=CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hinFile==INVALID_HANDLE_VALUE)
{printf("INVALID_HANDLE_VALUE\nFile Does Not Exist");exit(0);}
else{
    check=GetFileSizeEx(hinFile,file_size);
    printf("The Size of File is %d",file_size);
}
return 0;
}

Now, in the above program whether i try to open a file in directory of the Executable or i specify the path as command line arguments, the only output i get is: INVALID_HANDLE_VALUE\nFile Does Not Exist

Please Explain why this is happening?

+2  A: 

I modified a little your code to make it work.

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

int _tmain(int argc, TCHAR *argv[])
{
HANDLE hinFile;
BOOL check;
LARGE_INTEGER li;
PLARGE_INTEGER file_size;

file_size=&li;

hinFile=CreateFile(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hinFile==INVALID_HANDLE_VALUE)
{
    printf("INVALID_HANDLE_VALUE\nFile Does Not Exist");
}
else
{
    check = GetFileSizeEx(hinFile,file_size);
    printf("The Size of File is %I64d",file_size->QuadPart);
}
return 0;
}

I tested with Visual Studio 2005 (Version 8.0).

Iulian Şerbănoiu
No its not working.
strut
Show me the command line you're using.
Iulian Şerbănoiu
This is what i enter:c:> getfilesize.exe "D:\Image.nrg"
strut
try to copy the executable on D:\ and run it like this: getfilesize.exe Image.nrg. Also print the argv[1] command line argument. What is the compiler you're using?
Iulian Şerbănoiu
strut
I modified the source code a little. Probably you were compiling in Unicode first and the received argument (argv) was not `wchar_t*` but `char*`. Please try the new code (I modified it a little) or compile your old code in ANSI (not Unicode)
Iulian Şerbănoiu
You are the MAN!!! THANKS MAN ....THANKS A TON... This ANSI vs UNICODE Thing is not so clear to me, could you please explain what happened???? –
strut
All right! It was like this: when you declared int main(int argc, LPTSTR argv[]) the signature was like this in unicode: int main(int argc, wchar_t* argv[]). Unfortunatelly in Visual Studio the main function is called with char* arguments instead of wchar_t* arguments. Since you were accessing them as wchar_t ... the information there was pretty messed up (each wchar_t would contain 2 bytes which would lead to unpredictable results; like expecting apples and receiving axes). More info here: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/2d5af4be-8baa-4ff1-bd65-21aefa049e4b
Iulian Şerbănoiu