Link to home
Start Free TrialLog in
Avatar of kabix
kabix

asked on

Browse for folder function

I found a function that should let a user to select a folder. However, trying to compile it I got following errors:
  [C++ Error] shobjidl.h(2193): E2238 Multiple declaration for 'FOLDERSETTINGS'
  [C++ Error] shobjidl.h(8095): E2238 Multiple declaration for 'DESKBANDINFO'
  [C++ Error] shlobj.h(1422): E2238 Multiple declaration for 'FVSHOWINFO'
  [C++ Error] shlobj.h(3457): E2238 Multiple declaration for 'SHELLFLAGSTATE'
  [C++ Error] Unit1.cpp(55): E2451 Undefined symbol 'Label1'
  [C++ Error] Unit1.cpp(59): E2451 Undefined symbol 'Label2'

I am using Borland C++ Builder 6. How should i fix this problem? I included the following unit1.cpp file that i was using. I noticed that when i add #include <shlobj.h> even to an empty project it shows the same errors. Any ideas how to fix it?

--------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------

#include <vcl.h>
#include <shlobj.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    BROWSEINFO    info;
    char          szDir[MAX_PATH];
    char          szDisplayName[MAX_PATH];
    LPITEMIDLIST  pidl;
    LPMALLOC      pShellMalloc;

    // SHBrowseForFolder returns a PIDL. The memory for the PIDL is
    // allocated by the shell. Eventually, we will need to free this
    // memory, so we need to get a pointer to the shell malloc COM
    // object that will free the PIDL later on.
    if(SHGetMalloc(&pShellMalloc) == NO_ERROR)
    {
        // if we were able to get the shell malloc object,
        // then proceed by initializing the BROWSEINFO stuct
        memset(&info, 0x00,sizeof(info));
        info.hwndOwner = Handle;                 // Owner window
        info.pidlRoot  = 0;                      // root folder
        info.pszDisplayName = szDisplayName;     // return display name
        info.lpszTitle = "Browse Title";         // label caption
        info.ulFlags   = BIF_RETURNONLYFSDIRS;   // config flags
        info.lpfn = 0;                           // callback function

        // execute the browsing dialog
        pidl = SHBrowseForFolder(&info);

        // pidl will be null if they cancel the browse dialog.
        // pidl will be not null when they select a folder
        if(pidl)
        {
            // try to convert the pidl to a display string
            // return is true if success
            if(SHGetPathFromIDList(pidl, szDir))
            {
                // set one caption to the directory path
                Label1->Caption = szDir;
            }

            // set another caption based on the display name
            Label2->Caption = info.pszDisplayName;

            // use the shell malloc com object to free the pidl.
            // then call Relasee to signal that we don't need
            // the shell malloc object anymore
            pShellMalloc->Free(pidl);
        }
        pShellMalloc->Release();
    }
}
//---------------------------------------------------------------------------
Avatar of Kashra
Kashra

Well, those kinds of errors could come from a header file that doesn't have flags to prevent multiple includes.

You should check the header file to see if it has an #ifndef statement at the beginning. If not, you should put the following on either end of the header file:

#ifndef INCLUDE_THIS_HEADER_ONLY_ONCE_SHLOBJ_H
#define INCLUDE_THIS_HEADER_ONLY_ONCE_SHLOBJ_H

// REST OF HEADER FILE HERE

#endif

The last two errors are obvious. You are using the variables Label1 and Label2 without ever defining them.
Avatar of kabix

ASKER

Hey,
I tried with a simplier code that uses the same file. Below I included the cpp file that i was trying to compile. In my case I wasnt able to compile because i got the errors which are below as well.
I tried to put
#ifndef INCLUDE_THIS_HEADER_ONLY_ONCE_SHLOBJ_H
#define INCLUDE_THIS_HEADER_ONLY_ONCE_SHLOBJ_H
in both of the files however c++ showed a bunch of other errors. I installed brand new version of Borland C++ Builder 6 but it still does the same. How could I fix this error?


ERRORS:
[C++ Error] shobjidl.h(2191): E2238 Multiple declaration for 'FOLDERSETTINGS'
[C++ Error] shobjidl.h(8093): E2238 Multiple declaration for 'DESKBANDINFO'
[C++ Error] shlobj.h(1422): E2238 Multiple declaration for 'FVSHOWINFO'
[C++ Error] shlobj.h(3457): E2238 Multiple declaration for 'SHELLFLAGSTATE'


UNIT1.CPP
//---------------------------------------------------------------------------

#include <vcl.h>
#include <shlobj.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ShellExecute(Handle, "open", "d:\\website\\index.htm", NULL,NULL,SW_SHOWDEFAULT);
}
//---------------------------------------------------------------------------
Did you remember to put #endif at the end of the file as well? You should only be putting those flags in the header files that are actually giving the errors (shobjidl.h and shlobj.h).

Otherwise, its difficult to tell what is wrong without seeing the code from the header files themselves (preferably the lines giving the errors, of course)
This is what I have used in my code, and it works great:

static BROWSEINFO bi;
LPITEMIDLIST         pidl;

bi.hwndOwner =  hwnd;
bi.pidlRoot  = NULL;              // This is to search entire computer (you can specify your own root)

bi.pszDisplayName = szFolderName;
pidl = SHBrowseForFolder(&bi);

if ( pidl == NULL )
     return FALSE;

SHGetPathFromIDList(pidl, szFolderPath);
Avatar of kabix

ASKER

Kashra: i added endif at the end of each file however it did not work. Any other ideas how to fix it?
I've got no other ideas at the moment, I'm sorry. The only other times I see error messages like that are when I forget to use keyword "inline" in front of my inlined functions, but if you're using a commercial header file then that's not very likely.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
Lunchy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial