zoukankan      html  css  js  c++  java
  • 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,所以要这么做,不然删除文件夹会失败的

  • 相关阅读:
    编译使用tinyxml
    GitLab 项目创建后地址由Localhost改为实际IP的方法
    树莓派相机设定
    MongoDB的数据备份与恢复
    Nginx PHP fpm forbidden 原因
    PSR2规范
    docker 日志管理
    Docker 拷贝文件
    Docker MySQL基本操作
    deepin安装php5.6
  • 原文地址:https://www.cnblogs.com/likemao/p/9205477.html
Copyright © 2011-2022 走看看