Thread: SHBrowseForFolder() -- A better way?

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    SHBrowseForFolder() -- A better way?

    SHBrowseForFolder looks like it may be my only choice. Is there a better way to have the user select a directory (other than, of course, writing my own)?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    How do you define "better"?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    One line of code in place of three. Please correct me if I'm wrong, but one needs to call CoInitialize() before and CoUninitialize() after to get it to work correctly. Also, I'm kinda confused as to why there is a return value of PIDLIST_ABSOLUTE when MSDN specifies that one must pass the pointer location of at least MAX_PATH to BROWSINFO.pszDisplayName for the return folder name.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Oh screw!! It uses DLLs. . . Crap crap crap crap crap!!!!

    There _HAS_ to be a better way. . . anyone?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Every application that uses this particular sort of folder selection box uses SHBrowseForFolder - unless they use some wrapper around it. (Which might exist - search the web.)

    Regarding Co*, you call CoInitialize at the start of your application and CoUninitialize at the very end, not before and after every call to SHBrowseForFolder.

    The documentation about the whole thing IS confusing, I'll give you that.

    I have an old project that uses the function, but I won't have access to it for another week.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Oh screw!! It uses DLLs. . . Crap crap crap crap crap!!!!

    There _HAS_ to be a better way. . . anyone?
    Is that really your logic?

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I wouldn't want to disappoint you, but most (if not all) functions of the Win32 API are located in DLLs (kernel.dll, user32.dll ...)

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by anonytmouse
    sample.
    Ah, the way to explicitly say to the compiler, "Hey, I'm using these DLLs." This is my first tour in Windows programming. In Linux, when I need a runtime library, that GCC doesn't already know exists, I just -L the sucker. I didn't know how in Windows. . . which is why I said:
    Quote Originally Posted by Tonto
    Quote Originally Posted by Kennedy
    Oh screw!! It uses DLLs. . . <cut>
    Is that really your logic?
    So, yes, it _WAS_ my logic, however, now that I know how to use the DLLs it isn't so much a pain for me anymore.
    Quote Originally Posted by Desolation
    I wouldn't want to disapoint you. . .
    True, however, much unlike (as far as I have seen) Linux, there has to be some explicit coding (or is there some type of linker commands to make it look at a specific DLL) to make the compilier know that I want a specific run-time library/DLL. . .which ever way you want to look at it. On my Linux boot, I installed the library and compiler. GCC on my Linux boot knows about all my libs and will dynamically link to anything it needs based upon my #include's (except, of course, if I add a library later, then it goes back to the -L again).

    I had found a thread that stated the way to use a DLL was something along these lines:
    Code:
    int main(int argc, char* argv[])
    {
        hinstDll = LoadLibrary((LPCTSTR)"DGDLL.dll");
        if(hinstDll==NULL)
            cout<<"FAILD TO LOAD THE DLL"<<endl;
        hShellHookProc = (HOOKPROC)GetProcAddress(hinstDll,"ShellProc");
        if(hShellHookProc != NULL){
            SetWindowsHookEx(WH_SHELL, hShellHookProc, hinstDll, 0);
        }else{
            cout<<"FAILD TO CONNECT TO THE DLL FUNCTION"<<endl;
        }
    
        return 0;
    }
    Which is in C++, however, I thought that this was the way to do it. . . hence the "crap" as I had not yet found the way to go from the LoadLibrary() and the GetProcAddress() to CoInitialize() and CoUnInitialize(). And, I hoped there was a better way.

    But, looks like anonytmouse's example with the #pragma comment(lib, "shell32.lib") is a better way.

    EDIT: I assume that this pragma command tells the linker if you cannot find a function that is listed in this code, assume that it is from "shell32.lib" and check there before ditching out?

    Thanks for all your help, anonytmouse!!

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    That command links the library shell32. However, it only works under MSVC. Otherwise, you'll need to go in your project settings and then something like 'dependencies' or 'linking options'. You don't need to do all of that. I only use this method when working with plug-ins.

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Good, I'm not completely brain-fried. I noticed that in Code::Blocks, that pragma command didn't work. I've been looking (for the past hour or so) as to how to pass to gcc (or whatever linker gcc is actually calling on my machine) to use these DLLs. Would any of you be so kind as to let me in on the secret? Like I said, I've been searching for the past hour and haven't had any luck yet.

    EDIT: Ugh. . . I just found this. Is that what I have to do to get the ole32 and system32 DLLs usable in my program? . . . or, again, is there a "better" way?
    Last edited by Kennedy; 09-20-2006 at 11:44 AM.

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Okay, I'm apparently thick. . . but I don't know that understand:
    Quote Originally Posted by MinGW Website
    However, for __stdcall functions, the above method does not work. For MSVC will prefix an underscore to __stdcall functions while MinGW will not. The right way is to produce the DEF file using the pexports tool included in the mingw-utils package and filter off the first underscore by sed:

    pexports testdll.dll | sed "s/^_//" > testdll.def

    Then, when using dlltool to produce the import library, add `-U' to the command line:

    dlltool -U -d testdll.def -l libtestdll.a

    And now, you can proceed in the usual way:

    gcc -o testmain testmain.c -L. -ltestdll

    Hooray, we got it.
    Does this mean that I have to reconstruct the lib file from the DLL file then link using the new reconstructed lib file. . . THEN. . . this would create an executable that would NOT have linked in the lib file, but would rather, using the system DLL path, search the system for the approiate DLL file and call that. . .

    OR. . .

    Does this mean that now I have that section of the DLL statically linked into my code? . . . and if this is true does this stomp the rules for MS developement????

    Sorry folks, I'm not intentionally thick. . . it is just that this is my first venture into a GUI on Windows. . . I'm used to Textual GUIs and XWindows. . . I'm not used to the Win32 API/Win programming. . . yet.

  13. #13
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Found it. . . I was just being thick. . .

    Essentially, all I have to do is -lole32

    I did try it everywhich way, but that one. . . -lole32.dll doesn't work .

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Just 'FYI', you don't link to dll's (dymanic link libraries), you link to static normal libraries, object files, and import libraries (which give you access to dynamic libraries' functions).

  15. #15
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hmmmm. . . I did. . . and it worked. So, I guess the next question is: How am I supposed to link to DLLs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SHBrowseForFolder
    By UnclePunker in forum Windows Programming
    Replies: 1
    Last Post: 04-15-2005, 05:06 AM
  2. Default directory for SHBrowseForFolder
    By PJYelton in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2005, 08:49 PM
  3. multiple selection folder chooser
    By Corrington_j in forum Windows Programming
    Replies: 4
    Last Post: 02-09-2005, 10:38 PM
  4. Help setting SHBrowseForFolder(...) root please
    By cDir in forum Windows Programming
    Replies: 3
    Last Post: 02-07-2002, 03:49 PM