我的c ++代码可以直接工作几次,经过几次执行后它突然停止工作并抛出异常(没有任何变化!),我无法弄清楚原因.
这是代码中有问题的部分:
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
TCHAR *path;
SHGetKnownFolderPath(FOLDERID_Startup, KF_FLAG_CREATE, NULL, &path);
lstrcat(path, L"\\calc.exe");
if (CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
Run Code Online (Sandbox Code Playgroud)
执行几次后,CreateProcess()行会抛出2个异常,第一个:
Unhandled exception at 0x779D8829 (ntdll.dll) in PS_Down.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77A15890).
Run Code Online (Sandbox Code Playgroud)
第二:
Exception thrown at 0x77946111 (ntdll.dll) in PS_Down.exe: 0xC0000005: Access violation reading location 0x00000069.
Run Code Online (Sandbox Code Playgroud)
它发生在我的一些其他项目(不包括CreateProcess()函数)和iv'e注意到它总是发生在涉及TCHAR和SHGetKnownFolderPath()时.任何有助于理解如何解决问题的帮助将非常感谢,提前感谢!
PS - 我是cpp编码的新手,所以请尝试相应地解释
我正在制作一个程序,以递归方式列出某个目录中的所有文件,并使用WinINet将每个文件分别上传到FTP服务器.我遇到的问题是在FtpPutFile()函数中使用filesystem :: path :: filename,因为需要LPCWSTR.什么是最好和最简单的转换方式(或以某种方式使用它)?
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().filename(), entry.path().filename(), FTP_TRANSFER_TYPE_BINARY, 0);
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:没有合适的转换函数从"const std :: experimental :: filesystem :: v1 :: path"到"LPCWSTR"存在
编辑:这是一个适合我的解决方案,遵循Lightness解决方案:
std::string path = "C:\\Programs";
for (const auto & entry : std::experimental::filesystem::recursive_directory_iterator(path))
FtpPutFile(hIConnect, entry.path().wstring().c_str(), entry.path().filename().wstring().c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
Run Code Online (Sandbox Code Playgroud)