Solved

VC++ and error C2664

Posted on 2000-03-07
3
373 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

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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

21 Experts available now in Live!

Get 1:1 Help Now