김광수 님이 쓰신 글 :
: callback이란 말이 잇는대 이게 몬가요?
피호출 함수(FunctionC)에서 호출할 수 있도록 호출 함수(FunctionA)가 전달한 함수(FunctionB).
보통 Windows API 함수들 중 Enum으로 시작하는 함수들이 콜백함수를 요구합니다.
void FunctionA();
int CALLBACK FunctionB(int no);
void FunctionC(int (CALLBACK *callbackFunction)(int));
FunctionC는 보통 FunctionA나 FunctionB가 들어가는 프로그램을 만들기 전에 이미 별도로 만들어져 있는 것입니다.
void FunctionA()
{
FunctionC(FunctionB);
}
int CALLBACK FunctionB(int no)
{
//no를 가지고 필요한 작업을 수행한 후
//적당한 값을 return;
}
void FunctionC(int (CALLBACK *callbackFunction)(int))
{
int no=??;
//콜백함수를 요구하는 함수는 보통 그게 for든 while이든 루프를 돌리며 작업해야 하는 경우가 많음
while(1){
//함수 용도에 맞는 작업을 여기서 하고;
//no=???;
if(callbackFunction) callbackFunction(no);
//if(????) break;
}
}
실제 예를 들면,
BOOL CALLBACK callbackForEnumWindows(HWND hwnd, LPARAM lParam);
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Clear();
//Windows API 함수인 EnumWindows를 호출
EnumWindows(callbackForEnumWindows,(LPARAM)ListBox1);
}
//---------------------------------------------------------------------------
BOOL CALLBACK callbackForEnumWindows(HWND hwnd, LPARAM lParam)
{
static char buff[1024];
GetWindowText(hwnd,buff,1023);
((TListBox*)lParam)->Items->Add(IntToStr((unsigned)hwnd) + " : " + buff);
return TRUE;
}
|