Common Dialog Boxes

i haz errors

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 11028 Views - Last Post: 23 February 2009 - 07:32 AM Rate Topic: -----

#1 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Common Dialog Boxes

Posted 22 February 2009 - 07: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?

Is This A Good Question/Topic? 0
  • +

Replies To: Common Dialog Boxes

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 07: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
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 07: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

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 07: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

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 07: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
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Common Dialog Boxes

Posted 22 February 2009 - 08:05 PM

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

#8 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#10 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Common Dialog Boxes

Posted 22 February 2009 - 08:11 PM

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

#11 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Common Dialog Boxes

Posted 22 February 2009 - 08: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 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2