473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Getting the desktop (or home) folder in linux

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

Expand|Select|Wrap|Line Numbers
  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #include <ShlObj.h>
  4. # ifndef SHGFP_TYPE_CURRENT
  5. #  define SHGFP_TYPE_CURRENT 0
  6. # endif
  7. #pragma comment( lib, "advapi32.lib" )
  8. #pragma comment( lib, "shell32.lib" )
  9. #else
  10. // LINUX
  11. #endif
  12.  
  13. #include <string>
  14. using namespace std;
  15.  
  16. wstring GetDesktop()
  17. {
  18.     wstring oFolder = L"";
  19. #ifdef _WIN32
  20.     wchar_t aFolder[MAX_PATH];
  21.     SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
  22.     oFolder = aFolder;
  23. #else
  24.     // LINUX [...]
  25. #endif
  26.     return oFolder;
  27. }
  28.  
Oct 6 '07 #1
9 12682
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 6 '07 #2
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?

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.

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.

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.).
Oct 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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.

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.
Oct 7 '07 #4
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

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.
Oct 7 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. #include <tchar.h>
  2. TCHAR aFolder[MAX_PATH];
  3. SHGetFolderPath( 0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, aFolder );
  4.  
Read this and this.

Be sure to follow the links.
Oct 8 '07 #6
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.
Oct 8 '07 #7
RRick
463 Expert 256MB
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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main( int, char **, char ** env)
  5. {
  6.     for ( ;  *env; env+=2)
  7.         cout << "VAR: " << *env << "  Value: " << *(env+1) << endl;
  8. }
  9.  
Oct 8 '07 #8
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 :-)
Oct 8 '07 #9
While you are correct that the HOME env var might be changed by the user you are incorrect for not using it. Especially on linux users tend to be more aware of how their system works and if they changed their $HOME environment variable it was likely for the better and I would advise heeding their recomendation.
Oct 27 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alex Leduc | last post by:
Is there an API function to get the user's home directory path in Java? I'd like to save a configuration file in it. For a user named "myUser", I would get /home/myUser on Linux ...
4
by: Luca T. | last post by:
Hello, i need a way to find the home folder of the current user no matter if i am in Linux or Windows for instance: * Linux: /home/username * Windows: C:\Documents and Settings\username Is...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
3
by: Lex | last post by:
Is there a way in C# to get the windows/media drive/location? I looked in the SpecialFolder enum but saw nothing helpful. Regards
1
by: John Dalberg | last post by:
I have my SQL Server installation on drive c: and the databases on another drive. My c drive got corrupted and I am planning to restore it from an old backup. This means drive c will not be up to...
6
by: bill | last post by:
I have been presented with the task of getting the last file from a linux directory when the files are of the form: nnnnnn-xxxxxxx-.ext where nnnnnn are 6 numeric digits and xxxxxx is variable...
0
by: AxOn | last post by:
Hi, Could somebody tell me how to get the letter of the home folder drive, witch is specified in AD. Thanks!
3
by: lokeshrajoria | last post by:
Hello Everyone, I am running Perl in Linux 9.0 but after writing simple hello program not getting output. despite of compile sucessfully and i set path of perl. Please do...
8
by: Okonita | last post by:
Hi all, I have gone through the process of installing DB2 UDBv9 Express-C in my local linux environment. I am used to having icon placed on my Desktop or taskbar for easy access to (db2cc)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.