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.
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
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>).
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.
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetKnownFolderPath
usingnamespace 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;
}