School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,872 people online right now. Registration is fast and FREE... Join Now!



Common Dialog Boxes

Common Dialog Boxes i haz errors Rate Topic: -----

#1 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 06:35 PM

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:
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 ;).)

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?
Was This Post Helpful? 0
  • +
  • -


#2 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 22 February 2009 - 06:45 PM

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.

Was This Post Helpful? 0
  • +
  • -

#3 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 06:48 PM

I kind of assumed it'd be backward compatible with the rest of NT...
Soo, will common item dialog work on XP?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • PHoToN PoWeR PaCK : not included
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 15,043
  • Joined: 10-May 07


Dream Kudos: 2875

Expert In: Goofing Off

Posted 22 February 2009 - 06:53 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 22 February 2009 - 06:55 PM

Yeah. Barebone code on XP I can compile:

#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);
}


Was This Post Helpful? 0
  • +
  • -

#6 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 07:04 PM

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. :)

This post has been edited by lanec42: 22 February 2009 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict
  • PipPipPipPip
  • Group: Members
  • Posts: 571
  • Joined: 18-January 09


Dream Kudos: 0

Posted 22 February 2009 - 07:05 PM

Did you include commdlg.h?
Was This Post Helpful? 0
  • +
  • -

#8 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 07:06 PM

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 February 2009 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 22 February 2009 - 07:09 PM

View Postlanec42, on 22 Feb, 2009 - 08:06 PM, said:

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 February 2009 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#10 bsaunders  Icon User is offline

  • D.I.C Addict
  • PipPipPipPip
  • Group: Members
  • Posts: 571
  • Joined: 18-January 09


Dream Kudos: 0

Posted 22 February 2009 - 07:11 PM

Do your linker settings include comdlg32.lib?
Was This Post Helpful? 0
  • +
  • -

#11 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 07:11 PM

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 February 2009 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 22 February 2009 - 07:14 PM

:lol: 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.
Was This Post Helpful? 0
  • +
  • -

#13 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 07:19 PM

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 :)
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • while(sad){!sad; awesome();}
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 13,183
  • Joined: 14-September 07


Dream Kudos: 3000

Expert In: C, C++, Java

Posted 22 February 2009 - 07:24 PM

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

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 February 2009 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 22 February 2009 - 07:29 PM

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? :angry: 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 February 2009 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#16 bsaunders  Icon User is offline

  • D.I.C Addict
  • PipPipPipPip
  • Group: Members
  • Posts: 571
  • Joined: 18-January 09


Dream Kudos: 0

Posted 22 February 2009 - 07:41 PM

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 February 2009 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#17 lanec42  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Members w/DIC++
  • Posts: 229
  • Joined: 25-March 08


Dream Kudos: 0

Posted 23 February 2009 - 06:32 AM

I'm using Code::Blocks, but it's similar. I'll try rebuilding from scratch, maybe that'll help.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month