Click here to Skip to main content
Click here to Skip to main content

How to create an enum and callback function

By , 2 Apr 2005
 

image002.jpg

Introduction

Have you ever seen functions by name EnumFunctions? Like EnumWindows etc. Have you ever wondered how they are created? In this article, we will create a simple one! Just be relaxed and read this article to EOF!

What is an enum function?

These types of functions are ...!?! Let me explain like this: assume that you have some fruits. OK? Some apples, oranges, and bananas. You are searching for apples. And, for each apple you can find, you will shout: "Hey I found another apple!" This is the role of this type of functions: for each element, return something.

Let's start

Declaring an EnumFunction is simple. First, you should have a variable type to tell the function what kind of function is the input. See this code snippet (declarations in the header file):

typedef BOOL (CALLBACK*YOURPROCTYPE)(FirstType,SecondType,...);

Now see mine:

typedef BOOL (CALLBACK*MYPROC)(LPCTSTR);

And for declaring the EnumFunction:

ReturnType WINAPI EnumFunction(YOURPROCTYPE lpProcname,arg-list,…);

And mine:

BOOL WINAPI EnumFunc(MYPROC lpProc);

EnumFunc passes the parameters to a callback function:

ReturnType CALLBACK yourProcName(FirstType,SecondType,...);

Note that the argument list of this callback function must be what you give in YOURPROCTYPE:

BOOL CALLBACK MyProc (LPCTSTR prompt);

Here are my sample code declarations:

typedef BOOL (CALLBACK*FILESPROC)(LPCTSTR);
void WINAPI EnumFiles(FILESPROC lpEnumFiles,LPCTSTR lpszFileName);
int CALLBACK EnumFilesProc (LPCTSTR lpszFileName);

And here goes the EnumFiles function. Its task is to find the files in the given path, and for each file, calls the callback function:

void WINAPI EnumFiles(FILESPROC lpEnumFiles,LPCTSTR lpszFileName)
{
        CString nfle;
        numoffiles=0;
        CFileFind file;
        BOOL bFind=file.FindFile(lpszFileName,NULL);//Find first file in the given
        //path
        while (bFind)//While bFind==True continue finding...
        {
               bFind=file.FindNextFile(); //Find next file
               numoffiles=lpEnumFiles(file.GetFileName());//Call given callback function
               //by user with appropriate inputs 
        }
        nfle.Format("%d  Files found",numoffiles);
        AfxMessageBox(nfle);//Give the user number of files found in
        //the given path
}

BOOL CALLBACK EnumFilesProc(LPCTSTR lpszFileName)
{ 
        numoffiles++;
        list->InsertString(list->GetCount(),lpszFileName); 
        return numoffiles;
}

Now for calling the EunmFunction, you only need to write some code:

list->ResetContent();
EnumFiles(EnumFilesProc,"C:\\*.exe");

In this source code, we will get a path and enumerate all the files in the path! I use this enumeration technique to get all the sections in an INI file, and you can do anything you want! Any opinions and comments are welcome. Or questions maybe..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

dSolariuM
Web Developer
United States United States
Member
I started Visual C++ on 2003 and I am a beginner now,I am working on a Network program and I am searching for some partners.I am intrested in DLLs and GUI in programing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank You!memberAndy San23 May '06 - 2:20 
This article saved some time for me. Thank You.Smile | :)
QuestionWhat?memberSreekanth Muralidharan6 Dec '05 - 19:24 
Hey !!
In your code, you are calling the callback not the kernel.
Beware!!WTF | :WTF:
Regards

 
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
AnswerRe: What?memberThatsAlok6 Dec '05 - 21:46 
Sreekanth Muralidharan wrote:
In your code, you are calling the callback not the kernel.

 
Any particuler reason behind it!
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
GeneralRe: What?memberSreekanth Muralidharan8 Dec '05 - 16:51 
Hello Alok
Callback functions are registered with WinOS to be called by it. Here nothing of that sort happens.
Regards
 
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
GeneralRe: What?memberThatsAlok8 Dec '05 - 17:34 
Sreekanth Muralidharan wrote:
Callback functions are registered with WinOS to be called by it. Here nothing of that sort happens.

 
Something some where missing ...AFAIK It is not necessay to register Callback function with OS you can create you own Callback function... you just have to call the Function thatsIT
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
GeneralRe: What?memberSreekanth Muralidharan8 Dec '05 - 22:00 
Thanks for your info.

 
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
GeneralRe: What?memberSreekanth Muralidharan22 Nov '06 - 5:51 
Infact, I didn't literally mean the word "register", and as you said, the callback function after creation should be informed to the API framework so that it gets called by the kernel.
 
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA

GeneralRe: What?memberThatsAlok23 Nov '06 - 0:09 
Sreekanth Muralidharan wrote:
11:51 22 Nov '06

 
reply after one year.. great! Smile | :)
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Re

GeneralRe: What?memberSreekanth Muralidharan24 Nov '06 - 5:22 
Ha ha ha!! Alok!! I hope you got my point... I am a bit confused in this topic.
 
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA

GeneralRe: What?memberoldkingcoleman21 Nov '06 - 7:51 
Hello - Are you looking for a partner to cooperate in some research? I'm not sure what benefit I can be to you but I am also interested in that subject - just drop me a line when you receive this and we'll see what we have in common. Peter, in Ireland. petercoleman2004@eircom.net
 
reacclimatising after a prolonged absence from the world of software

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 2 Apr 2005
Article Copyright 2005 by dSolariuM
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid