Thread View: borland.public.cppbuilder.nativeapi
18 messages
18 total messages
Started by Jos�
Wed, 02 Jan 2008 19:51
Cannot retrieve file date
Author: Jos�
Date: Wed, 02 Jan 2008 19:51
Date: Wed, 02 Jan 2008 19:51
36 lines
1875 bytes
1875 bytes
What went wrong?
_FILETIME f1;
FILE*han1=fopen("J:\\Projects\\test1.h","r");
res=GetFileTime(han1, &f1,NULL,NULL);
The file exists, han1 is not zero and the file can be read. But
GetFileTime returns an error.
I tried to find what went wrong. The help file tells me to:
LPVOID lpMsgBuf;
But that causes a compilation error, ("cannot convert void * to const
char *) so I declared:
char* lpMsgBuf;
and it goes on:
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION);
LocalFree( lpMsgBuf );
The error message is "De ingang is ongeldig". If I set the language to
English, no message is returned. Translated into English, it would be
"The entry is invalid".
Well, that's good to know: the entry is invalid! But what is the
entry? And why is it invalid?
--
Jos�
Re: Cannot retrieve file date
Author: Asger Joergensen
Date: Wed, 02 Jan 2008 21:00
Date: Wed, 02 Jan 2008 21:00
14 lines
1210 bytes
1210 bytes
Hi Jos
Jos says:
> What went wrong?
>
> _FILETIME f1;
> FILE*han1=fopen("J:\\Projects\\test1.h","r");
> res=GetFileTime(han1, &f1,NULL,NULL);
You need to use the Win API CreateFile toget the handle of the file
Kind regards
Asger
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Wed, 02 Jan 2008 21:02
Date: Wed, 02 Jan 2008 21:02
17 lines
1479 bytes
1479 bytes
"Jos�" <jose@127.0.0.1> wrote in message news:hfonn3l8o175ht0j11i9d4q5msanpr6apt@4ax.com... > What went wrong? You cannot pass a C runtime FILE* handle to a Win32 API function. You need to use the Win32 API CreateFile() function instead of the C runtime fopen() function. > The file exists, han1 is not zero and the file can be read. > But GetFileTime returns an error. Because you are not passing a valid Win32 API file handle to it. Gambit
Re: Cannot retrieve file date
Author: Jos�
Date: Wed, 02 Jan 2008 21:37
Date: Wed, 02 Jan 2008 21:37
23 lines
1828 bytes
1828 bytes
On Wed, 2 Jan 2008 13:02:06 -0800, "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote in borland.public.cppbuilder.nativeapi: >> What went wrong? > >You cannot pass a C runtime FILE* handle to a Win32 API function. You need >to use the Win32 API CreateFile() function instead of the C runtime fopen() >function. > >> The file exists, han1 is not zero and the file can be read. >> But GetFileTime returns an error. > >Because you are not passing a valid Win32 API file handle to it. The help file only says that a handle must be passed, so it's obvious to use fopen(). Anyhow, CreateFile() won't do, I want to know the time of an existing file. It is usually not very useful to create a file and obtain the date immediately afterwards. My only current solution is findfirst() . This also returns the date of a file. -- Jos�
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Wed, 02 Jan 2008 22:05
Date: Wed, 02 Jan 2008 22:05
69 lines
2834 bytes
2834 bytes
-----------------------------
#include <cstdio>
#include <io.h>
using namespace std;
HANDLE STDCALL WindowsHandleFromFILEHandle(FILE *fp)
{
return (HANDLE) _get_osfhandle(fileno(fp));
}
...
in some function
_FILETIME f1 = { 0 };
FILE*han1=fopen("J:\\Projects\\test1.h","r");
if (han1 != NULL) // if file has successfully opened
{
BOOL res;
res=GetFileTime(WindowsHandleFromFILE(han1), &f1,NULL,NULL);
fclose(han1);
han1 = NULL;
if (res == 0) // if error in GetFileTime
memset(&f1, 0, sizeof(f1))l
}
-----------------------------
. Ed
> Jos� wrote in message
> news:hfonn3l8o175ht0j11i9d4q5msanpr6apt@4ax.com...
> What went wrong?
>
> _FILETIME f1;
> FILE*han1=fopen("J:\\Projects\\test1.h","r");
> res=GetFileTime(han1, &f1,NULL,NULL);
>
> The file exists, han1 is not zero and the file can be read. But
> GetFileTime returns an error.
>
> I tried to find what went wrong. The help file tells me to:
>
> LPVOID lpMsgBuf;
>
> But that causes a compilation error, ("cannot convert void * to const
> char *) so I declared:
>
> char* lpMsgBuf;
>
> and it goes on:
>
> FormatMessage(
> FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
> NULL, GetLastError(),
> MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
> (LPTSTR) &lpMsgBuf, 0, NULL);
> MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION);
> LocalFree( lpMsgBuf );
>
> The error message is "De ingang is ongeldig". If I set the language to
> English, no message is returned. Translated into English, it would be
> "The entry is invalid".
>
> Well, that's good to know: the entry is invalid! But what is the
> entry? And why is it invalid?
> --
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Wed, 02 Jan 2008 22:11
Date: Wed, 02 Jan 2008 22:11
82 lines
3223 bytes
3223 bytes
Here is a working function that uses FormatMessage to display what the
problem reported by GetLastError is:
---------------------------
// hwnd - the parent window, use NULL if there is none
// title_text - the text that will show in the title of an error
//
// message box. Use "" or NULL if you do not want to
// provide any text
//
// If GetLastError() returns zero, indicating no error, no
// error message is shown
//
// This function returns the last error number and sets
// last error in the OS to zero
//
DWORD WINAPI ShowLastError(HWND hwnd, const char *title_text)
{
TCHAR *err_msg;
DWORD last_error = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
last_error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPTSTR>(&err_msg),
0,
NULL);
if (title_text && *title_text)
MessageBox(hwnd, err_msg, title_text, MB_OK);
else
MessageBox(hwnd, err_msg, "Error", MB_OK);
LocalFree(err_msg);
SetLastError(0);
return last_error;
}
---------------------------
. Ed
> Jos� wrote in message
> news:hfonn3l8o175ht0j11i9d4q5msanpr6apt@4ax.com...
>
> What went wrong?
>
> _FILETIME f1;
> FILE*han1=fopen("J:\\Projects\\test1.h","r");
> res=GetFileTime(han1, &f1,NULL,NULL);
>
> The file exists, han1 is not zero and the file can be read. But
> GetFileTime returns an error.
>
> I tried to find what went wrong. The help file tells me to:
>
> LPVOID lpMsgBuf;
>
> But that causes a compilation error, ("cannot convert void * to const
> char *) so I declared:
>
> char* lpMsgBuf;
>
> and it goes on:
>
> FormatMessage(
> FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
> NULL, GetLastError(),
> MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
> (LPTSTR) &lpMsgBuf, 0, NULL);
> MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION);
> LocalFree( lpMsgBuf );
>
> The error message is "De ingang is ongeldig". If I set the language to
> English, no message is returned. Translated into English, it would be
> "The entry is invalid".
>
> Well, that's good to know: the entry is invalid! But what is the
> entry? And why is it invalid?
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Wed, 02 Jan 2008 22:13
Date: Wed, 02 Jan 2008 22:13
16 lines
1305 bytes
1305 bytes
Correction.
In my last message this line:
HANDLE STDCALL WindowsHandleFromFILEHandle(FILE *fp)
Should be this:
HANDLE WINAPI WindowsHandleFromFILEHandle(FILE *fp)
and would also work if you wrote it as this:
HANDLE __stdcall WindowsHandleFromFILEHandle(FILE *fp)
. Ed
> Ed Mulroy wrote in message
> news:477c0a92$1@newsgroups.borland.com...
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Wed, 02 Jan 2008 22:35
Date: Wed, 02 Jan 2008 22:35
30 lines
2028 bytes
2028 bytes
"Jos�" <jose@127.0.0.1> wrote in message news:5p0on3lb068m6ob82jeg8565fmt5e2fsh4@4ax.com... > The help file only says that a handle must be passed, so it's > obvious to use fopen(). You are reading the Win32 API help file. It has no concept of the C runtime functions. Everything in that help files expects handles that are provided by the Win32 API only. > Anyhow, CreateFile() won't do, I want to know the time of an existing > file. In order to use GetFiletime(), you must use CreateFile() to get a suitable file handle. Otherwise, use stat() or fstat() instead. > It is usually not very useful to create a file and obtain the date > immediately afterwards. If you read the documentation for CreateFile(), you will see that it is used for both opening existing files as well as creating new files. > My only current solution is findfirst() . This also returns the date of a > file. That is another option. Gambit
Re: Cannot retrieve file date
Author: Jos�
Date: Wed, 02 Jan 2008 22:48
Date: Wed, 02 Jan 2008 22:48
15 lines
1480 bytes
1480 bytes
On Wed, 2 Jan 2008 17:05:00 -0500, "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> wrote in borland.public.cppbuilder.nativeapi: > return (HANDLE) _get_osfhandle(fileno(fp)); Something new for me. I would never have thought of searching for a function named _get_osfhandle(). The help file explains that there is a difference between an "operating system file handle" and a "run-time file handle". It should not be too simple. Oh dear, assembly was so easy, I used it a lot in the time of good old DOS. -- Jos�
Re: Cannot retrieve file date
Author: Jos�
Date: Wed, 02 Jan 2008 22:57
Date: Wed, 02 Jan 2008 22:57
32 lines
2307 bytes
2307 bytes
On Wed, 2 Jan 2008 14:35:00 -0800, "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote in borland.public.cppbuilder.nativeapi: >You are reading the Win32 API help file. It has no concept of the C runtime >functions. Everything in that help files expects handles that are provided >by the Win32 API only. I type a name in my source text and hit F1. I see that the layout of the help text varies, but to me they are just information. Names like "C runtime" and "Win32 API" are Greek to me, although it is obvious that the former is an extra layer. >If you read the documentation for CreateFile(), you will see that it is used >for both opening existing files as well as creating new files. Well yes, you are right again. But I am pretty stupid, I do not consider reading the documentation of CreateFile() when I do not want to create a file. Besides, I always use fopen() to open or create a file. Actually, I find it even more stupid to name a function CreateFile() if it can be used to open an existing file. But I dislike to blame others for my misunderstanding >> My only current solution is findfirst() . This also returns the date of a >> file. > >That is another option. While usually it's a return value you aren't interested in. -- Jos�
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Wed, 02 Jan 2008 23:30
Date: Wed, 02 Jan 2008 23:30
73 lines
4316 bytes
4316 bytes
> Something new for me. I would never have thought of searching > for a function named _get_osfhandle(). That function seems to be little-known. I have needed it only rarely but when I do it has bailed me out of many tough situations. It is provided by the compiler but is considered an "extension" because it not part of the official C or C++ languages. There are many types of file handle. The C and C++ languages define a FILE* pointer which is associated with opened files. Programmers commonly call it a file handle. Another set of I/O routines supplied with the compiler (open/read/write/close) use a file handle which is a number, an integer. These are used very little these days but long ago were frequently used. The operating system uses its own file handling system. As the C and C++ languages were created to be reasonably independent of which operating system is being used the languages implement their file systems using those of the operating system. C and C++ provide no facilities for reading file times so to perform your task you needed to step down to a lower level and use the operating system (the OS) itself. API stands for Application Program Interface, a set of schemes, usually functions to call, provided by something. The Windows API is often called the WIN32 API (the 32 because Windows is 32 bits these days but years ago the API was smaller, a little different and Windows was only 16 bits). Help files cover the languages and those extensions provided that are not part of the official language. When dealing with the native OS information is typically found in a different help file. In this case that help file is the WIN32 API help file. Using it is analagous to the separate documentation you used in the DOS days for Dos and BIOS interrupt calls and information structures such as the DOS List of Lists or BIOS list of serial ports. Do not make the mistake of thinking the help files can teach you the language or the Windows API. The help files are intended as references and not as tutorials. For learning look around on the web for tutorials and look at books. Links to C++ tutorials and a (old) copy of the C++ standard: http://www.mulroy.org/programg.htm#cpptutor Links to other tutorials, including some WIN32 API tutorials: http://www.mulroy.org/programg.htm#othertutor Links to other informative sites about Windows including the WIN32 API http://www.mulroy.org/programg.htm#win32 If for some reason you cannot find the help file for the WIN32 API, this link (at least used to be) where you can get a copy of it: http://www.winasm.net/index.php?ind=downloads&op=entry_view&iden=23 . Ed > Jos� wrote in message > news:f05on3tbpa6au45ii99v71rsn5k89d2ut7@4ax.com... > >> return (HANDLE) _get_osfhandle(fileno(fp)); > > Something new for me. I would never have thought of searching for a > function named _get_osfhandle(). The help file explains that there is > a difference between an "operating system file handle" and a "run-time > file handle". It should not be too simple. > > Oh dear, assembly was so easy, I used it a lot in the time of good old > DOS.
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Jan 2008 00:03
Date: Thu, 03 Jan 2008 00:03
44 lines
3137 bytes
3137 bytes
"Jos�" <jose@127.0.0.1> wrote in message
news:qa5on3d6p24qit5hic73bl397a6pg4b9i3@4ax.com...
> Well yes, you are right again. But I am pretty stupid, I do not
> consider reading the documentation of CreateFile() when I do
> not want to create a file.
The documentation for GetFileTime() says the following:
hFile
A handle to the file or directory for which dates and times are to
be retrieved. The handle must have been created using the CreateFile
function with the GENERIC_READ access right. For more
Besides, you posted your original question in the Win32 API newsgroup to
begin with, so you got an answer related to the Win32 API.
> Besides, I always use fopen() to open or create a file.
Like I said earlier, the C runtime (which you are using to open the file in
the first place) has its own functions for retrieving file information -
stat() and fstat(). You can't really mix APIs like you arte attempting. If
you open a file (or any other resource) using the C runtime, then you have
to use the C runtime to work with it. If you open a file/resource using the
Win32 API directly, then you have to use the API to access it.
> Actually, I find it even more stupid to name a function CreateFile()
> if it can be used to open an existing file.
CreateFile() is not creating a file itself. It is creating a "handle" to
the file, so there is always a resource creation occuring. How the handle
interprets the actual file depends on the flags passed to CreateFile().
There MAY be a file creation occuring as well, but only if the function is
told to do that.
The same argument can be said of fopen(), though. It is able to create new
files even though its name suggests it can only open them. There is no
separate fcreate() function. You have to tell fopen() whether you are
opening an existing file or creating a new file. The same applies to
CreateFile().
Gambit
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Jan 2008 00:06
Date: Thu, 03 Jan 2008 00:06
11 lines
1402 bytes
1402 bytes
"Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> wrote in message news:477c1ea9$1@newsgroups.borland.com... > C and C++ provide no facilities for reading file times stat() and fstat(). Both return a struct that has st_atime, st_mtime, and st_ctime members. Gambit
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Thu, 03 Jan 2008 00:27
Date: Thu, 03 Jan 2008 00:27
19 lines
1921 bytes
1921 bytes
Has something changed? When I last looked neither C nor C++ provided means for reading file times. Both of stat and fstat were provided as extensions but are part of Posix and not of the C or C++ standards. Also note that he was using FILE* access and neither stat nor fstat takes a FILE* argument. They operate on an int file handle such as is returned by open or _rtl_open. . Ed > Remy Lebeau wrote in message > news:477c2759$1@newsgroups.borland.com... > >> C and C++ provide no facilities for reading file times > > stat() and fstat(). Both return a struct that has st_atime, st_mtime, and > st_ctime members.
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Jan 2008 03:34
Date: Thu, 03 Jan 2008 03:34
12 lines
1438 bytes
1438 bytes
"Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> wrote in message news:477c2bea@newsgroups.borland.com... > Also note that he was using FILE* access and neither > stat nor fstat takes a FILE* argument. Not directly, but you can use fileno() to convert a FILE* into an int that fstat() will take. Gambit
Re: Cannot retrieve file date
Author: Jos�
Date: Thu, 03 Jan 2008 11:03
Date: Thu, 03 Jan 2008 11:03
20 lines
1851 bytes
1851 bytes
On Wed, 2 Jan 2008 16:03:41 -0800, "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote in borland.public.cppbuilder.nativeapi: >CreateFile() is not creating a file itself. It is creating a "handle" to >the file, so there is always a resource creation occuring. > >The same argument can be said of fopen(), though. It is able to create new >files even though its name suggests it can only open them. The name CreateFile() is rather threatening. I do not want to create a file which already exists, destroying the existing file. OTOH, the name fopen() sounds rather safe. If it offers the unexpected extra service to create a non-existant file, it causes no damage. I always used fopen() to handle files. Is there a reason why I should use CreateFile()? Is the use of either function obsolete and frowned upon? -- Jos�
Re: Cannot retrieve file date
Author: Ed Mulroy [TeamB]
Date: Thu, 03 Jan 2008 15:09
Date: Thu, 03 Jan 2008 15:09
14 lines
1594 bytes
1594 bytes
Yes, just as was done in the example where fileno() was used to convert to an int to pass to _get_osfhandle(), which, like fstat() is an extension supplied with the compiler. . Ed > Remy Lebeau wrote in message > news:477c59d0$1@newsgroups.borland.com... > >> Also note that he was using FILE* access and neither >> stat nor fstat takes a FILE* argument. > > Not directly, but you can use fileno() to convert a FILE* into an int that > fstat() will take.
Re: Cannot retrieve file date
Author: Remy Lebeau (TeamB)
Date: Thu, 03 Jan 2008 18:25
Date: Thu, 03 Jan 2008 18:25
13 lines
1655 bytes
1655 bytes
"Jos�" <jose@127.0.0.1> wrote in message news:i0gpn35bvgrd66k53s7usnqaok04m6b9dd@4ax.com... > I always used fopen() to handle files. Is there a reason why I > should use CreateFile()? Is the use of either function obsolete > and frowned upon? fopen() is a standardize function that is platform-agnostic. It uses CreateFile() internally when running on a Windows platform. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads