SHFILEOPSTRUCT 不确定如何加倍转义字符串
SHFILEOPSTRUCT not sure how to double escape strings
我正在尝试将 运行ning 可执行文件复制到特定文件夹。我想用相同的安全资源属性复制它。但是,我不确定我 "double escaping" 文件名是否正确 doc
我的代码如下:
#include <shlobj.h>
#include <shellapi.h>
#define LIVE_FOLDER "Cloud9"
bool CopySelf()
{
TCHAR buf[MAX_PATH];
TCHAR path[MAX_PATH];
TCHAR ogpath[MAX_PATH];
// The function gets the current module for you.
SHGetFolderPath(NULL,CSIDL_APPDATA, NULL, 0, path);
GetCurrentDirectory(MAX_PATH, ogpath);
std::string dir = std::string(path)+"\"+LIVE_FOLDER ;
std::string dirog = std::string(ogpath);
if(dir==dirog)
{
return false;
}
if (CreateDirectory(dir.c_str(), NULL) ||
ERROR_ALREADY_EXISTS == GetLastError())
{
std::string newprocessName = dir + "\test.exe[=10=][=10=]";
GetModuleFileName(0, buf, MAX_PATH);
std::string oldprocessName = std::string(buf)+"[=10=][=10=]";
SHFILEOPSTRUCT a=
{
NULL,
FO_COPY,
oldprocessName.c_str(),
newprocessName.c_str(),
FOF_SILENT|FOF_NOERRORUI|FOF_NOCONFIRMATION,
FALSE,
NULL,
NULL,
};
SHFileOperation(&a);
return true;
}
else
{
throw ERROR;
// return false;
}
return false;
}
当我 运行 这段代码时,目标位置不是可执行文件,而是一个名为 "test.exe".Image is here - not enough points to embed it.
的文件夹
如果我将 CopyFile 或 CopyFileEx 与以下代码一起使用,文件将被正确复制。
std::string newprocessName = dir + "\test.exe";
GetModuleFileName(0, buf, MAX_PATH);
CopyFile(buf, newprocessName.c_str(), TRUE);
问题是没有复制安全属性。在这一点上,我不确定是我没有通过 "double escaping"/"double null terminating" 还是我做错了什么。为什么它创建一个名为 "test.exe" 的文件夹而不是复制文件?
我正在使用 Code::Blocks 和 GCC 编译器,我已经在 Windows 8.1 Professional 和 Windows 7 Professional 上试过了。
终于想通了。问题确实出在双空终止
我使用 append 附加了一个空终止符,如下所示,并在声明字符串时使用了一个空终止符:
std::string newprocessName = dir + "\test.exe[=10=]";
GetModuleFileName(0, buf, MAX_PATH);
std::string oldprocessName = std::string(buf)+"[=10=]";
newprocessName.append(1, '[=10=]');
oldprocessName.append(1, '[=10=]');
不确定这有何不同,但它现在可以正常工作了。 this SO page 接受的答案正是我所需要的。
我正在尝试将 运行ning 可执行文件复制到特定文件夹。我想用相同的安全资源属性复制它。但是,我不确定我 "double escaping" 文件名是否正确 doc
我的代码如下:
#include <shlobj.h>
#include <shellapi.h>
#define LIVE_FOLDER "Cloud9"
bool CopySelf()
{
TCHAR buf[MAX_PATH];
TCHAR path[MAX_PATH];
TCHAR ogpath[MAX_PATH];
// The function gets the current module for you.
SHGetFolderPath(NULL,CSIDL_APPDATA, NULL, 0, path);
GetCurrentDirectory(MAX_PATH, ogpath);
std::string dir = std::string(path)+"\"+LIVE_FOLDER ;
std::string dirog = std::string(ogpath);
if(dir==dirog)
{
return false;
}
if (CreateDirectory(dir.c_str(), NULL) ||
ERROR_ALREADY_EXISTS == GetLastError())
{
std::string newprocessName = dir + "\test.exe[=10=][=10=]";
GetModuleFileName(0, buf, MAX_PATH);
std::string oldprocessName = std::string(buf)+"[=10=][=10=]";
SHFILEOPSTRUCT a=
{
NULL,
FO_COPY,
oldprocessName.c_str(),
newprocessName.c_str(),
FOF_SILENT|FOF_NOERRORUI|FOF_NOCONFIRMATION,
FALSE,
NULL,
NULL,
};
SHFileOperation(&a);
return true;
}
else
{
throw ERROR;
// return false;
}
return false;
}
当我 运行 这段代码时,目标位置不是可执行文件,而是一个名为 "test.exe".Image is here - not enough points to embed it.
的文件夹如果我将 CopyFile 或 CopyFileEx 与以下代码一起使用,文件将被正确复制。
std::string newprocessName = dir + "\test.exe";
GetModuleFileName(0, buf, MAX_PATH);
CopyFile(buf, newprocessName.c_str(), TRUE);
问题是没有复制安全属性。在这一点上,我不确定是我没有通过 "double escaping"/"double null terminating" 还是我做错了什么。为什么它创建一个名为 "test.exe" 的文件夹而不是复制文件?
我正在使用 Code::Blocks 和 GCC 编译器,我已经在 Windows 8.1 Professional 和 Windows 7 Professional 上试过了。
终于想通了。问题确实出在双空终止
我使用 append 附加了一个空终止符,如下所示,并在声明字符串时使用了一个空终止符:
std::string newprocessName = dir + "\test.exe[=10=]";
GetModuleFileName(0, buf, MAX_PATH);
std::string oldprocessName = std::string(buf)+"[=10=]";
newprocessName.append(1, '[=10=]');
oldprocessName.append(1, '[=10=]');
不确定这有何不同,但它现在可以正常工作了。 this SO page 接受的答案正是我所需要的。