• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# How to open directories (similar to GetOpenFileName) ??

## Recommended Posts

Hi all, I''m looking for something similar to GetOpenFileName(), but instead of choosing a file I want to choose a directory. While browsing msdn I found this function : SHBrowseForFolder(). But this doesn''t work (Probably it has something to do with the "SH" in front of it...i guess I have to include something, but I don''t know what though.) Thanks !

##### Share on other sites
Try including Shlobj.h.

##### Share on other sites
Look here:

Clickage

Be sure to read the remarks, you have to intialize COM.

[edited by - CodeMunkie on August 5, 2003 3:37:40 PM]

##### Share on other sites
Thanks !! But I still have a problem initializing the COM library.
They say I have to use the function CoInitializeEx() to do this. I''ve included the necessary header(objbase.h) but I get the error : "undeclared identifier". :s

This is what I found in msdn :

Import Library: Included as a resource in ole32.dll.

I have checked the files and they are all included...

Anything else i''m forgetting ??

##### Share on other sites
It says you can just use CoIntialize since it automatically uses apartment threading. What error are you getting? Are you linking to ole32.dll?

##### Share on other sites
I double checked it : the program is linked to Ole32.lib

This is the error I get : e:\programmeren\filesync\filesync.cpp(305) : error C2065: 'CoInitializeEx' : undeclared identifier

[edited by - BertVanSemmertier on August 6, 2003 1:20:39 PM]

edit : I tried CoInitialize() and this works !!
But at msdn they say you really should use CoInitializeEx() in new applications. Besides I HAVE to use CoInitializeEx() before I can use SHBrowseForFolder().

[edited by - BertVanSemmertier on August 6, 2003 1:24:43 PM]

##### Share on other sites
You have to define something before you include your headers - let me dig up my code...

Here it is:
#include <ObjBase.h>#include <COMDef.h>#include <COMUtil.h>//...//If this is undefined, put "#define _WIN32_WINNT 0x0500" before the include ObjBase//alternatively, you can use  #define _WIN32_WINNT 0x0501 for XP// at the top of your StdAfx.h file - before *anything* elseif(FAILED(hr=CoInitializeEx(NULL, COINIT_MULTITHREADED)))	_com_issue_error(hr);

You might not want the MTA though - you probably want the STA (the default).

PS You can most likley use Coinitialize with SHBrowseForFolder and it'll work just fine.

[edited by - Magmai Kai Holmlor on August 6, 2003 8:59:26 PM]

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor

//If this is undefined, put "#define _WIN32_WINNT 0x0500"

You might not want the MTA though - you probably want the STA (the default).

Great !! This did the trick !! Where did you find this info ?? It isn''t mentioned in the msdn library

quote:
Original post by Magmai Kai Holmlor
You might not want the MTA though - you probably want the STA (the default).

What do you mean ??

Thanks again !!

##### Share on other sites
Ok, one last problem (i hope )

Everything works fine now !! But I''d like to get the full path of the selected folder. The only thing that is saved in "pszDisplayName" is the name of the selected folder (without the path).
I tried to use GetFullPathName() but that doesn''t work...

Any ideas ??

thanks again...

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978696

• 21
• 14
• 12
• 10
• 12