How to print error in C++?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

h4ever

Member
Aug 30, 2013
163
0
0
I have overlooked that id is 3rd argument of the function
Code:
bool Sheet_HandleCommand(HWND sheet, WORD code, WORD id, HWND control)
And it is defined like here
Code:
Sheet_HandleCommand(sheet, HIWORD(wParam), LOWORD(wParam), (HWND)lParam))
Which is called from inside this function:
Code:
//    MainDlgProc: The DialogProc for the main property sheet window.
#define CALLPROC()    CallWindowProc((WNDPROC)pproc, sheet, msg, wParam, lParam)

INT_PTR CALLBACK MainDlgProc(HWND sheet, UINT msg, WPARAM wParam, LPARAM lParam)
Have you idea what does the LOWORD(wParam) function and HOWORD(hParam) function do?
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Calls to handlers in Windows have 2 data arguments lParm and wParm.
Because these are both 32 bit words; the wParm, in this example is being used to carry two pieces of information in each 16bit chunk.
The HIWORD(wParam) extracts the high 16 bits of wParam: wParm >> 16
The LOWORD(wParam) extracts the low 16 bits of wParam: wParm & 0xFFFF
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
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?

id is one of the parameters that is passed into the Sheet_HandleCommand method.

bool Sheet_HandleCommand(HWND sheet, WORD code, WORD id, HWND control)
 

h4ever

Member
Aug 30, 2013
163
0
0
Finally I found what the CALLBACK WndProc is for and how it works. I realized that the work callback has just same meaning like running a routine programme which handles events. (Sorry if my terms are not correct, I still learn correct terms.) I think I don't need WndProc or CallWindowProc in my program, because I would like to create dll which will be controlled by dll call from different application. I think I will reader need some different function or method to gain control on running dll app. E.g. if I will load the dll, then want to contact the app to open a scenario, then I will contact the dll to load data and send them, then I will contact the app to close the scenario and then I will contact app to terminate itself.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I do not think you can contact the DLL directly - it must be called via exposed methods to the app.

The DLL is just a collection of methods that are used to support the app. It is not a stand alone application; Some wrapper code is needed.

The DLL may utilize some shared communication mechanism to inter application communication
 

h4ever

Member
Aug 30, 2013
163
0
0
I will use this application as dll later, but now I would like to fix the bugs and to make it to open & read file.
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
Hello,
can you please help again? I have the project here

project download

I have 2 problems.

Suddenly it makes error
Code:
 LINK : fatal error LNK1104: cannot open file 'zlib1d.lib'

There is not any line number so I don't know where to look. It's strange because original project should have zlib1d.lib . Did I delete some (wrong) file? I see the file "zlib1.lib" in the dep include folder. but the zlib1d.lib is not even in original.

Another problem:
Can you help me to create a class with a window and error dialog box, which will open when MessageBox is called? I have no idea how to fix the thing with errors. This file should be made as dll later. I created a function controller in aokts.cpp which should be called from external application. So the application will specify what file should be opened and what to do with it. But now I need to fix these two problems.
 

h4ever

Member
Aug 30, 2013
163
0
0
There must be additional dependency on the library file in the project. This is what I have found when been looking for the keyword:
Code:
r:\project_path\aokts.vcproj
     63: AdditionalDependencies="zlib1d.lib comctl32.lib libexpatw.lib"
    220: AdditionalDependencies="zlib1d.lib comctl32.lib libexpatw.lib"
r:\project_path\aokts.vcxproj
     91: <AdditionalDependencies>zlib1d.lib;comctl32.lib;libexpatw.lib;%(AdditionalDependencies)</AdditionalDependencies>
    135: <AdditionalDependencies>zlib1d.lib;comctl32.lib;libexpatw.lib;%(AdditionalDependencies)</AdditionalDependencies>
r:\project_path\Debug\aokts.log
     13: p:\VS_10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:".\aokts.exe" /INCREMENTAL /NOLOGO /LIBPATH:"r:\project_path\deps\lib" zlib1d.lib comctl32.lib libexpatw.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Debug\aokts.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"r:\project_path\aokts.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:".\aokts.lib" /MACHINE:X86 Debug\AOKTS.res
     31: LINK : fatal error LNK1104: cannot open file 'zlib1d.lib'

Looks like it is debug version of the file zlib1.lib
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Can you help me to create a class with a window and error dialog box, which will open when MessageBox is called?

MessageBox is a Windows method that creates a unique dialog box with the parameters that you pass into it.

If you need more than the capabilities that the MessageBox provides
You will need a class that is derived from CDialog and add in the components that you need.
 

h4ever

Member
Aug 30, 2013
163
0
0
I thought that MessageBox needs HWND hWnd so I need to create a window into which I can add it. So if I need to do it, should I create a class with method to create Winapi window?

The content of the method would be ...
Code:
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPTSTR cmdline, int cmdshow)
{
HWND sheet;}
But I need to find out how to create the HWND sheet window...

