wxMSW - My documents - folder

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
renaud.s
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Feb 22, 2006 9:23 pm
Location: Belgium
Contact:

wxMSW - My documents - folder

Post by renaud.s » Thu Mar 02, 2006 3:53 pm

Hello,

I'm using wxFileDialog to save a file. I would like the default directory point to: C:\Documents and Settings\Renaud\My documents or whatever is the default 'My documents' folder for the current user.

At first I though to use wxGetHomeDir() but the problem is that this return the following path: C:\Documents and Settings\Renaud\
so I miss the 'My documents' part.

The problem is that this folder 'name' change depending of the language of the OS you use.... so it would not work to simply add 'My Documents' to the path string :-(

I guess the 'name' of the folder is written somewhere in the windows registry... I'm not experienced enough to know how to access it...

Is there any wx standard way to do it or to directly get a path to this user folder ? This would be something nice to implement in wxWidgets if it doesn't exist yet....

Any suggestion ?

Thanks

stephan
In need of some credit
In need of some credit
Posts: 8
Joined: Wed Aug 17, 2005 2:20 pm

Post by stephan » Thu Mar 02, 2006 4:35 pm

hi,

use "SHGetSpecialFolderPath" with the correct CSIDL to get the directory. of course this is windows only code.

Code: Select all

char mydocuments[MAX_PATH];    
SHGetSpecialFolderPath( GetDesktopWindow(), mydocuments, CSIDL_PERSONAL,FALSE );

greets,
- stephan.

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Thu Mar 02, 2006 7:16 pm

Although it doesn't work for getting "My Documents" it may be of interest to know that there is wxStandardPaths for retreiving several other important paths in a cross platform way.

renaud.s
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Feb 22, 2006 9:23 pm
Location: Belgium
Contact:

unicode / ainsi

Post by renaud.s » Fri Mar 03, 2006 10:24 am

At first, thanks for your help again.

I derive a bit from your propoasal but it least they put me on the road...
So the following code is working:

...

#ifdef __WXMSW__
#include <shlobj.h>
#endif

...

wxString defaultPath = wxGetHomeDir();

#ifdef __WXMSW__
wchar_t mydocuments[MAX_PATH];

if (! SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, mydocuments))
{
defaultPath = mydocuments;
}
#endif

...


I'm using wxWidgets 3.6.1 UNICODE build.

Now a last question... I guess this code will absolutelly not work with a NON-UNICODE build ? So I have to put some #ifdef UNICODE statment to test what build I have and then use:
char mydocuments[MAX_PATH]
instead of
wchar_t mydocuments[MAX_PATH];
is it correct ?

Thanks

chris
I live to help wx-kind
I live to help wx-kind
Posts: 150
Joined: Fri Oct 08, 2004 2:05 pm
Location: Europe

Post by chris » Fri Mar 03, 2006 10:45 am

Hi,

use the generic type wxChar for that. It defaults to char in ANSI and wchar_t in Unicode builds.

I.e.

Code: Select all

wxChar mydocuments[MAX_PATH];
should do the trick.

Chris
this->signature=NULL;

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Fri Mar 03, 2006 5:45 pm

To add a little to this solution, I figure I might as well post the code I use as it is tiny bit different also a bit cleaner:

Code: Select all

wxString dir;
HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, wxStringBuffer(dir, MAX_PATH));
if(SUCCEEDED(result)) // or FAILED(result)
{
	// Do something
}
You use 0 as the 4th argument rather than SHGFP_TYPE_CURRENT. They equate to the same thing but SHGFP_TYPE_CURRENT actually says what you are requesting which might save some documentation lookup headaches in the future. Also, using wxStringBuffer lets you forgo creating your own temporary char/wchar_t/wxChar buffer which makes the code much more clean in my opinion.

renaud.s
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Feb 22, 2006 9:23 pm
Location: Belgium
Contact:

Post by renaud.s » Fri Mar 03, 2006 8:16 pm

Sorry, a new questiona again...

Ok, I use wxStringBuffer...
Just I have a problem with SHGFP_TYPE_CURRENT, the compiler complain that he don't find this identifier... ok, it's in shlobj.h which included in my file, but this enum is placed between #ifdef (_WIN32_IE >= 0x0500)... #endif.
So I guess I have ti define this symbol in my code too... just where is the best place ? In every file where I need low level API ?

Many thanks again, it's great that experienced programmer spend a bit of their time to help beginners... I'll try to do the same when I'll be a bit more clever.... :-)

Renaud

renaud.s
Knows some wx things
Knows some wx things
Posts: 43
Joined: Wed Feb 22, 2006 9:23 pm
Location: Belgium
Contact:

_WIN32_IE >= 0x0500

Post by renaud.s » Fri Mar 03, 2006 8:40 pm

I did a stupid mistakes in my previous message...
it's not:
#ifdef (_WIN32_IE >= 0x0500)
but
#if (_WIN32_IE >= 0x0500)

And my question becomes...
How, where should I set _WIN32_IE ? I'm using WinXP and Win2k...

Thanks

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Fri Mar 03, 2006 11:51 pm

wxWidgets seems to get it defined for me by itself. Are you placing the include after the inclusion of <wx/wx.h> (you should be)? Just to be thorough, here is a list of my project wide preprocessor defines: WIN32;_DEBUG;_CONSOLE;__WXMSW__;__WXDEBUG__

Post Reply