Question Anybody here familiar with Win32?

BruceJohnJennerLawso

Dread Lord of the Idiots
Addon Developer
Joined
Apr 14, 2012
Messages
2,585
Reaction score
0
Points
36
Hello everybody,

Ive been working through forgers Win32 tutorial, mostly with lots of success. I recently hit a roadblock in getting the code to work that appears to be some sort of a configuration problem, probably due to the tutorial being written a decade ago. I posted the issue on cplusplus.com, but no help yet, I was wondering if anyone could take a look at it for me?

http://www.cplusplus.com/forum/windows/114042/

The code should work fine in theory, as it was included as an example with the tutorial, but I get createdialog() returning null every time.

Any help would be greatly appreciated.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,217
Reaction score
1,563
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
The reason the GetLastError() call is returning 0 (ERROR_SUCCESS) is because the original value is being replaced by the MessageBox(...) call:

Code:
g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR), hwnd, ToolDlgProc);
if(g_hToolbar != NULL)
{
	ShowWindow(g_hToolbar, SW_SHOW);
}
else
{
	//int n = GetLastError();   
	[COLOR="Red"]MessageBox(hwnd, "CreateDialog returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);   // <<<<<<<< this sets GetLastError() to zero (ERROR_SUCCESS)[/COLOR]
	string nstring;
	std::ostringstream stream;
	[COLOR="red"]stream << GetLastError();  // <<<< This returns zero because the MessageBox call succeeded.[/COLOR]
	nstring = stream.str();
	MessageBox(hwnd, nstring.c_str() , "Warning!", MB_OK | MB_ICONINFORMATION);
}

You need to call GetLastErrror() right away after the failed call before making any more Win32 API calls (including MessageBox), so what you want is something like this:

Code:
g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TOOLBAR), hwnd, ToolDlgProc);
if(g_hToolbar != NULL)
{
    ShowWindow(g_hToolbar, SW_SHOW);
}
else
{
    [COLOR="red"]const DWORD dwError = GetLastError();  // <<<< immediately saves GetLastError() so we don't lose its value
    char temp[256];  // temp buffer to hold our message string (although it would be better to just inspect dwError in the debugger here)
    sprintf(temp, "CreateDialog returned NULL: GetLastError=%u", dwError);  // <<< build temporary message string
    MessageBox(hwnd, temp, "Warning!", MB_OK | MB_ICONEXCLAMATION);[/COLOR]
}

One more note -- in the long run it is easier and faster to debug code using the debugger instead of displaying messageboxes, etc. That way you can just set a breakpoint after the GetLastError() call and inspect the dwError variable (and other variables) directly in the debugger.
 

Enjo

Mostly harmless
Addon Developer
Tutorial Publisher
Donator
Joined
Nov 25, 2007
Messages
1,665
Reaction score
13
Points
38
Location
Germany
Website
www.enderspace.de
Preferred Pronouns
Can't you smell my T levels?
Win32 API is sooo C... If you want to get serious about GUI coding, then I recommend QT (used commercially, sometimes in OSS) and wxWidgets (used mostly in OSS, sometimes commercially). I, personally, am a wxWidgets pr0. However if I could take the choice again between the two libs, then I'd choose QT, because of its commercial recognition. In my younger times though, the QT's license was unclear. It was GPL or commercial, now it's LGPL or commercial.
 
Last edited:

BruceJohnJennerLawso

Dread Lord of the Idiots
Addon Developer
Joined
Apr 14, 2012
Messages
2,585
Reaction score
0
Points
36
Win32 API is sooo C... If you want to get serious about GUI coding, then I recommend QT (used commercially, sometimes in OSS) and wxWidgets (used mostly in OSS, sometimes commercially). I, personally, am a wxWidgets pr0. However if I could take the choice again between the two libs, then I'd choose QT, because of its commercial recognition. In my younger times though, the QT's license was unclear. It was GPL or commercial, now it's LGPL or commercial.

hmmm, yeah you may have a point there.

I would like to develop marketable skills in programming, so I guess I might go with QT.

