How to print error in C++?

h4ever

Member
Aug 30, 2013
163
0
0
Hello,
I am C++ newbie. I am starting with programming. I need a help. What should I do when I need to print error for user. This is in case when I try to open file

Code:
try
    {
        scen.open(ScenPath, TempPath);
    }
    catch (std::exception &ex)
    {
    }
so when the file is not open, the user should see dialog box "Cannot open file"... and a button to submit the dialogue box.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Depends on the underlying OS as to how you would then generate a dialog box.

Why not output a string to the stdout.
 

h4ever

Member
Aug 30, 2013
163
0
0
This is to be used in Windows. I use Windows XP, but other users can use different Windows version. I dont know if I print out to stdout will they see the output?
 

Daxwax

Member
Oct 7, 2011
35
0
0
Are you using a framework for user interface like winforms, WCF or a cross platform framework like Qt?
If it's a console application, you're probably stuck with printing a message to the console window and "Press any key to continue..".
 

h4ever

Member
Aug 30, 2013
163
0
0
I begin in Visual Studio Expres 2010, i dont use any frame work. The application dont need any windows except the error message. I think it would be better to generate dialogue window with error same as Windows does it. So I think I will need to keep it in win32 app.

Edit:
I start from opensource project which I try to remake to do what I need. So I remove some parts from the code and some files. So it is already win32 app. The author of the original project used function like this:
Code:
 MessageBox(desc.c_str(), "Scenario Load Error", MB_ICONWARNING);
But there is the problem that I remove the window that Author of the original code used. So I acutally dont have any window where I could to print it.

You can check the source codes here:
http://sourceforge.net/projects/autots/files/aokts-1.0.1 scenario_extractor.zip/download
The original project is called AOK Trigger Studio which serves to edit scenario files. I try to make a program which will extract certain scenario data. When I commented out some parts of the project - see file aokts.cpp ... every line with @@@ is commented out by me. So I finished with the problem that I don't know how to display the errors. The original program uses Window with menus to open and load scenario, to edit data and save them.. But I don't need anything from the menus or sheets the author used.
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
Ok, I know, but I need to learn how to use it with WINAPI. Author made a window which is opened:
http://paste.ofcode.org/5KCKbwj9m83djn28igkgV7
But I wish not the window to be opened. I wish the Window (with error) opens only when the error occurs. I would have it like in the original sourcecode so I would have all the time the window opened. For example. The Window will open then I start to read a scenario, then I wait to complete loading of data, during the time I still see the Window and then when completed, the window will disapper (app is closed). But I wish not Window to be there until error occurs.
 

h4ever

Member
Aug 30, 2013
163
0
0
What if I would do it like in this tut:
http://www.builder.cz/rubriky/c/c--/ucime-se-win-api-motivacni-uvod-155994cz
there is the code at bottom. They use NULL as a handler
Code:
 MessageBox(NULL, "Hello Win API !!!","My first program", MB_OK);

But it's still not clear to me how to do it if I want create the window only when the Incident happens.

BTW: I plan to remake the application to an dll if possible so this will not look like an app with userinterface
 

h4ever

Member
Aug 30, 2013
163
0
0
Yet I have found one code:

Code:
HWND CreateMapView(HWND owner, int x, int y, class Scenario * scenario);

HWND CreateMapView(HWND owner, int x, int y, Scenario * scenario)
{
    static bool registered = false;

    // WTF is this for? Yeah, I've read the docs, but the question remains.
    HINSTANCE instance = GetModuleHandle(NULL);

    if (!registered)
    {
        MakeMapClass(instance);
        registered = true;
    }

    HWND mapview = CreateWindowEx(WS_EX_TOOLWINDOW, MapClass, "Map Viewer", 
        WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
        x, y,
        0, 0,   // width, height are calculated in OnWM_CREATE
        owner, NULL, instance, scenario);

    return mapview;
}

HWND MakeMapView(HWND sheet, int cmdshow)
{
    HWND ret;
    RECT rect;

    GetWindowRect(sheet, &rect);
    ret = CreateMapView(sheet, rect.right + MAP_OFFSET, rect.top, &scen);
    ShowWindow(ret, cmdshow);

    return ret;
}
Would it be usable to remake it to a window when I need to display error?
Maybe you could explain me the difference. Because I clearly see here is difference between the definition of the main window:
Code:
int WINAPI WinMain ...
and the Map window (which can be closed without exiting the main app).
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Put the MessageBox in the catch area.

There are multiple options for buttons/icons for the MessageBox - check MSDN for the details.


example
MessageBox(NULL,"Error Mesasge", MBICONWARNING | MBOK);
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
But I dont ask if should I use MessageBox

but if should I use WINAPI WINMAIN. But now it seems to me obvious, that that is necessary.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
But I dont ask if should I use MessageBox

but if should I use WINAPI WINMAIN. But now it seems to me obvious, that that is necessary.

WinMain is the initial entry point for a windows program. Required
You do not need to actually have a Window created for the program; just the message pump.

MessageBox is just a child window that gets created; had it's own message pump and notifies WinMain when destroyed
 

h4ever

Member
Aug 30, 2013
163
0
0
Can you help with this?

http://paste.ofcode.org/35B57JYGkaJDnct4UkDfGcB

I removed many things from the original code, because my goal is to get Win32 app to read (decompress) scenario and extract data from it.

Now I got the error near:
Code:
MessageBox(sheet, desc.c_str(), "Scenario Load Error", MB_ICONWARNING);
The error says that sheet does not exist. The original application created sheets or sheet and pages. But I think I need not sheets. Maybe only a simple Win32 app. I need no User interface.

