I've read some problems with "Access Violation" on the net but this is very weird for me
I have tested some "solutions" but with no results
This is the piece of code:
TDateTime CFile开发者_JAVA技巧VersionInfo::GetFileDate() const
{
FILETIME local;
SYSTEMTIME st;
TDateTime res;
FILETIME ft;
ft.dwLowDateTime = m_FileInfo.dwFileDateLS;
ft.dwHighDateTime = m_FileInfo.dwFileDateMS;
FileTimeToLocalFileTime(&ft, &local);
FileTimeToSystemTime(&local, &st) ;
//GetLocalTime(st);
res = SystemTimeToDateTime(st) ;
return res;
}
I can Make or Build the program with no problems but when I run the program it show me the Access Violation error, if I comment the function:
// TDateTime dateTime = fvi.GetFileDate();
The program run perfectly
I'm not a C++ pro I just want to make a program for personal use so I ask this question to expert
EDIT:
I've resolved the problem
TDateTime CFileVersionInfo::GetFileDate() const
{
_FILETIME local;
_SYSTEMTIME st;
TDateTime res;
FILETIME ft;
ft.dwLowDateTime = m_FileInfo.dwFileDateLS;
ft.dwHighDateTime = m_FileInfo.dwFileDateMS;
FileTimeToLocalFileTime(&ft, &local);
FileTimeToSystemTime(&local, &st) ;
GetLocalTime(&st); // <-- This is the solution.. for now...
res = SystemTimeToDateTime(st) ;
return res;
}
You are not doing any error checking to make sure the API conversions are succeeding, so you could be trying to convert trash data. Always check for errors, eg:
TDateTime CFileVersionInfo::GetFileDate() const
{
FILETIME local;
SYSTEMTIME st;
FILETIME ft;
ft.dwLowDateTime = m_FileInfo.dwFileDateLS;
ft.dwHighDateTime = m_FileInfo.dwFileDateMS;
if( FileTimeToLocalFileTime(&ft, &local) )
{
if( FileTimeToSystemTime(&local, &st) )
return SystemTimeToDateTime(st);
}
return 0.0;
}
I've resolved the problem
TDateTime CFileVersionInfo::GetFileDate() const
{
_FILETIME local;
_SYSTEMTIME st;
TDateTime res;
FILETIME ft;
ft.dwLowDateTime = m_FileInfo.dwFileDateLS;
ft.dwHighDateTime = m_FileInfo.dwFileDateMS;
FileTimeToLocalFileTime(&ft, &local);
FileTimeToSystemTime(&local, &st) ;
GetLocalTime(&st); // <-- This is the solution.. for now...
res = SystemTimeToDateTime(st) ;
return res;
}
精彩评论