How to call callback function in nested DLL?
How to call the callback function in the nested DLL
HMODULE hModelHand = LoadLibrary(__T("MyDLL.DLL"));
SetLog pLogShowFun = (SetLog)GetProcAddress(hModelHand, MYTESTFUN);
{ if (NULL == pLogShowFun)
AfxMessageBox(__T("get mydll.dll address erro!"));
}
pLogShowFun((writeLog *)(Mylog::writelog));1234567891011
Operation in DLL
#define MyDLL_API extern "C" __declspec( dllexport )
typedef void(*writeLog)(const char *plogFile, int p_nType, const char * logText);
writeLog g_RunLog = NULL;
MyDLL_API void mytestfun(void *pfun)
{ if (pfun)
{
g_RunLog = (writeLog)pfun;
}
}
Explain how to implement C callback function
What is a C# callback function? The C# callback function is a function provided by the application to be called by the Windows system DLL or other DLL. It is generally used to intercept messages, obtain system information, or handle asynchronous events. The application tells the DLL the address pointer of the callback function, and the DLL calls the function at the appropriate time. The callback function must comply with the pre-specified parameter format and delivery method, otherwise the program or system will crash as soon as the DLL calls it. Normally, the callback function uses the standard Windows API calling method, that is, __stdcall. Of course, the DLL compiler can define the calling method by himself, but the client program must also comply with the same regulations. In the __stdcall mode, the parameters of the function are pushed onto the stack in order from right to left. Unless they are explicitly pointed out as pointers or references, the parameters are passed by value. You are responsible for popping the parameters from the stack before the function returns. Understand C# callback functions! When a program calls a function (usually referred to as api), it is equivalent to the program calling a function. The relationship is expressed as follows: call
program--------------------→dll
When a program calls a function, it passes the address of its own function as a parameter to the function called by the program (then its own function is called a callback function). DLL functions that require callback functions are often those that must be executed repeatedly. The function and relationship of the operation are expressed as follows: call (call)
callback (callback) When the function you call passes the return value to the callback function, you can use the callback function to process or complete certain operations. As for how to define your own callback function, it depends on the specific API function used. Many different types of callback functions have various parameters. Descriptions of these parameters are generally described in the help, such as the parameters and return values of the callback function. In fact, simply put, the callback function is called by the DLL after the function you wrote meets certain conditions! There is also a saying (which is easier to understand): The callback function is like an interrupt processing function, which is automatically called by the system when the conditions you set are met. The following is the sample code: WorkClass.cs: usingSystem; namespaceCallBackSample ...{ publicdelegatestringShowMessage(stringmessage); internalclassWorkClass ...{ publicvoidDoWork(stringmessage) ...{ ShowMessageshowMessage=newShowMessage(showCoolMessage); showMessage =showTerribleMessage; stringresult=""; foreach(ShowMessageshinshowMessage.GetInvocationList()) ...{ result =sh(message); } Console.Write(result); Console.Read(); } privatestringshowCoolMessage(stringmessage) ...{ strings=message "Cool!"; returns; } privatestringshowTerribleMessage(stringmessage) ...{ strings=message "Terrible!"; returns; } } } program.cs: namespaceCallBackSample ...{ classProgram ...{ staticvoidMain(string[]args) ...{ WorkClassworkClass =newWorkClass(); workClass.DoWork("Basketball"); } } }
The above is the detailed content of How to call callback function in nested DLL?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

Chris Titus Tech has a tool called Windows Utility that can help you easily create a debloated Windows 11/10 ISO to install a clean system. php.cn offers a full guide on how to do this thing using the Chris Titus tool.
