Compiling windows apps (not using MFC) with .cpp extension

I am using Microsoft VC++ 5.0

I've been writing a windows app in C, recently I changed the file extension of the main project file to .cpp so I could use some handy classes I created a while ago. Everything was fine after the change except that the DialogBoxParam func will not compile (this worked fine before), the actual code in question is below along with the error.

DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_TRANSACTION), hwnd, TransactionDialogProc, NULL);

error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'int (void *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(void)'

will this conversion not work in c++? I can't imagine that I can't use a dialog box just because I am using the cpp complier
camejosAsked:
Who is Participating?
 
chensuCommented:
This is because C++ has stronger type check. The TransactionDialogProc function must match the following prototype.

BOOL CALLBACK DialogProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
);
0
 
chensuCommented:
And you may need to type cast as follows.

DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_TRANSACTION), hwnd, (DLGPROC)TransactionDialogProc, NULL);
0
 
NickRepinCommented:
Nice shot :)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TryCommented:
Right on, "chensu", ... right on!!
0
 
TryCommented:
"camejos" do you understand what "chensu" has done?  If you don't, this is your moment to ask questions.
0
 
nils pipenbrinckCommented:
Funny..

I had the same problem yesterday night writing an example code for the c++ programming area :)

I've simply casted the function pointer to DLGPROC and it compiled...

nils


0
 
nils pipenbrinckCommented:
the code line was:

  DialogBox (hInstance, "DIALOG_1", 0, DlgFunc);

the c++ version is:

  DialogBox (hInstance, "DIALOG_1", 0, (DLGPROC) DlgFunc);


0
 
chensuCommented:
If you are used to C programming, you will experience a lot of such things in C++ due to C++'s strong type check. The new C++ standard also introduces four type cast operators - static_cast, const_cast, reinterpret_cast and dynamic_cast.
0
 
TryCommented:
Some of these cast operators are equivalent to playing with fire, which we know can cause severe discomfort.  For example, 'const_cast' allows 'const-ness' to be cast away.

C++ then goes one step further and in addition to 'const_cast', provides the storage class specifier 'mutable' as an alternative to 'const_cast'.  A 'mutable' data member is ALWAYS modifiable even in a 'const' member function or 'const' object.

Handle with care, or keep a lot of heartburn remedies nearby.
0
 
nils pipenbrinckCommented:
<offtopic, sorry>

I'm very glad about these new typecasting things..

you know.. when I really want to modify a const value I can do it.. using inline assembler or void * casting... the whole thing just makes it more portable.

</offtopic, sorry>

nils
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.