Getting the desktop (or home) folder in linux | Newbie | | Join Date: Oct 2007
Posts: 9
| |
Hi All, I use the following code to get the desktop folder in windows but I couldn't find any informations about doing the same thing under linux.
Any ideas? Thanks for your help,
Michele -
#ifdef _WIN32
-
#include <windows.h>
-
#include <ShlObj.h>
-
# ifndef SHGFP_TYPE_CURRENT
-
# define SHGFP_TYPE_CURRENT 0
-
# endif
-
#pragma comment( lib, "advapi32.lib" )
-
#pragma comment( lib, "shell32.lib" )
-
#else
-
// LINUX
-
#endif
-
-
#include <string>
-
using namespace std;
-
-
wstring GetDesktop()
-
{
-
wstring oFolder = L"";
-
#ifdef _WIN32
-
wchar_t aFolder[MAX_PATH];
-
SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
-
oFolder = aFolder;
-
#else
-
// LINUX [...]
-
#endif
-
return oFolder;
-
}
-
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,382
| | | re: Getting the desktop (or home) folder in linux
None of your Windows code will work in Linux.
It's hard enough to make ti work with different versions of Windows let alone a completely different operating system.
You will need to have two separate programs. One for Windows and one for Linux.
| | Newbie | | Join Date: Oct 2007
Posts: 9
| | | re: Getting the desktop (or home) folder in linux
I'm simply looking for a c/c++ way to get the user desktop folder, which under windows is "Drive:\Documents and settings\username\Desktop" and under linux is "/home/username/Desktop".
Any ideas?
The fact is that finding information on the web about win32 developement is easy as there is a lot of well written, organized and easy to find documentation websites (msdn, gamedev, thescripts, flipcode are just a few examples).
For linux ... honestly I don't know, but it seems really hard to find any decent resources. Can you give me some directions? Quote:
Originally Posted by weaknessforcats None of your Windows code will work in Linux. Indeed. See the preprocessor #ifdef structure? the linux part is empty, sorry, I thought it was clear. Quote:
Originally Posted by weaknessforcats It's hard enough to make ti work with different versions of Windows let alone a completely different operating system. Not sure what you mean ... the windows sdk supports all the windows versions I need (xp, 2k, vista), both on x86 and x64. Quote:
Originally Posted by weaknessforcats You will need to have two separate programs. One for Windows and one for Linux. The software I'm working on runs fine under xp, x64, linux x86 (gentoo/sabayon).
Never heard of crossplatform apps? Same program with some different code sections to achieve the same functionality using platform specific code.
The project only needs to be recompiled on the different platforms against their dependencies (which are crossplatform too, like boost, various opengl libraries, etc.).
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,382
| | | re: Getting the desktop (or home) folder in linux Quote:
Originally Posted by motionblur Quote:
Originally Posted by weaknessforcats
It's hard enough to make ti work with different versions of Windows let alone a completely different operating system.
Not sure what you mean ... the windows sdk supports all the windows versions I need (xp, 2k, vista), both on x86 and x64. 64-bit libraries do not work on 32-bit systems.
True, the windws sdk supports different versions but there are different libraries with the same functions rewritten for the os.
Now you might be able to run 32-bit app on a 64-bit machine but when you compile a 64-bit application you will need 64-bit libraries. Quote:
Originally Posted by motionblur Never heard of crossplatform apps? Same program with some different code sections to achieve the same functionality using platform specific code. Yep. Been there done that. I just never had 50,000 lines of Windows code followed by 50,000 lines of Motif, followed by etc...
I used separate code repositories. I don't like large ripple when changes are needed. BTW: The #ifdef is really dangerous. Has to be unique.
| | Newbie | | Join Date: Oct 2007
Posts: 9
| | | re: Getting the desktop (or home) folder in linux
Man, not trying to be rude or anything, but I don't understand why you're trying to explain me the obvious, like the fact that I need to recompile a 32 bit app using the x64 libs of the sdk to get a real x64 app. It's what I already do.
I've got a bunch of config scripts that set the proper search directories so that the VC++ EE loads the right compiler, includes, libs from the sdk depending on what I need, and different configurations like debug x86, release x86, debug x64, release x64, release LINUX x86, etc. with the different options already set.
Also this app is actually a plugin for a 3d animation software, so most of the things that I do are platform / system agnostic, as it's mostly based on the app sdk. The plugin itself wouldn't load in the x64 version of the software, as 32 bit and 64 bit libraries can't be mixed within the same x64 app.
Back to the original question, I almost solved the issue.
This is how I get the path the home folder in linux:
string oFolder_user;
passwd *pw = getpwuid(getuid());
if (pw)
oFolder_user = pw->pw_dir;
Still ... I don't know what happens when there are japanese or chinese characters in the username ... is there a wstring version of this function?
In windows it's much simpler ... like:
wchar_t aFolder[MAX_PATH];
SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
Any ideas?
Thanks a lot,
Michele Quote:
Originally Posted by weaknessforcats 64-bit libraries do not work on 32-bit systems.
True, the windws sdk supports different versions but there are different libraries with the same functions rewritten for the os. | | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,382
| | | re: Getting the desktop (or home) folder in linux Quote:
Originally Posted by motionblur Still ... I don't know what happens when there are japanese or chinese characters in the username ... is there a wstring version of this function?
In windows it's much simpler ... like:
wchar_t aFolder[MAX_PATH];
SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
Any ideas? When Windows supports Unicode the strings are wchar_t arrays containinf Unicode characters. When Windows supports ASCII, the strings are arrays of char. Typically, you can't tell until compile time whether your code conrtains Unicode are ASCII. In Visual Studio, there is a project property you set so you get the correct support at compile time.
That means you can't code using wchar_t.
Instead, Microsoft provides the TCHAR mappings in a header tchar.h.
The aFolder argument is a LPTSTR. That is, a pointer to a TCHAR string. This will be a wchar_t string with Unicode or an ASCII string.
You have to use these mappings: -
#include <tchar.h>
-
TCHAR aFolder[MAX_PATH];
-
SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
-
Read this and this.
Be sure to follow the links.
| | Newbie | | Join Date: Oct 2007
Posts: 9
| | | re: Getting the desktop (or home) folder in linux
Argh. Man, it's ok. Don't worry. I don't know why you keep trying to give ultra-basic explainations like you were talking to a complete newbie.
wchar_t*s / wstrings are supported everywhere on the versions of win I need to use. And yes, _UNICODE and UNICODE defines are set. And no, I don't need to use TCHAR, as I need exactly wstrings and wchar_t.
Thanks for your efforts and your time, but if you read again the topic of this thread you'll notice it has nothing to do with your answers, no idea why you keep giving me useless information that I already know. The windows part was done since my first post, and it works correctly.
|  | Expert | | Join Date: Feb 2007
Posts: 430
| | | re: Getting the desktop (or home) folder in linux Quote:
Originally Posted by motionblur I'm simply looking for a c/c++ way to get the user desktop folder, which under windows is "Drive:\Documents and settings\username\Desktop" and under linux is "/home/username/Desktop". In linux, some system level information is passed via the system variables. I couldn't find any with the desktop in them, but HOME does have the path to the user's root directory. From that you can get to the Desktop directory.
The system variables are passed as pairs to the third parameter to the main routine. It is usually defined as char ** env. The following will print out all of the system variables passed to a program -
#include <iostream>
-
using namespace std;
-
-
int main( int, char **, char ** env)
-
{
-
for ( ; *env; env+=2)
-
cout << "VAR: " << *env << " Value: " << *(env+1) << endl;
-
}
-
| | Newbie | | Join Date: Oct 2007
Posts: 9
| | | re: Getting the desktop (or home) folder in linux
Hi RRick thanks for the answer.
I've found two alternative solutions to this problem, like getenv("HOME") or
string oFolder_user; passwd *pw = getpwuid(getuid()); if (pw) oFolder_user = pw->pw_dir;
The last one looks better to me as I guess that in linux the HOME env var could be changed by the user (not completely sure though).
My concern now is ... what happens when the HOME folder contains unicode characters that are non-western and non-european, like chinese, japanese, etc.
I mean ... is the path UTF-8 encoded? so I can convert it to wstring using mbstowcs() using the current locale?
Thanks :-)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,546 network members.
|