Win32 resource problem

Started by
7 comments, last by Bonehed316 18 years, 6 months ago
I'm trying to write a function that pops up a custom dialog box. I want this function to be defined in a .lib. I added a dialog box resource to the .lib and created the relavent function, but my call to DialogBox() fails. The Win32 error I get is: The specified image file did not contain a resource section This error is definitely caused by the dialog function as I set the last error to ERROR_SUCCESS before calling it. When I compile the library I get the following warning: resources.res : warning LNK4221: no public symbols found; archive member will be inaccessible Finally, my DialogBox() call looks like this:
DialogBox(NULL,MAKEINTRESOURCE(IDD_ASSERT_DIALOG),NULL,assert_box_func);
This is Win32 API with MSVC2003 - not MFC. Any ideas as to how to solve my problem? [Edited by - Nitage on October 1, 2005 2:53:46 AM]
Advertisement
I wrote a wrapper for this, its fun. Unfortunately I dont have the source with me (at work). However, I do have MSDN, and I would be willing to bet money that your problem is in the first parameter of your DialogBox( call. That NULL should be the "Handle to the module whose executable file contains the dialog box template. " In otherwords, your dll, or wherever the .res file is compiled in to. You can get it via GetModuleHandle("MyCool.dll"), I believe. NULL is only acceptable if the resource is in the same module as the call, I believe.
I thought that parameter could be the problem - the thing is I'm not using a DLL - I'm using a static library.


My thought are that the resource simply isn't being put into the library by the compiler - warning LNK4221 seems to indicate that.

I've had a look around the IDE and MSDN, but I can't find any infomation about how to get the resource exported.



Maybe it cant be done wit a lib, then. Or maybe its not detecting the dependancy, and thus not importing. Not sure what to do about that. Check compiler options, maybe.
Are resources bound into .lib files? You may not be able to use that approach.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
You can't put resources in a .lib. You'll either have to create all the dialog controls with code or figure out the dark & mysterious world that is DialogBoxIndirect.
-Mike
Thanks for the replies so far.

I had a look at the MSDN and the example code for DialogBoxIndirect is interesting to say the least. I'm more of a high level standard library object oriented kind of programmer. I couldn't make head nor tail of it.

Does anyone know of any resource that explains how to create a dialog template in memory rather than just giving an example? Honestly, the MSDN example could be entered into the obfuscated c contest no problem.

Here's a quick snippet of it:

    //-----------------------    // Define an OK button.    //-----------------------    lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary    lpdit = (LPDLGITEMTEMPLATE) lpw;    lpdit->x  = 10; lpdit->y  = 70;    lpdit->cx = 80; lpdit->cy = 20;    lpdit->id = IDOK;  // OK button identifier    lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;    lpw = (LPWORD) (lpdit + 1);    *lpw++ = 0xFFFF;    *lpw++ = 0x0080;    // button class    lpwsz = (LPWSTR) lpw;    nchar = 1+MultiByteToWideChar (CP_ACP, 0, "OK", -1, lpwsz, 50);    lpw   += nchar;    lpw = lpwAlign (lpw); // align creation data on DWORD boundary    *lpw++ = 0;           // no creation data



If there isn't any good explanation of how to use DialogBoxIndirect, I might switch instead to a .dll
I'm currently writing a wrapper for creating a dialog box template.

This is the least fun I've ever had programming, including programming at work :(

I'll be very glad when I've finished this.

EDIT:
I gave up on DialogBoxIndirect. I discovered that you can directly link the .res produced by the resource compiler into the main exectutable. Much less painful.

[Edited by - Nitage on October 1, 2005 8:24:27 AM]
At home now, heres my wrapper class, there isnt anything fancy about it at all.

class WindowsDialog{private:	WindowsDialog();	DLGPROC DialogFunc;	HINSTANCE Instance;	HWND Parent;public:	WindowsDialog(WindowsDialog& other)	{		DialogFunc = other.DialogFunc;		Instance = other.Instance;		Parent = other.Parent;	}	WindowsDialog(HINSTANCE hInst, INT Template, HWND hWndParent, DLGPROC DlgFunc)	{		DialogBox(hInst, (LPCTSTR)MAKEINTRESOURCE(Template), hWndParent, DlgFunc);	}	virtual ~WindowsDialog()	{	}};


There is pretty much no error checking, but I only use this once, lol. I call it like so:

WindowsDialog dlg(GetModuleHandle("My.dll"),IDD_DLG_ERROR,NULL,DlgProc);

Where GetModuleHandle returns the HINSTANCE, IDD_DLG_ERROR is the resource of the dialog box, NULL for parent, and DlgProc is the callback used to handle the dialog events:

BOOL CALLBACK DlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)

In this case, "My.dll" is staticly linked.

This topic is closed to new replies.

Advertisement