Solved

Browse for folder function

Posted on 2003-11-12
9
1,443 Views
Last Modified: 2010-05-18
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();
    }
}
//---------------------------------------------------------------------------
0
Comment
Question by:kabix
9 Comments
 
LVL 3

Expert Comment

by:Kashra
ID: 9736458
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.
0
 
LVL 4

Author Comment

by:kabix
ID: 9743621
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);
}
//---------------------------------------------------------------------------
0
 
LVL 3

Expert Comment

by:Kashra
ID: 9743843
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)
0
 
LVL 1

Expert Comment

by:georgiek50
ID: 9798301
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);
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:kabix
ID: 9798471
Kashra: i added endif at the end of each file however it did not work. Any other ideas how to fix it?
0
 
LVL 3

Expert Comment

by:Kashra
ID: 9799212
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.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249078
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
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
ID: 10284510
PAQed, with points refunded (50)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now