Solved

VC++ and error C2664

Posted on 2000-03-07
3
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 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