Do I really need all the stuff as shown here?
http://www.winprog.org/tutorial/simple_window.html
Or I don't need these functions:
Code:
CALLBACK WndProc
UpdateWindow
and

Code:
while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
Do I need wc initiation?
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I thought that MessageBox needs HWND hWnd so I need to create a window into which I can add it. So if I need to do it, should I create a class with method to create Winapi window?

The content of the method would be ...
Code:
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPTSTR cmdline, int cmdshow)
{
HWND sheet;}
But I need to find out how to create the HWND sheet window...

Do I really need all the stuff as shown here?
http://www.winprog.org/tutorial/simple_window.html
Or I don't need these functions:
Code:
CALLBACK WndProc
UpdateWindow
and

Code:
while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
Do I need wc initiation?

If HWND is not NULLL, the parent window controls the visibility behavior of the MessageBox window.

Look up the definition of MessageBox

For windows related programming in this detail; you should get a book on the Win32API.

It provides you the insight as to how the guts of Windows programming operates.


Code:
while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
Do I need wc initiation?
The above is what is called the main message pump.

Code:
CALLBACK WndProc
UpdateWindow
The above is what is called the main message processor that takes each message and processes the details. this method is assigned when the main window class is defined..
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Charles Petzold had a good book that I learned from in the early 90s
 

h4ever

Member
Aug 30, 2013
163
0
0
I think I don't need a book. I know about site in my language, where they present C++ Win32 tutorial series. I can start to learn there.

If I would want to create the dll for the project, would it be realizable? You know, there are some global variables in the begin of the aokts.cpp, but when I would load the aokts.dll, there were not those global variables initiated (right?). So is it possible to share those globals of the dll project into the methods of the dll?

Edit: But I still have a feeling that learning GUI is not the right direction for me and looks like waste of time. I can learn about 4-6 basic lessons just for interest, to see how the windows works. But still what I need to find is tutorial to create dll which can use some basic windows like the one containing error message.
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You would be best to find tutorials on DLL usage and building them
but to use the DLL, you need to understand how the application interacts with the DLL.

You are working backward in the way to understand.
DLLs can be used in GUI as well as non GUI apps. All they are is an external library.
Just requires a little extra work to setup by the application in knowing that there is a DLL, the accessor methods.
 

h4ever

Member
Aug 30, 2013
163
0
0
Few times I tried to separate functions from main file and to include it. Unsuccessful. It starts to print error like the files needed were not included. Do I need to include those files in every included file? Thats strange. Just for short:

main.cpp:
here including common files....
here my function
here next code for WINAPI

... then I tried
main.cpp:
here including common files....
here include my function
here next code for WINAPI
and this did not work.

Also I tried this:
making a resources to load icons and menus...
main.cpp
main.h
main.rc
resources.rc

I found that I must to include the line into main.h and into main.rc
#include "resource.h"
Why not to include the file only once? I would say that some identifier was not defined.
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You should not include .cpp files in the source files;
Include the header files that have the signature of the methods in the .cpp files
the ensure that the project knows about the .h and .cpp files that you are using.
 

h4ever

Member
Aug 30, 2013
163
0
0
You should not include .cpp files in the source files; Include the header files that have the signature of the methods in the .cpp files the ensure that the project knows about the .h and .cpp files that you are using.

What do you mean. This sentence has two contradictory meannings
"Include the header files that have the signature of the methods in the .cpp files"
Did you mean
"Include the header files that have the signature of the methods into the .cpp files"?

So what does mean the "signature"? You mean declaration of the method? So I should create a class in .h probably, but this is not in the lesson, probably later.

Also why this prints error

Code:
main.rc(35): warning RC2182: duplicate dialog control ID 10
main.rc(36): warning RC2182: duplicate dialog control ID 10
main.rc(38): warning RC2182: duplicate dialog control ID 10
main.rc(40): warning RC2182: duplicate dialog control ID 10
refers to IDC_STATIC. The application works, it is a form to add text items into box lists or remove them...

Code:
IDD_DIALOG_MAIN DIALOGEX 0, 0, 218, 150
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | 
    WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,161,82,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,161,101,50,14
    LISTBOX         IDC_LIST_BOX,7,7,143,64,LBS_NOINTEGRALHEIGHT | 
                    WS_VSCROLL | WS_TABSTOP
    PUSHBUTTON      "Add text",IDC_ADD_TEXT,161,36,50,14
    EDITTEXT        IDC_EDIT,7,75,143,14,ES_AUTOHSCROLL
    PUSHBUTTON      "Remove item",IDC_DELETE_TEXT,161,55,50,14
    COMBOBOX        IDC_COMBO,7,103,143,43,CBS_DROPDOWN | CBS_SORT | 
                    WS_VSCROLL | WS_TABSTOP
    LTEXT           "Removed items",IDC_STATIC,7,92,59,8
    LTEXT           "0",IDC_LB_COUNT,181,16,30,8
    LTEXT           "Count of items",IDC_STATIC,166,7,45,8
    ICON            IDI_ICON1,IDC_STATIC,7,123,20,20
    LTEXT           "Ukázka základ&#367; práce s list-boxem a combo-boxem",
                    IDC_STATIC,44,125,164,8
    LTEXT           "Ovládání zcela intuitivní ... dobrou zábavu !!!",
                    IDC_STATIC,43,135,146,8