Can you help please, to configure it so to work? I know that there are not the included files, but maybe you can check it and show me how to correct to display the error. Or how to simplify and remove the sheets?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Replace sheet with NULL.

The first parameter is the handle/window that the MessageBox is to be a child of.
 

h4ever

Member
Aug 30, 2013
163
0
0
OK, thanks. Could you please help me (guide me) with the error messages which I got in the aokts.cpp?

Now I got error(s)
Code:
error C2065: 'PSCB_PRECREATE' : undeclared identifier
error C2051: case expression not constant
error C2065: 'PSCB_INITIALIZED' : undeclared identifier
error C2051: case expression not constant
error C2065: 'TOOLTIPS_CLASS' : undeclared identifier
error C3861: 'TooltipInit': identifier not found
error C2065: 'PROPSHEETHEADER' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'header'
There is this code:

Code:
int CALLBACK PropSheetProc(HWND sheet, UINT msgid, LPARAM lParam)
{
    switch (msgid)
    {
    case PSCB_PRECREATE:
        {
            DLGTEMPLATE *templ = (DLGTEMPLATE*)lParam;

            templ->cy += 5;
I believe this is the onbly one location in the project where the keyword PSCB_PRECREATE is used. But of sure the code was OK until I commented some parts.

And yet this one: this is at creation of the sheet
HWND MakeSheet(HINSTANCE app)
Code:
error C2065: 'header' : undeclared identifier
For this line:
PROPSHEETHEADER header;


'PSH_MODELESS' : undeclared identifier
error C2065: 'PSH_USECALLBACK' : undeclared identifier
error C2065: 'PSH_NOAPPLYNOW' : undeclared identifier
error C2065: 'PSH_NOCONTEXTHELP' : undeclared identifier
error C2065: 'PSH_USEICONID' : undeclared identifier
error C2065: 'header' : undeclared identifier

Looks similar

Code:
http://paste.ofcode.org/35B57JYGkaJDnct4UkDfGcB
 
Last edited:

sm625

Diamond Member
May 6, 2011
8,172
137
106
You seem to be missing an include file which defines a PROPSHEETHEADER object as well as all the flags used by that object.
 

h4ever

Member
Aug 30, 2013
163
0
0
You seem to be missing an include file which defines a PROPSHEETHEADER object as well as all the flags used by that object.

Do you mean this?

Code:
HWND MakeSheet(HINSTANCE app)
{
    PROPSHEETHEADER header;
header.dwSize = sizeof(header);
    header.dwFlags = PSH_MODELESS | PSH_USECALLBACK |
        PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP | PSH_USEICONID;
    header.hwndParent = NULL;
    header.hInstance = app;
    header.pszIcon = MAKEINTRESOURCE(IDI_LOGO);
    header.pszCaption = szTitle;
    header.nStartPage = 0;
    header.pfnCallback = &PropSheetProc;
Or do you mean some other file, where the word PROPSHEETHEADER should be found? There is only one word PROPSHEETHEADER in the original project (which works fine).

I don't understand why the file should miss but original verison of program worked fine still.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Ensure you have prtsht.h in your project

That contains the PSDH_* variables that you need
 

h4ever

Member
Aug 30, 2013
163
0
0
Something like this?
http://www.google.cz/url?q=http://www.cs.colorado.edu/~main/cs1300-old/include/prsht.h&sa=U&ei=MdxmUsGQN8vJswbhn4GADA&ved=0CDEQFjAE&usg=AFQjCNGppiKl-lvm0nLcLd_irnntkWCDPQ
But why should I add it into project when author of the project has no this file? It means that he used that constants but he does not need the file (because programmers can run the app withouth the file - you can test if you don't believe me). It confuses me. I don't know how should I set the constants. It could result in something
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
The file was within the project package that you linked to in Post #17.
Copy the file to you working project directory.
Reference it in the source file that you have the PSH_*variables.

As to why the file contents are needed but not referenced, I can not explain. I use Visual Studio but am not an expert on it's internals
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
I consulted with one programmer and he told me:
OnCompressOrDecompress seems to inflate/deflate the scenario's body.
While FileOpen is used to read the file (a cache/temporary file).
Both are needed; you should not remove any to simplify the code.
But the guy is not so good in C++ so cannot help me more. I simplified the code as much as I was able, but now I am in stage, where I need help of more experienced programmers, how to make the app working. I need to solve thiese things:
1) CALLBACK PropSheetProc: do I really need it? If no, how to remove it. (Not, I don't need tooltips, I forgot to delete this piece of code)
2) Do I need MakeSheet and all the sheet thing/references?
If not how to remove it.
3) Do I need the CALLBACK DefaultDialogProc? If not how to remove it?
4) how to change Sheet_HandleCommand to make the application able
a) open scenario
b) (first command) to open (if not opened yet) and load scenario data?
c) to close file and exit app
5) Do I need the CALLBACK MainDlgProc ? I no, how to remove it?
6) Do I need ProcessCmdline?

The project is here:
http://sourceforge.net/projects/autots/files/aokts-1.0.1-r70_extractor%20no%20tooltips.zip/download

My goal is to read scenario file and decompress it (to get the data from the scenario file). I wish not use GUI.
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
EagleKeeper:
Do you still have the AOKTS project in your computer? If yes, could you please check this:

aokts.cpp - there is definition of function Sheet_HandleCommand, where is variable id used in the switch (id) command. Whilst I know where the constants are defined (common.rc), I cannot find where the id is declared and defined. Can you help to find the location?