Solved

'.c' vs '.cpp'

Posted on 1998-04-18
7
374 Views
Last Modified: 2013-12-03
I have a program called 'app.c' that opens a dialog box. However when I rename the file to 'app.cpp' I get an error.

The line that gives the error is:
DialogBox ( hinstance, "SelWND", hwnd, WndDialogProc) ;
and the error message is:
error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'int (void *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(void)'

Declaration for WndDialogProc is :
BOOL    CALLBACK WndDialogProc (HWND, UINT, WPARAM, LPARAM) ;

Why does this error happen? I wanted to use classes/C++ without using MFC.
0
Comment
Question by:vsinha
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 60 total points
ID: 1298522
Because C++ does not implicitly convert between pointers to different function types while C does.

Your dialog procedure should be declared as:
BOOL CALLBACK WndDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

And
  #define STRICT
before you
  #include <windows.h>

0
 
LVL 1

Author Comment

by:vsinha
ID: 1298523
I was missing the following line:
#define STRICT

why is it needed? what does it mean?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1298524
You need to #define STRICT, which means to do strict type checking

It ensures that handle types and function prototypes much match exactly.  With C it helps catch errors, with C++ it makes code compile (as C++ already checks that function pointers match ok)

When STRICT is on, DLGPROC is defined as

typedef BOOL (CALLBACK* DLGPROC)(HWND, UINT, WPARAM, LPARAM);

When it is off it is just

typedef FARPROC DLGPROC;

where FARPROC is just typedef int (FAR WINAPI *FARPROC)();

This won't match with your WndDialogProc function in C++.  This is because in C the () in a function pointer declaration means there could be any args at all, and so arg type checking isn't done.  In C++ arg type checking is always done and () means no args at all - the same as (void).

In general is is a good idea to always use #define STRICT for both C and C++.  But it is almsot ESSENTIAL for C++. (C may compile without it, but C++ won't).

I hope this clears things up for you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:vsinha
ID: 1298525
Thank a lot!

btw, what is the advantage of not having #define STRICT ? or, in other words why does it exist, it seems to me that all programs should always use it.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1298526
For compatibility with code that was (poorly) ported from Win3.x.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1298527
Shame I didn't get offered any points for my lucid explanation .. that's life I guess :-(
0
 
LVL 1

Author Comment

by:vsinha
ID: 1298528
Ronslow, check Q.10048982
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
display png images in VB6 4 379
Is it possible to use a scroll bar to view records in a form? 7 42
knowledge extraction for DB and VBA 6 74
Issue testing the Class Library 4 48
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now