Assistence needed to set a path automatically to the User's desktop

What the title says, how do you create a path to the user's desktop automatically? If it would help, I'm using the program Code::Blocks, and I'm testing out a few ideas. I could just have the user enter the path directly using a string, but a few errors could occur if the user doesn't know how to find the path on his system and so forth.

So can anyone give me a few ideas? Already tried to google it but I have simply no idea how to start this project, so I would like some help.

I've already found
 
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


but getting an error saying
C:\Users\root_2\Desktop\Debate\main.cpp||In function 'int main()':|
C:\Users\root_2\Desktop\Debate\main.cpp|22|error: 'Environment' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|


My exact code (Without my other pre compiler directives) so far is
1
2
3
4
5
6
7
8
9
#include <windows.h>
using namespace std;
int main(  )
{
    cout<<"Hello";
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


}
Last edited on
Where do you declare the Environment object? from what i can infer you dont have an environment object declare or you're missing a header (ex <windows.h>).
I added the exact code I have right now, and how could I declare it then?
Environment.GetFolderPath is .Net stuff. You can only use that from managed C++ (C++/CLI)

The corresponding Win32 call, which is accessible from regular C++, is either SHGetFolderPath (if you need to support Windows 2000) or SHGetKnownFolderPath.

While SHGetFolderPath is old fashioned, it might be easier to use as it (a) has an Ansi (char*) version as well as a Unicode (wchar_t*) version, and (b) uses a caller-provided buffer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetFolderPath
using namespace std;

int _tmain() {
    TCHAR path[_MAX_PATH] = _T(""); // must be _MAX_PATH in size

    HRESULT hr = SHGetFolderPath( NULL, // no parent window
                                  CSIDL_DESKTOP,
                                  0,    // no flags
                                  NULL, // no tokens
                                  path );

    if(SUCCEEDED(hr)) {
#ifdef _UNICODE
        wcout << L"Desktop path = " << path << endl;
#else
        cout << "Desktop path = " << path << endl;
#endif
    }

    return 0;
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetKnownFolderPath
using namespace std;

int _tmain() {
    PWSTR pPath = NULL; // this is always a wchar_t*

    // There is no Ansi (char*) version of this function so the TCHAR trick
    // doesn't work here
    HRESULT hr = SHGetKnownFolderPath( FOLDERID_Desktop,
                                       0,    // no flags
                                       NULL, // no tokens
                                       &pPath ); // note we're passing the address here

    if(SUCCEEDED(hr)) {
        wcout << L"Desktop path = " << pPath << endl;
        CoTaskMemFree(pPath); // free memory with correct deallocator function
    }

    return 0;
}
Last edited on
Thanks for the help, and hopefully it will work.
Topic archived. No new replies allowed.