• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

C++ get APPDATA folder path?

Elite Diviner
Loyal Member
Joined
Sep 25, 2008
Messages
457
Reaction score
37
How would I get the appdata path and return it to a string so that I could use it while trying to download a file using the code below

Code:
HRESULT hr = URLDownloadToFile ( NULL, _T("http://www.codeguru.com/forum/showthread.php?threadid=463496"), _T("I want to use the appdata path string here"), 0, NULL );

Any help is appreciated.
 
Ginger by design.
Loyal Member
Joined
Feb 15, 2007
Messages
2,340
Reaction score
653


Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv){
	char* appdata = getenv("APPDATA");
	printf("Appdata: %s\n",appdata);
	return 0;
}
 
Elite Diviner
Loyal Member
Joined
Sep 25, 2008
Messages
457
Reaction score
37
I need it to be the path aswell as a file name at the end so like

appdata + "\test.html", but i get

1>c:\documents and settings\mike\my documents\visual studio 2010\projects\mydlll\mydlll\maindll.cpp(33): error C2110: '+' : cannot add two pointers
1>c:\documents and settings\mike\my documents\visual studio 2010\projects\mydlll\mydlll\maindll.cpp(34): error C2065: 'Lappdata' : undeclared identifier

when i try to compile
 
Junior Spellweaver
Joined
Apr 16, 2009
Messages
115
Reaction score
22
Code:
char* appdata = getenv("APPDATA");
strcat(appdata, "\\test.html")
printf("Appdata: %s\n",appdata);

EDIT: oops
 
Last edited:
Elite Diviner
Loyal Member
Joined
Sep 25, 2008
Messages
457
Reaction score
37
Create a new string with the char*. The string class has the + operator you're looking for.

I end up getting an error that LAPPDATA is undeclared or something (Appdata = the string).
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
No way to help you there not knowing what your LAPPDATA is. Try declaring it. :D

Be more specific in what exactly you tried and what exactly the error is.
 
Elite Diviner
Loyal Member
Joined
Sep 25, 2008
Messages
457
Reaction score
37
Code:
char* appdata = getenv("APPDATA");
strcat(appdata, "\\test.html");
    HRESULT hr = URLDownloadToFile ( NULL, _T("http://www.codeguru.com/forum/showthread.php?threadid=463496"), _T([COLOR="Red"]appdata[/COLOR]), 0, NULL );

and it errors up saying

Code:
1>c:\documents and settings\mike\my documents\visual studio 2010\projects\mydlll\mydlll\maindll.cpp(37): error C2065: 'Lappdata' : undeclared identifier
 
Experienced Elementalist
Joined
Apr 12, 2009
Messages
241
Reaction score
32
Here's what I use...

Code:
CoInitialize(0);

wchar_t AppDataFolder[MAX_PATH];
SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, AppDataFolder);
wcscat_s(AppDataFolder, L"\\test.html");

If you don't have a good C++ compiler like vs2010, you might not have that particular wcscat_s override - change it to wcscat_s(AppDatafolder, MAX_PATH, L"\\test.html");
 
Back
Top