Can you explain a bit more of that last part for me...? I'm lost a bit.

Whats OSS? (other than the Office of Strategic Services :lol:). And what do you mean by the license of it? Must I pay for in order to be able to create nice GUIs?

Edit: Tried that fix DBeachy suggested at the top, Im getting a consistent error code of 1813. Not sure how to figure out what that means.
 
Last edited:

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,217
Reaction score
1,563
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
According to here Windows error 1813 is:

ERROR_RESOURCE_TYPE_NOT_FOUND

1813 (0x715)
The specified resource type cannot be found in the image file.

Are you sure your resources are linked into your DLL? That errorcode means that the resource with the ID IDD_TOOLBAR could not be found in the DLL that made the CreateDialog call -- the DLL handle returned by GetModuleHandle(NULL), in this case.
 

BruceJohnJennerLawso

Dread Lord of the Idiots
Addon Developer
Joined
Apr 14, 2012
Messages
2,585
Reaction score
0
Points
36
According to here Windows error 1813 is:



Are you sure your resources are linked into your DLL? That errorcode means that the resource with the ID IDD_TOOLBAR could not be found in the DLL that made the CreateDialog call -- the DLL handle returned by GetModuleHandle(NULL), in this case.

Yes that resource is defined in the rc file for the project, as 101 (I think)

If the resource wasnt linked, it should bring up an error in the compiler though, right? I shouldnt be able to compile anyways without it...

Hmmm, lemme go check something. This code should be written fine, since the example was included by the tutorial author, but there have been other minor things that I've had to fix manually as well...

Code:

Edit: okay, in the Rc file, there is

Code:
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
BEGIN
    PUSHBUTTON      "&Press This Button",IDC_PRESS,7,7,84,14
    PUSHBUTTON      "&Or This One",IDC_OTHER,7,31,84,14
END

And then in the resource.h

Code:
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by dlg_two.rc
//
#define IDR_MYMENU                      100
#define IDD_TOOLBAR                     101
#define IDC_PRESS                       1000
#define IDC_OTHER                       1001
#define ID_FILE_EXIT                    40001
#define ID_DIALOG_SHOW                  40002
#define ID_DIALOG_HIDE                  40003

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40004
#define _APS_NEXT_CONTROL_VALUE         1002
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

Although the source code example included originally had

Code:
#define IDR_MYMENU                      101
#define IDD_TOOLBAR                     101

which led me to wonder whether there might not be a few other silly errors hidden in various places

but why is it talking about an image file? This example is for creating a modeless dialog, so what is it talking about?

Edit: Aww crap :facepalm:

My mistake. I didnt get the rc file properly for some reason when I set the project up. I could have sworn I had imported it when I started. Sorry to bother you with this.
 
Last edited:

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
but why is it talking about an image file? This example is for creating a modeless dialog, so what is it talking about?
Image file = executable image file. That's what it's talking about.
 

Enjo

Mostly harmless
Addon Developer
Tutorial Publisher
Donator
Joined
Nov 25, 2007
Messages
1,665
Reaction score
13
Points
38
Location
Germany
Website
www.enderspace.de
Preferred Pronouns
Can't you smell my T levels?
Whats OSS? (other than the Office of Strategic Services :lol:). And what do you mean by the license of it? Must I pay for in order to be able to create nice GUIs?

OSS = Open Source Software
GPL library means that you need to provide source code of your application, whenever you distribute it after linking with the library. Not really suitable for making moneys, because the client will replace you with somebody who's cheaper. Commercial license removes this burden - you can do anything you want, but this costs too much money for somebody who doesn't own a company and has a lot of clients.
LGPL means that you don't need to distribute the source code, as long as you link the library dynamically (library as .dll files, together with the .exe)
Add to this that there are other OSS libraries, like Apache, BSD, boost, which are not as restrictive as GPL, or even LGPL, because they require only a copyright notice (BSD for sure - not sure about Apache), and that's all you need to know about licensing in software development, and many people have no idea about.
 
Last edited:
Top