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;
}

This is my code above, and I am getting an error on this lane:

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

The compilation error it's giving me says that "+ truepath" is not possible to be used there.

I've tried .c_str() and few other ways but can't manage it to work. Any help is appreciated.

user2699298
  • 1,446
  • 3
  • 17
  • 33

2 Answers2

4

You cannot add two pointers.

The truepath is a pointer to char.

And when you say "http://localhost:8080/upload.php?name=Huh?" it returns a pointer to a char. So, you are trying to add two pointer and here is what standard says about the additive operator...

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.

Also, you have to allocate memory for the truepath variable or else it will crash.

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(), [...] 
manuell
  • 7,528
  • 5
  • 31
  • 58
  • Thanks for the example, do you mind if you add an Unicode example as well? – user2699298 Dec 19 '13 at 13:52
  • `error C2664: 'URLDownloadToFileW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'` and `1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast` – user2699298 Dec 19 '13 at 14:00
  • 1
    weird. c_str() return const wchar_t * for a std::wstring. Please double check your code. – manuell Dec 19 '13 at 14:03