C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,817 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,684 people online right now. Registration is fast and FREE... Join Now!




Common Dialog Boxes

 

Common Dialog Boxes, i haz errors

lanec42

22 Feb, 2009 - 06:35 PM
Post #1

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
I'm trying to use an open file common dialog box, but I'm getting a "|47|undefined reference to `_GetOpenFileNameA@4'|".

Here's the function GetOpenFileName is called in:
CODE
void FileOpen(HWND hwnd)
{
    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    // Display the Open dialog box.

    if (GetOpenFileName(&ofn)==TRUE)
        hf = CreateFile(ofn.lpstrFile,
                        GENERIC_READ,
                        0,
                        (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        (HANDLE) NULL);
}

(Yeah, I pretty much just copy/pasted from MSDN wink2.gif.)

I'm using Code::Blocks on Vista, and I'd be grateful if anyone who's in the same boat would try to compile the code and let me know what errors (if any) they get.

My main question is this: is there a library i'm missing? I've included windows.h, but could one of the libraries it #include s be missing?

User is offlineProfile CardPM
+Quote Post


KYA

RE: Common Dialog Boxes

22 Feb, 2009 - 06:45 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Did you see this?

QUOTE

Note Starting with Windows Vista, the Common File Dialog has been superseded by the Common Item Dialog when used to open a file. We recommended that you use the Common Item Dialog API instead of the Common File Dialog API in this case. For more information, please see Common Item Dialog.

User is online!Profile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 06:48 PM
Post #3

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
I kind of assumed it'd be backward compatible with the rest of NT...
Soo, will common item dialog work on XP?
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: Common Dialog Boxes

22 Feb, 2009 - 06:53 PM
Post #4

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,488



Thanked: 303 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
I would think that starting with Vista means that it will still work for XP. I've used the File Open Common Dialog Box from the Windows API on XP before. It wouldn't hurt to detect the Windows version in your code, that way assuring the API will work correctly.
User is offlineProfile CardPM
+Quote Post

KYA

RE: Common Dialog Boxes

22 Feb, 2009 - 06:55 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Yeah. Barebone code on XP I can compile:

CODE

#include <windows.h>
void FileOpen(HWND);

int WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    HWND hwnd;
    FileOpen(hwnd);
    return 0;
}

