LPTSTR currentDirectoryLPTSTR = new TCHAR[currentDirectory.size() + 1];
allocates memory, but does not fill it with any data.
std::string(currentDirectoryLPTSTR)
creates a new string
object and tries to copy data from currentDirectoryLPTSTR
into the string
. Which is undefined behavior since currentDirectoryLPTSTR
is not a proper null-terminated string. This code does not cause the string
object to point at the memory allocated for currentDirectoryLPTSTR
, like you clearly think it does.
You are then append()
ing File
to that string
object, not to the contents of currentDirectoryLPTSTR
.
And then you throw away the string
object you just created, and pass the unfilled currentDirectoryLPTSTR
as-is to URLDownloadToFile()
. Which is why your output file has a messed-up filename (you are lucky it even shows up in the correct folder at all).
Try this instead:
std::string GetCurrentDirectory()
{
char buffer[MAX_PATH] = {};
DWORD size = GetModuleFileNameA(NULL, buffer, MAX_PATH);
if (size == 0 || size == MAX_PATH) return "";
std::string fileName(buffer, size);
std::string::size_type pos = fileName.find_last_of("\/");
return fileName.substr(0, pos + 1);
}
void StartDownload()
{
HRESULT downloadUpdate;
LPCSTR downloadUrl = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", File = "download.png";
string localFile = GetCurrentDirectory() + File;
downloadUpdate = URLDownloadToFileA(0, downloadUrl, localFile.c_str(), 0, 0);
switch (downloadUpdate)
{
case S_OK:
updateSuccess();
break;
case E_OUTOFMEMORY:
updateOOMError();
break;
case INET_E_DOWNLOAD_FAILURE:
updateError();
break;
default:
updateErrorUnknown();
break;
}
}
On a side note, you really shouldn't be downloading files to the same folder that your program is running from. If your program is installed in a folder like C:Program Files
or C:Program Files (x86)
, somewhere only admins can write to, then the download will be likely to fail. You should be downloading to a folder that the user has write access to, such as to a subfolder you create under %APPDATA%
for your program to use.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…