END
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
You should not include .cpp files in the source files; Include the header files that have the signature of the methods in the .cpp files the ensure that the project knows about the .h and .cpp files that you are using.

What do you mean. This sentence has two contradictory meannings
"Include the header files that have the signature of the methods in the .cpp files"
Did you mean
"Include the header files that have the signature of the methods into the .cpp files"?

So what does mean the "signature"? You mean declaration of the method? So I should create a class in .h probably, but this is not in the lesson, probably later.
Different verbiage, same meaning.
.h files should have only the declarations
.cpp files should have only the implementation of the declaration.

.cpp include the .h (header) files


Also why this prints error

Code:
main.rc(35): warning RC2182: duplicate dialog control ID 10
main.rc(36): warning RC2182: duplicate dialog control ID 10
main.rc(38): warning RC2182: duplicate dialog control ID 10
main.rc(40): warning RC2182: duplicate dialog control ID 10
refers to IDC_STATIC. The application works, it is a form to add text items into box lists or remove them...
IDC_STATIC is a unique definition and it not normally assigned to 10
You are possibly redefining it in a .h file.



Code:
IDD_DIALOG_MAIN DIALOGEX 0, 0, 218, 150
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | 
    WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,161,82,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,161,101,50,14
    LISTBOX         IDC_LIST_BOX,7,7,143,64,LBS_NOINTEGRALHEIGHT | 
                    WS_VSCROLL | WS_TABSTOP
    PUSHBUTTON      "Add text",IDC_ADD_TEXT,161,36,50,14
    EDITTEXT        IDC_EDIT,7,75,143,14,ES_AUTOHSCROLL
    PUSHBUTTON      "Remove item",IDC_DELETE_TEXT,161,55,50,14
    COMBOBOX        IDC_COMBO,7,103,143,43,CBS_DROPDOWN | CBS_SORT | 
                    WS_VSCROLL | WS_TABSTOP
    LTEXT           "Removed items",IDC_STATIC,7,92,59,8
    LTEXT           "0",IDC_LB_COUNT,181,16,30,8
    LTEXT           "Count of items",IDC_STATIC,166,7,45,8
    ICON            IDI_ICON1,IDC_STATIC,7,123,20,20
    LTEXT           "Ukázka základ&#367; práce s list-boxem a combo-boxem",
                    IDC_STATIC,44,125,164,8
    LTEXT           "Ovládání zcela intuitivní ... dobrou zábavu !!!",
                    IDC_STATIC,43,135,146,8
END
Link to understand IDC_STATIC
 

h4ever

Member
Aug 30, 2013
163
0
0
IDC_STATIC is a unique definition and it not normally assigned to 10
You are possibly redefining it in a .h file.

Ok, it was wrong value, thanks.
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
You should not include .cpp files in the source files;
Include the header files that have the signature of the methods in the .cpp files
the ensure that the project knows about the .h and .cpp files that you are using.

When I generate win32 window app, so I have these lines in the winapi.cpp:

Code:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
Should not these line be in the winapi.h when it just declares the functions which implementation is bellow?

Also I sometimes see something similar:
Code:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, 
                // A POINTER TO DIALOG BOX PROCEDURE - declared on begin of this file
                (DLGPROC)About);
there is the DLGPROC and I don't understand if is is a type or macro defined in some header file? What this piece of code does
(DLGPROC)About
does it declare a type or what? Why the brackets must be there?
 
Last edited:

h4ever

Member
Aug 30, 2013
163
0
0
Also I want to ask you if do you agree with this DEFAULT order of thing in the code:
This is inside APIENTRY _tWinMain block:
Code:
// WHOLE THIS BLOCK LOOKS TO ME AS INITIATION SO WHY WOULD WE
    // SEPARATE ALL THE LINES FROM InitInstance ?
    MSG msg;
    HACCEL hAccelTable;
    // ESSPECIALLY THAT THESE STRING VARS ARE USED IN InitInstance()
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_WINAPI, szWindowClass, MAX_LOADSTRING);
    // AND THE CLASS REGISTERED HERE IS ALSO USED IN THE InitInstance()
    MyRegisterClass(hInstance);
    // SO THE ARRANGMENT OF THINGS HERE LOOKS ODD TO ME
    if (!InitInstance (hInstance, nCmdShow)) 
    {
        return FALSE;
    }
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WINAPI);
The InitInstance() basicly only creates the window with command:
Code:
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

How would you prefer arrangment of the code? Move the lines before into the IniIntstance() function or would you abort the IniIntstance() for clear arrangement?
 
Last edited: