• 回调函数(C/C++)


    回调函数,就是自己写一个函数自己不调用,而是其他程序调用,下面举例exe调用dll函数,反过来,dll也调用exe函数,模拟杀毒软件查杀调用。

    exe代码:

    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    #define  Log(str) {{printf("[Kill]%s
    ",str);}}
    typedef void(*pFunc)(DWORD addrFunc,char * msg);
    typedef bool(*pFuncScan)();
    
    pFunc dllCallBack;
    pFuncScan dllScan;
    
    void showResult(char * msg)
    {
        Log(msg);
    }
    
    int main(int argc,char * argv[])
    {
        HMODULE hDll = LoadLibrary("CallBackDll.dll");
    
        if (!hDll)
        {
            Log("LoadLibrary Failed");
            return 0;
        }
    
        dllCallBack = (pFunc)GetProcAddress(hDll, "RegCallBack");
    
        if (!dllCallBack)
        {
            Log("GetProcAddress Failed");
            return 0;
        }
    
        dllScan = (pFuncScan)GetProcAddress(hDll, "ScanVirus");
        dllCallBack((DWORD)showResult, "Main Virus");
    
        if (dllScan())
        {
            Log("Main Scan OK");
        }
    
        system("pause");
        return 0;
    }

    dll 代码:

    #include <windows.h>
    #include <stdio.h>
    
    //Log Print
    #define  Log(str) {{printf("[Kill]%s
    ",str);}}
    
    //Function Pointer
    typedef void (* pFunc)(char * argument);
    char * argument = NULL;
    
    pFunc callback;
    
    //Register CallBack Function
    extern "C" __declspec(dllexport) void  RegCallBack(DWORD addrFunc,char * msg)
    {
        callback = (pFunc)addrFunc;
        argument = msg;
    }
    
    //Real Scan(Real Kill Function)
    bool ScanMain()
    {
        Log("Virus....");
        Sleep(1000);
        return true;
    }
    
    //Scan Show
    extern "C" __declspec(dllexport) bool ScanVirus()
    {
        Log("Scanning Begin...");
        Sleep(3000);
    
        if (ScanMain())
        {
            callback(argument);
            Sleep(1000);
        }
    
        Log("Scanning End...");
    
        return TRUE;
    }
    
    //Dll Main
    BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
    {
        switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
        case DLL_PROCESS_DETACH:
            break;
        }
    
        return TRUE;
    }
  • 相关阅读:
    【Java】String和Date、Timestamp之间的转换
    pl/sql改为汉语窗口的办法
    MySql生成随机数
    ETL概念
    oracle数据库中序列使用讲解
    oracle中的merge into用法解析
    mysql实现自动更新时间戳
    3.6:Linux touch命令:修改文件的时间戳
    3.5:Linux rmdir命令:删除空目录
    3.4:Linux mkdir命令:创建目录(文件夹)
  • 原文地址:https://www.cnblogs.com/DeeLMind/p/7595549.html
Copyright © 2020-2023  润新知