码迷,mamicode.com
首页 > 编程语言 > 详细

C/C++ 动态链接库

时间:2018-08-08 00:55:46      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:_stdcall   lex   动态链接   http   nbsp   roc   fun   null   delete   


导出函数1 

extern "C" __declspec(dllexport) int fn__declspec(int x, int y);

 


导出函数2

int __stdcall fn_def(int x, int y);
//.def
LIBRARY EXPORTS ; entryname[
=internalname] [@ordinal [NONAME]] [PRIVATE] [DATA] fn_def

 


导出类1

class __declspec(dllexport) CExportClass
{
public:
    CExportClass();
    ~CExportClass();

    int Test() { return 1; };
};

 


 

导出类2

//IExport.h
class IExport
{
public:
    virtual int Test() = 0;  //功能测试。
    virtual void Release() = 0;  //析构。
};
//CExport.h
#include "IExport.h"

class CExport : public IExport
{
public:
    virtual int Test();  //功能测试。
    virtual void Release();  //析构。delete this;
};
#include "IExport.h"
#include "CExport.h"

extern "C" __declspec(dllexport) IExport* __stdcall CreateExportObj();
extern "C" __declspec(dllexport) void __stdcall DestroyExportObj(IExport* pExport);
//Functions.cpp
#include "stdafx.h"
#include "Functions.h"

IExport* __stdcall CreateExportObj()
{
    return new CExport();
}

void  __stdcall DestroyExportObj(IExport* pExport)
{
    pExport->Release();
}
//.def
LIBRARY

EXPORTS
    ; entryname[=internalname]   [@ordinal   [NONAME]]   [PRIVATE]   [DATA] 
    CreateExportObj
    DestroyExportObj

 


 调用

//隐式调用函数
#include "../Win32DLLExportFunc/ExportFunc.h"
#pragma comment(lib, "../Debug/Win32DLLExportFunc.lib")
void CallDllFunc1()
{
    printf("fn__declspec:%d\r\n", fn__declspec(1, 2));
    printf("fn_def:%d\r\n", fn_def(1, 2));
}

//显式调用函数
#include <wtypes.h>
void CallDllFunc2()
{
    HINSTANCE hInst = LoadLibrary(_T("../Debug/Win32DLLExportFunc.dll"));
    if (hInst)
    {
        typedef int (*FN__DECLSPEC)(int x, int y);
        FN__DECLSPEC fn__declspec = (FN__DECLSPEC)GetProcAddress(hInst, "fn__declspec");
        printf("FN__DECLSPEC:%d\r\n", fn__declspec(1, 2));
        typedef int(__stdcall *FN_DEF)(int x, int y);
        FN_DEF fn_def = (FN_DEF)GetProcAddress(hInst, "fn_def");
        printf("FN_DEF:%d\r\n", fn_def(1, 2));
        FreeLibrary(hInst);
        hInst = NULL;
    }
}

//隐式调用类1
#include "../Win32DLLExportClass1/CExportClass.h"
#pragma comment(lib, "../Debug/Win32DLLExportClass1.lib")
void CallDllClass1()
{
    CExportClass obj = CExportClass();
    printf("CExportClass:%d\r\n", obj.Test());
}

//隐式调用类2
#include "../Win32DLLExportClass2/IExport.h"
#include "../Win32DLLExportClass2/Functions.h"
#pragma comment(lib, "../Debug/Win32DLLExportClass2.lib")
void CallDllClass2()
{
    IExport* pExport = CreateExportObj();
    printf("IExport:%d\r\n", pExport->Test());
    DestroyExportObj(pExport);
}

//显式调用类
void CallDllClass3()
{
    HINSTANCE hInst = LoadLibrary(_T("../Debug/Win32DLLExportClass2.dll"));
    if (hInst)
    {
        typedef IExport* (__stdcall *CREATEEXPORTOBJ)();
        CREATEEXPORTOBJ CreateExportObj = (CREATEEXPORTOBJ)GetProcAddress(hInst, "CreateExportObj");
        IExport* pExport = CreateExportObj();
        printf("IExport:%d\r\n", pExport->Test());
        typedef void(__stdcall *DESTROYEXPORTOBJ)(IExport* pExport);
        DESTROYEXPORTOBJ DestroyExportObj = (DESTROYEXPORTOBJ)GetProcAddress(hInst, "DestroyExportObj");
        DestroyExportObj(pExport);
        pExport = NULL;
        FreeLibrary(hInst);
        hInst = NULL;
    }
}

int main()
{
    CallDllFunc1();
    CallDllFunc2();
    CallDllClass1();
    CallDllClass2();
    CallDllClass3();

    return 0;
}

 


https://github.com/Neverever00544/Dynamic-link-library-demo

C/C++ 动态链接库

标签:_stdcall   lex   动态链接   http   nbsp   roc   fun   null   delete   

原文地址:https://www.cnblogs.com/dailycode/p/9440459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!