在Debug中工作时,GetFileAttributesExW()或FileTimeToSystemTime()在Release中不起作用

时间:2013-11-05 13:50:23

标签: c++ visual-studio debugging release visual-studio-debugging

我使用GetFileAttributesExW()来获取文件的日期。在 DEBUG 模式(Visual Studio 2012)中,一切正常,我得到了 “2013年5月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 中,必须进行一些优化。那些优化和我的错误(我看不到)必然会导致日期损坏。

到函数go字符串:

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;

0 个答案:

没有答案