Callback

zhaozj2021-02-16  85

The callback function is written by you yourself. You need to call another function, and one of the parameters of this function is?

It is your callback function name. In this way, when the system is necessary, call your callback function, so you can?

To complete what you have to do in the callback function. ?

?

CapVideoStreamCallback? This callback function, I have not done, I saw Help, should it be sent by sending a message?

WM_CAP_SET_CALLBACK_VIDEOSTREAM, set, or call macro CapsetCallbackonvideStream?

To do it. After setting, the system will automatically call the callback function you write during the image capture process. ?

?

The function of this callback function needs you to write, then call it in another function, ie,?

LRESULT? Callback? CapVideoStreamCallback (hwnd? Hwnd, lpvideohdr? Lpvhdr)?

{?

? ........?

}?

/ / Call it in another function (ie as a parameter as the address of CapVideoStreamCallback)?

Function (1, ..., capvideostreamCallback, .....);?

This is as if we use timers, set up a modular function to the timer when set the timer:?

:: SetTimer (m_hwnd, 1,100, (timerproc); TMPROC is the callback function?

?

?

Module A has a function foo, which transmits the address of the FOO to the module B, and then when a certain event (Event) occurs in B, the foo's address is called Foo, and the notification A is what happened, let A made a corresponding reaction. ?

??? So we call the foo as a callback function. ?

?

?? "This callback function is not a declaration in vfw.h,"?

? ---- That is to declare the prototype of the callback function, telling you that the callback function to pass must be consistent with the prototype it defines. ?

?

??? "Why should I write a function?"?

For example, in the above module B, it only knows that when Event occurs, it is notified to the module A. How to respond to this event, not b, nor B can be expected. ?

??? You stand in a A perspective, of course, you have to make your own reaction, that is, you have to write a function yourself. ?

?

??? If you understand the function pointer inside C , you can easily understand the callback function. ?

?

"I don't know what results after the system calls, or how do I use this result?"

--- If you pass a callback function address to the system, then your program is equivalent to the module A I said above, the system is equivalent to the module B, the system just call your function, it is impossible to know what results will be . ?

?? How do you use this result? See how you define this callback function. ??????

The callback function and the callback mechanism are different concepts. , The function is called, but the callback mechanism is not implemented in a different language. . . . For example, C # ... General In the Windows? API?, It will be implemented using a function pointer. . . ?

?

?

?

Function call mode?

??

We know that there are several call methods when performing a function call, mainly divided into C, PASCAL style. In C and C ? C-type call is default, the members of the class is default call to _stdcall. The two are different, let's use it below?

Example Description:?

?

1.? __ cdecl?: C and C default call mode?

Example:??

Void? INPUT (? INT? & M, INT? & n); / * is equivalent to void? __ cdecl? INPUT (int? & m, int? & n); * /?

The following is the corresponding assembly code:?

00401068? LEA? EAX, [EBP-8]? Take [EBP-8] address (EBP-8), save to EAX?

0040106B? Push? EAX?; Then press the stack?

0040106C? LEA? ECX, [EBP-4]? Take [EBP-4] address (EBP-4), save to ECX?

0040106F? Push? ECX?; Then press the stack?

00401070? Call? @ Ilt 5 (INPUT)? (0040100A); then call the INPUT function?

00401075? Add? ESP, 8?; Recovery stack?

??

• From the process of calling the input function, you can see that before calling this function, first pressing EBP-8,

The rear pressure stack EBP-4, then call the function INPUT, and finally the Input function call is completed, use the ESP 8 recovery stack. From?

This shows that the default function modification _cdecl in the C language call is made by the main call function and recovered?

Stack. ?

Let's take a look at: What is address EBP-8 and EBP-4? ?

??? In the VIEW of VC, select Debug? Windows, then select Registers, display the register variable, and then?

Select Debug? Windows The following Memory, input EBP-8 values ​​and EBP-4 values ​​(or directly input EBP-8 and -4),?

Look at what is the actual storage of these two addresses, is actually the address (EBP-8), M address of the variable? N??

(EBP-4), it can be seen that the stack is performed in the main modulation function and the order is from right to left. In addition,?

Since the argument is a reference to the corresponding variable, it is also proved that the address of the variable is actually referenced (similar to pointer). ?

Summary: The default function modification _cdecl in C or C language is called, the parameter stack is made by the main call function and?

Restore the stack, the stack order of the arguments is from right to left, and finally the stack recovery is made by the master function. Due to main adjustment?

Function Management Stack, so you can implement a variable-gate function. In addition, the naming modification method adds a decline before the function?

Line (_).?

?

2.? WinApi? (Actually is PASCAL, CALLBACK, _STDCALL)?

Example:??

Void? WinAPI? INPUT (? INT? & M, INT? & n);?

Look at the assembly code of the corresponding call:?

00401068? LEA? EAX, [EBP-8]?

0040106B? Push? EAX?

0040106C? Lea? ECX, [EBP-4]?

0040106F? Push? ECX?

00401070? Call? @ Ilt 5 (Input)? (0040100A)?

??

• From the process of calling the input function, you can see that before calling this function, first pressing EBP-8,

The rear pressure stack EBP-4, then call the function INPUT, after calling the function input, no corresponding stack recovery work? (Call for other functions, so I don't list)?

The following will list the assembly code of the Input function itself: (actually this function is not big, but the assembly example is still big ,?

Everyone can only look at the front and after, the intermediate code is not related to this example)?

39:? Void? WinAPI? INPUT (? INT? & M, INT? & N)?

40:? {?

00401110? Push? EBP?

00401111? MOV? EBP, ESP?

00401113? SUB? ESP, 48H?

00401116? Push? EBX?

00401117? Push? ESI?

00401118? Push? EDI?

00401119? LEA? EDI, [EBP-48H]?

0040111c? MOV? ECX, 12H?

00401121? MOV? EAX, 0cccccccch?

00401126? Rep? STOS? DWORD? PTR? [EDI]?

41:? Int? S, i;?

42:?

43:? While (1)?

00401128? MOV? EAX, 1?

0040112d? Test? EAX, EAX?

0040112f? JE? Input 0C1H? (004011d1)?

44:? {?

45:? Printf ("/ NPLEASE? INPUT? THE? First? Number? M:");?

00401135? Push? Offset? String? "/ NPLEASE? INPUT? The? First? Number? M ...? (004260b8)?

0040113A? Call? Printf? (00401530)?

0040113f? Add? ESP, 4?

46:? Scanf ("% d", & m);?

00401142? MOV? ECX, DWORD? PTR? [EBP 8]?

00401145? Push? ECX?

00401146? Push? Offset? String? "% D"? (004260B4)?

0040114b? Call? Scanf? (004015f0)?

00401150? Add? ESP, 8?

47:?

48:? If? (? M =? S?)?

004011B3? MOV? EAX, DWORD? PTR? [EBP 8]?

004011B6? MOV? ECX, DWORD? PTR? [EAX]?

004011B8? CMP? ECX, DWORD? PTR? [EBP-4]?

004011bb? Jl? Input 0afh? (004011 bF)?

57:?? Break;?

004011bd? Jmp? Input 0C1H? (004011d1)?

58:? Else?

59:? Printf ("? M?

004011BF? Push? Offset? String? "? M?

004011C4? Call? Printf? (00401530)?

004011c9? Add? ESP, 4?

60:?}?

004011cc? Jmp? Input 18h? (00401128)?

61:?

62:?}?

004011d1? POP? EDI?

004011d2? Pop? ESI? 004011?

-------------------------------------------------- -------------?

?

?

Analysis of Stack Change when Win32 Program Function Call?

??

2002-12-10? 18: 19: 13?? PCVC.NET??? Halk?? Read the number of hours:? 4804??

? In the classic assembly language tutorial, the use of the stack when the function call is a problem that is highly concerned. Nowadays, with the increasingly improved senior language, there are not many procedures for simply use assembly development. However, the understanding of the stack trend when the function calls still helps us to implement the execution process of the program, so there is a clear idea in the process of programming and debugging. ?

?

One. Call agreement?

In WIN32, there are two main consequences of the calls of functions. ?

1. _stdcall?

???? The function called __stdcall is the following characteristics:?

?? •????????????????????????????????????????????????????????????????????????

? • • When the call returns, the stack is adjusted by the modified function?

2. __cdecl?

__cdecl agreement is the default call agreement of the C / C function. It has the following characteristics:?

?? •? The parameter is stack the right to left?

?? •? When the call returns, the stack is adjusted by the caller?

?

two. WIN32 function calling process?

?

1.???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

The parameters given by the caller will be pressed into the stack according to the above call mode. ?

?

2.??? Press the breakpoint?

When the program executes to the CALL instruction, the address of the current statement is pressed into the stack as a breakpoint address. ?

?

3.??? Jumping?

The value of the EIP is reset to the starting address of the modulated function. ?

?

4.????? EBP,? ESP?

Here, the EBP is used to find the parameters in which the caller pressed in the stack, and simultaneously as a backup of the caller stack pointer. It should also be implemented before:?

Push? EBP?

Save the original value in the EBP. ?

?

5.????? ESP, N?

Here n is a total byte number of local variables in the function plus an integer, typically 40. Thereafter, the ESP is the stack pointer to the modulated function. ?

?

6.??? Initializing the N-byte space between ESP? ~ ESP-N?

This is an initialization of memory space used to be used to local variables in the stack, which is usually set to 0xcc. ?

?

7.??? Sequentially executed in the function. ?

At this time, the stack of functions is located after all local variables of the memory space, and there is generally 40 bytes of isolation bands between the two. ?

?

8. Return?

In order to ensure the normal return of the call, the function should ensure that the specification uses the stack so that the value of the ESP will return to the status before performing the first statement when returning. Note White Point is that every Push has a corresponding POP. ?

The process of calling the return is as follows:?

MOV? ESP,? EBP?

After the execution, the ESP returns to the caller of the caller, and the value of the original EBP is still pressed, and the parameters when the invoked time is stored. ?

Then pop up the value of the EBP and the breakpoint address. If the __cdecl will return to the caller directly, the caller will be responsible for adjusting the stack and discarding the parameters previously pressing. If it is __stdcall, this work is executed by the modified function. ?

?

The program sample is as follows:?

...?

0040B8E8??????????????????????????????????????????????????????????????????????????????????????????????????????????

0040B8EA?????????? Call the function?

...?

00401028??????????; Jump to function entry? ......?

0040B7C0???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

0040b7c1????????????

0040b7c3??????; Set the stack pointer of the function, 4?

; Partial variables of bytes?

0040B7C6?? Push?????? EBX?

0040B7C7??????????????????

0040b7c8?????????????

0040b7c9??????????????

0040b7cc????? ECX, 11H?

0040B7D1????? Eax, 0cccccccch?

0040b7d6???????????; Initialize local variable space?

0040B7D8????? Eax, DWO

转载请注明原文地址:https://www.9cbs.com/read-13477.html

New Post(0)