void FileOpen(HWND hwnd)
{
    OPENFILENAME ofn;       // common dialog box structure
    char szFile[260];       // buffer for file name
    HANDLE hf;              // file handle

    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

    // Display the Open dialog box.

    if (GetOpenFileName(&ofn)==TRUE)
        hf = CreateFile(ofn.lpstrFile,
                        GENERIC_READ,
                        0,
                        (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        (HANDLE) NULL);
}


User is online!Profile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 07:04 PM
Post #6

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
QUOTE
I would think that starting with Vista means that it will still work for XP.

I sure hope so.

I like the version checking idea, but that only means it'll decide whether or not to execute the code. Although that may be helpful, my problem is with compiling, not executing.

Thanks for the help KYA/no2. smile.gif

This post has been edited by lanec42: 22 Feb, 2009 - 07:05 PM
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: Common Dialog Boxes

22 Feb, 2009 - 07:05 PM
Post #7

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
Did you include commdlg.h?
User is offlineProfile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 07:06 PM
Post #8

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
No. Do I need to? I'll go try it.

EDIT: Doesn't help. I was under the impression that windows.h included commdlg.h, though.
MORE EDIT: I'm reading the common item dialog stuff. Once i get a good compile, i'll go try it on an XP box.

This post has been edited by lanec42: 22 Feb, 2009 - 07:09 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: Common Dialog Boxes

22 Feb, 2009 - 07:09 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
QUOTE(lanec42 @ 22 Feb, 2009 - 08:06 PM) *

I was under the impression that windows.h included commdlg.h, though.


It does. The actual info for this call is in commdlg.h, but including windows.h covers that:

QUOTE

Declared in Commdlg.h, include Windows.h


This post has been edited by KYA: 22 Feb, 2009 - 07:10 PM
User is online!Profile CardPM
+Quote Post

bsaunders

RE: Common Dialog Boxes

22 Feb, 2009 - 07:11 PM
Post #10

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
Do your linker settings include comdlg32.lib?
User is offlineProfile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 07:11 PM
Post #11

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
Schweet. Maybe I should just spend a year as a hermit windows monk, reading header files from dawn to dusk.

QUOTE
Do your linker settings include comdlg32.lib?

Good question. I'll check.
Wait, does that matter if it's included in windows.h anyway?

This post has been edited by lanec42: 22 Feb, 2009 - 07:14 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: Common Dialog Boxes

22 Feb, 2009 - 07:14 PM
Post #12

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
laugh.gif Sorry, i wasn't suggesting that. I was merely relating some information readily and easily available. Can you get the snippet I showed to compile on Vista? That's as small as you can get before adding other stuff.
User is online!Profile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 07:19 PM
Post #13

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
No, it won't compile, but I think that's cause it still uses "Common File Dialog" stuff. At least that means it (probably) wasn't just my code smile.gif
User is offlineProfile CardPM
+Quote Post

KYA

RE: Common Dialog Boxes

22 Feb, 2009 - 07:24 PM
Post #14

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,489



Thanked: 507 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
That's good, at least the problem is somewhat isolated. How 'bout this?

CODE

HRESULT SimpleInvoke(HWND hwnd)
{
    IFileDialog *pfd;
    
    // CoCreate the dialog object.
    HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog,
                                  NULL,
                                  CLSCTX_INPROC_SERVER,
                                  IID_PPV_ARG(IFileDialog, &pfd));
    
    if (SUCCEEDED(hr))
    {
        // Show the dialog
        hr = pfd->Show(hwnd);
        
        if (SUCCEEDED(hr))
        {
            // Obtain the result of the user's interaction with the dialog.
            IShellItem *psiResult;
            hr = pfd->GetResult(&psiResult);
            
            if (SUCCEEDED(hr))
            {
                // Do something with the result.
                psiResult->Release();
            }
        }
        pfd->Release();
    }
    return hr;




Why the hell did they make opening a file dialog more complicated then it needs to be? URGH

This post has been edited by KYA: 22 Feb, 2009 - 07:29 PM
User is online!Profile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

22 Feb, 2009 - 07:29 PM
Post #15

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
CODE
D:\Users\lanec42\Desktop\Balance1\file.cpp||In function `void FileOpen(HWND__*)':|
D:\Users\lanec42\Desktop\Balance1\file.cpp|8|error: `CLSID_FileOpenDialog' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|11|error: `IFileDialog' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|11|error: `pfd' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|11|error: `IID_PPV_ARG' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|21|error: `IShellItem' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|21|error: `psiResult' was not declared in this scope|
D:\Users\lanec42\Desktop\Balance1\file.cpp|21|warning: unused variable 'IShellItem'|
||=== Build finished: 7 errors, 4 warnings ===|

Still not happening.

Where was "pfd" and "psiResult" declared, anyway? mad.gif rawr m$.

EDIT: this "Minimum operating systems Windows Vista" was here. Will this be Vista-only if (ever) i get it working? That's what they seem to suggest...

This post has been edited by lanec42: 22 Feb, 2009 - 07:34 PM
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: Common Dialog Boxes

22 Feb, 2009 - 07:41 PM
Post #16

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
Are you using Dev-C++?

If so, in the Project Options dialog box (Project->Project Options, or Alt+P), make sure in the Type group that Win32 GUI is selected, and not Win32 Console.

This post has been edited by bsaunders: 22 Feb, 2009 - 08:58 PM
User is offlineProfile CardPM
+Quote Post

lanec42

RE: Common Dialog Boxes

23 Feb, 2009 - 06:32 AM
Post #17

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 229


My Contributions
I'm using Code::Blocks, but it's similar. I'll try rebuilding from scratch, maybe that'll help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/20/09 10:12PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month