NO

Author Topic: GetFileTime example not working  (Read 5243 times)

tpekar

  • Guest
GetFileTime example not working
« on: July 27, 2011, 06:32:31 PM »
When I try to compile this example code using GetFileTime I get the following polink error:

Building C:\Users\pekar\Documents\Pelles C Projects\getfiltm\output\getfiltm.obj.
C:\Users\pekar\Documents\Pelles C Projects\getfiltm\getfiltm.c(38): warning #2203: Function 'main' can't be __stdcall, changed to __cdecl.
Building C:\Users\pekar\Documents\Pelles C Projects\getfiltm\getfiltm.exe.
POLINK: error: Unresolved external symbol '_WinMain@16'.
POLINK: fatal error: 1 unresolved external(s).
*** Error code: 1 ***
Done.

Here is the code:
Code: [Select]
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

// GetLastWriteTime - Retrieves the last-write time and converts
//                    the time to a string
//
// Return value - TRUE if successful, FALSE otherwise
// hFile      - Valid file handle
// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)
{
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC, stLocal;
    DWORD dwRet;

    // Retrieve the file times for the file.
    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
        return FALSE;

    // Convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

    // Build a string showing the date and time.
    dwRet = StringCchPrintf(lpszString, dwSize,
        TEXT("%02d/%02d/%d  %02d:%02d"),
        stLocal.wMonth, stLocal.wDay, stLocal.wYear,
        stLocal.wHour, stLocal.wMinute);

    if( S_OK == dwRet )
        return TRUE;
    else return FALSE;
}

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile;
    TCHAR szBuf[MAX_PATH];

    if( argc != 2 )
    {
        printf("This sample takes a file name as a parameter\n");
        return 0;
    }
    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile failed with %d\n", GetLastError());
        return 0;
    }
    if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))
         _tprintf(TEXT("Last write time is: %s\n"), szBuf);
    CloseHandle(hFile);   
}
« Last Edit: July 29, 2011, 08:50:37 AM by frankie »

CommonTater

  • Guest
Re: GetFileTime example not working
« Reply #1 on: July 27, 2011, 06:43:42 PM »
Compile the code as a console program... not as Windows GUI...

Project -> Project Options -> Subsystem -> Console

Also if you are using windows calls enable the Microsoft Extensions and Pelles C extensions.

You can avoid the first error by prototyping your main() as...

int __cdecl main(int arcg, char argv[])

Unless you are using #define UNICODE and #define _UNICODE there's no need to use _tmain() or TCHAR...



tpekar

  • Guest
Re: GetFileTime example not working
« Reply #2 on: July 28, 2011, 09:21:20 PM »
Thanks.  That worked.