开发者

Access violation at address in module 'executable.exe'. Read of address

开发者 https://www.devze.com 2023-03-19 10:40 出处:网络
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

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消