c++ - SHFileOperation cannot remove folder -
i writing c++ custom action wix called during installation remove leftovers installed installer. consider following code:
uint __stdcall deleteresidue(msihandle hinstall) { hresult hr = s_ok; uint er = error_success; lpwstr lpfolderpath = null; std::wstring temp; shfileopstruct shfile; hr = wcainitialize(hinstall, "deleteresidue"); exitonfailure(hr, "failed initialize"); hr = wcagetproperty(l"lpcommappdatafolder",&lpfolderpath); exitonfailure(hr, "failure in finding common app data folder"); temp = std::wstring(lpfolderpath); temp+=l"\0\0"; //stop lpa , lpa monitor service. delete residue. wcalog(logmsg_standard, "doing delete residue"); zeromemory(&shfile, sizeof(shfile)); shfile.hwnd = null; shfile.wfunc = fo_delete; shfile.pfrom = temp.c_str(); shfile.fflags = fof_allowundo | fof_noconfirmation | fof_noerrorui; bool res = directoryexists(lpfolderpath); if(res) { wcalog(logmsg_standard, "the directory exist"); int result = shfileoperation(&shfile); if(!result) wcalog(logmsg_standard, "the directory should have deleted now"); else wcalog(logmsg_standard, "the directory not deleted.error code %d", result); } else { wcalog(logmsg_standard, "it seems installed folder no more there"); } lexit: er = succeeded(hr) ? error_success : error_install_failure; return wcafinalize(er); } in above code, c:\programdata inlpcommappdatafolder. doc states pfrom should double null terminated. return value of code 0x2 i.e. error_file_not_found. wrong in code above?
you not double nul terminating pfrom.
you have standard string (which includes null terminator when call .c_str() on it).
temp = std::wstring(lpfolderpath); you concatenate empty string onto it:
temp+=l"\0\0"; this leaves original string unchanged. because std::string::operator+(const wchar_t*) takes null terminated string. fact have 2 nulls immaterial reads first null. doesn't add null you've given empty string , result of concatenating empty string else no-op.
there's few ways solve this, easiest change
temp+=l"\0\0"; to
temp.push_back(l'\0'); which explicitly adds nul string when eventaully call temp.c_str() you'll double nul-terminated string need.
Comments
Post a Comment