代码之家  ›  专栏  ›  技术社区  ›  Sijith

转换为LPCTSTR时出现文件复制问题

  •  0
  • Sijith  · 技术社区  · 14 年前
    // Convert to a wchar_t*
    
    size_t origsize = strlen(toChar) + 1;
    
    const size_t newsize = 100;
    
    size_t convertedChars = 0;
    
    wchar_t wcstring[newsize];
    
    mbstowcs_s(&convertedChars, wcstring, origsize, toChar, _TRUNCATE);
    
    wcscat_s(wcstring, L"\\*.*\0");
    
    wcout << wcstring << endl; // C:\Documents and Settings\softnotions\Desktop\Release\*.*
    
    
    
    SHFILEOPSTRUCT sf;
    
    memset(&sf,0,sizeof(sf));
    
    sf.hwnd = 0;
    
    sf.wFunc = FO_COPY;
    
    //sf.pFrom =wcstring;  /* when giving wcstring i am not getting answer */
    
     sf.pFrom = L"C:\\Documents and Settings\\softnotions\\Desktop\\Release\\*.*\0";
    
       wcout << sf.pFrom  <<endl;   // C:\Documents and Settings\softnotions\Desktop\Release\*.*
    

    两者 wcstring sf.pFrom 是一样的那为什么不在分配时得到答案 sf.pFrom =wcstring;

    1 回复  |  直到 14 年前
        1
  •  1
  •   ChrisN    14 年前

    SHFILEOPSTRUCT 要求 pFrom pTo 成为 double-null-terminated strings .

    指定给的字符串文本 光驱 嵌入了 \0 ,因此字符串以双空结尾。

    当你打电话 wcscat_s ,嵌入的 \0个 解释为要追加的字符串的结尾,因此生成的字符串不是以双null结尾的。

    正如您在评论中所说,您可以这样做(尽管您需要的功能是 wcslen ):

    wcscat_s(wcstring, L"\\*.*");
    wcstring[wcslen(wcstring) + 1] = 0;