0

GetFileAttributesExW()用来获取文件的日期。在调试模式(Visual Studio 2012)下,一切正常,我得到例如"2013-05-10"。但是当我将目标更改为RELEASE时,日期已损坏(我在函数的末尾得到字符串,例如"1678-09-08")。

完整代码:

bool Registry::fileDate(std::wstring pathString, std::string &dateOut)
{
    if (pathString.size() == 0)
        return false;

    if (pathString[0] == '\"')
    {
        pathString = pathString.erase(0, 1);
    }

    if (pathString[pathString.size()-1] == '\"')
    {
        pathString = pathString.erase(pathString.size()-1, 1);
    }

    size_t pos = 0;
    size_t found = pathString.find('\"', pos);
    if (found > 0 && found != std::string::npos)
    {
        pathString = pathString.substr(0, found);
    }

    std::wstring filePath = pathString;

    WIN32_FILE_ATTRIBUTE_DATA fileData;
    GetFileAttributesExW(filePath.c_str(), GetFileExInfoStandard, &fileData);
    SYSTEMTIME systemTime;

    bool res = FileTimeToSystemTime(&fileData.ftCreationTime, &systemTime);
    if (res)
    {
        char buf[40] = {0};
        for(int i=0; i<40; i++) //to be even more certain that no trash will get here
            buf[i] = ' ';
        sprintf(buf,"%04d-%02d-%02d",
                systemTime.wYear, systemTime.wMonth, systemTime.wDay);
        dateOut = buf+'\0';
    }
    else
    {
        //show error's window, i'm sure it DOESN'T occur, both in DEBUG and RELEASE
    }
}

我知道在RELEASE中必须进行一些优化。优化和我的错误(我看不到)一起必须导致日期损坏。

到函数去字符串,如:

L"\"C:\\Program Files\\File\\setup.exe\" /uninstall PROPLUS /dll OSETUP.DLL"

DEBUG中,在我的函数的第一行中将其转换为:

L"C:\\Program Files\\File\\setup.exe"

编辑:( 感谢Raymond Chen的评论)

现在我知道在 RELEASE 中,GetFileAttributesExW()返回FALSE,所以也许该部分在DEBUG中有效并且在RELEASE中无效(它应该从路径中删除额外的"/params ):

if(pathString.size() == 0)
    return false;

if(pathString[0] == '\"'){
    pathString = pathString.erase(0, 1);
}

if(pathString[pathString.size()-1] == '\"'){
    pathString = pathString.erase(pathString.size()-1, 1);
}

size_t pos = 0;
size_t found = pathString.find('\"', pos);
if (found > 0 && found != std::string::npos)
{
    pathString = pathString.substr(0, found);
}

std::wstring filePath = pathString;
4

0 回答 0