Solved

VC++ and error C2664

Posted on 2000-03-07
3
378 Views
Last Modified: 2010-08-05
Hi!
I've got VC++ 5.0 and a following code:

//function prototypes
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK       EnumWndAtch (HWND, LPARAM);

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ....
    //here I call EnumWndAtch function
    EnumWindows(EnumWndAtch, TRUE);
    ....
}
//called function
BOOL CALLBACK EnumWndAtch(HWND hwnd, LPARAM lParam)
{
    ....
}

When I have this code in a .cpp file, I will get a compiler error:
error C2664: 'EnumWindows' : cannot convert parameter 1 from 'int (void *,long)' to 'int (__stdcall *)(void)'.
But when I rename a .cpp file to a .c file, it works. But I want to use C++ enhancements, whitch i cannot use in a .c file.
I tried it with an extern "C" command, but it still didn't work. It's sure easy, but I can't think up how to use this code in C++.
Thanks in advance.
0
Comment
Question by:myson
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2592760
Your code is correct.  The problem is that--depending on the STRICt #define--the EnumWindows function is declared to take a pointer of type FARPROC which points to a procedure that does not have any parameters.  You are passing a pointer to a procedure that has parameters (as it should in this case) but the compiler complains because it things you should pass a pointer to a procedure with no parameters.  

To fix this, just cast the pointer you are passing to tyupe WNDENUMPROC,  like

EnumWindows((WNDENUMPROC) EnumWndAtch, TRUE);

Let me know if you have any questions.
0
 

Author Comment

by:myson
ID: 2596968
Thanks a lot, nietod! You're a genius.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2597456
A genius?

Ahh that explains it.  The reason all the IQ tests-WISC, WASE, WIPSE--all begin with W. W is for windows.   :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 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