zoukankan      html  css  js  c++  java
  • 模拟DLL加载

    #include <stdio.h>
    #include <malloc.h>
    #include <sys/stat.h>
    typedef int (*PFUNC)(int,int);
    typedef struct
    {
        int Position;
        int Length;
    }Fun;
    
    typedef struct
    {
        int num;
        Fun *pFun;
    }Lib;
    
    int main()
    {
     
        int FileSize =0;
        char FileName[] = "code.obj";
        FILE * FileHandle = fopen(FileName,"rb");
        if(FileHandle!=NULL)
        {
            Lib lib;
            fread(&lib.num,sizeof(char),4,FileHandle);
            //确定文件中有几个函数
            lib.pFun =(Fun*)malloc(sizeof(Fun)*lib.num);
            fread(lib.pFun,sizeof(Fun),lib.num,FileHandle);
            //读取文件中第一个函数的信息,(在文件中的)地址和长度
            char *instruction= (char*)malloc(sizeof(char)*lib.pFun->Length);
            //根据函数在内存中为函数分配空间
            fseek(FileHandle,lib.pFun->Position,0);
            //移动至函数地址,以字节为单位,0代表文件第一个字节
            fread(instruction,sizeof(char),lib.pFun->Length,FileHandle);
            //读取函数指令到预留内存
            PFUNC pfunc = (PFUNC)instruction;
            printf("%d 
    ",pfunc(3,4));
            //执行函数
        }
    }
    
    /*
    说明:
    1)没有附带函数类型信息,如果支持函数类型信息,那么可以说这就是DLL文件
    2)读取文件时可以把文件想象成一个char A[m],有一个char的指针指向到这个
     数组,并在这个数组中前后移动,初始指到A[0],我们总是从这个指针指向的
     地址向后读取一定的字节复制到另一个数组,假设指针指向A[0],我们要读取
     10个字节,读取完之后,指针定位到A[10],当然,我们也可以借助fseek任意的
     移动指针
    size_t fread(void *buffer,size_t size,size_t count,FILE *stream)
    {
        int i=0;
        while(stream->ptr!=NULL && i<count)
        {
            buffer = stream->ptr;
            (stream->ptr)++;
        }
        return i;
    } // 假想的函数代码 
     
    int fseek( FILE *stream,long offset,int origin)
    {
        stream->ptr = offset + origin
    }
    */

    CODE.OBJ
    01 00 00 00 0C 00 00 00 2B 00 00 00 55 8B EC 81
    EC C0 00 00 00 53 56 57 8D BD 40 FF FF FF B9 30
    00 00 00 B8 CC CC CC CC F3 AB 8B 45 08 03 45 0C
    5F 5E 5B 8B E5 5D C3

  • 相关阅读:
    cocos2d-js 写日志log 查看日志log Android调试查看log
    嵌入式开发之hi3519---网络不通问题rmii
    嵌入式开发之视频压缩比---h264、mjpeg、mpeg4
    c、c++---linux上的GetTickCount函数
    嵌入式开发之hi3516---GV7601 SPI通信问题
    嵌入式开发之hisilicon---hi3536 处理器简介
    嵌入式开发之zynq---Zynq PS侧I2C驱动架构
    嵌入式开发值zynq---zynq中tlv320aic23b spi的驱动移植
    jumpserver 3.2修改排序规则
    haproxy 非常完整的配置
  • 原文地址:https://www.cnblogs.com/code-style/p/4602899.html
Copyright © 2011-2022 走看看