SHFileOperation 解决double-null terminated

void rubyTools::funStrToWstr(string str, wstring& strw)
{
    const char* pData = str.c_str();
    int lengt = str.length();
    UINT CodePage = 936;
    DWORD dwNum = MultiByteToWideChar(CodePage, 0,
        pData, -1, NULL, 0);
    if (dwNum == 0)
    {
        return;
    }


    WCHAR* pwText = new WCHAR[dwNum];
    memset(pwText, '', dwNum);
    MultiByteToWideChar(CodePage, 0, pData, -1, pwText, dwNum);
    strw = pwText;
    delete []pwText;
}
wstring deletePath;
    funStrToWstr(mainrbPath + "\lib", deletePath);
    SHFILEOPSTRUCT FileOp = { 0 };
    FileOp.hwnd= (HWND)this->winId();
    FileOp.fFlags = FOF_NOCONFIRMATION | FOF_SILENT;

    const wchar_t  *tmp = deletePath.c_str();
    wchar_t *pTmp = const_cast<wchar_t *>(tmp);
    int a = wcslen(pTmp);
    //pTmp[a + 1] = '';//暴力增加double-null terminated.这个时候字符串结尾"   "了(widechar) dangerous
    wchar_t *tmp2 = new wchar_t[a + 2];
    wmemcpy(tmp2, tmp, a);
    wmemcpy(tmp2 + a, L"", 2);

    FileOp.pFrom = tmp2;
    FileOp.pTo = NULL;  
    FileOp.wFunc = FO_DELETE;
    if( SHFileOperation(&FileOp) != 0)
        errorNum |= replaceLibError;
    delete[]tmp2;

 由于参数必须是must be double-null terminated,所以要这么做,不然删除文件夹会失败的

原文地址:https://www.cnblogs.com/likemao/p/9205477.html