1
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <iostream>
#include <UrlMon.h>
#include <cstring>
#pragma comment(lib, "UrlMon.lib")
using namespace std;

int main()
{
char* appdata = getenv("APPDATA"); //Gets %Appdata% path
char* truepath = strcat(appdata, "\\USR\\file.dat"); // file location

cout << truepath ;
HRESULT hr = URLDownloadToFile(NULL, _T("http://localhost:8080/upload.php?name=Huh?" + truepath), _T("d"), 0, NULL);
cin.get();
return 0;
}

这是我上面的代码,我在这条车道上遇到错误:

HRESULT hr = URLDownloadToFile(NULL, _T("http://localhost:8080/upload.php?name=Huh?" + truepath), _T("d"), 0, NULL);

它给我的编译错误说“+ truepath”不可能在那里使用。

我尝试了 .c_str() 和其他几种方法,但无法使其正常工作。任何帮助表示赞赏。

4

2 回答 2

4

您不能添加两个指针。

truepath是指向 char 的指针。

当你说它"http://localhost:8080/upload.php?name=Huh?"返回一个指向 a 的指针时char。因此,您正在尝试添加两个指针,这就是标准对加法运算符的描述...

5.7
For addition, either both operands shall have arithmetic or unscoped enumeration 
type, or one operand shall be a pointer to a completely-defined object 
type and the other shall have integral or unscoped enumeration type.

此外,您必须为truepath变量分配内存,否则它将崩溃。

于 2013-12-19T12:28:57.637 回答
4
// ANSI Build
std::string appdata( getenv("APPDATA") ); 
appdata += "\\USR\\file.dat";
std::string url( "http://localhost:8080/upload.php?name=Huh?" );
url += appdata;
URLDownloadToFile( NULL, url.c_str(), [...] 

// UNICODE Build
std::wstring appdata( _wgetenv( L"APPDATA" ) ); 
appdata += L"\\USR\\file.dat";
std::wstring url( L"http://localhost:8080/upload.php?name=Huh?" );
url += appdata;
URLDownloadToFile( NULL, url.c_str(), [...] 
于 2013-12-19T13:25:19.300 回答