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;
}
}
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!