Solved

VC++ and error C2664

Posted on 2000-03-07
3
369 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
Comment Utility
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
Comment Utility
Thanks a lot, nietod! You're a genius.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now