C++ dll调用-动态(显式)

 C++ dll调用-动态(显式)

废话不说上代码, 
dll 头文件 j_test.h
#pragma once
extern "C"_declspec(dllexport) void maopao(int *p, int count);
extern "C"_declspec(dllexport) int test(int *p, char* count);
extern "C"_declspec(dllexport) int sum(int  i1,  int  i2);
class j_test
{

public:
    j_test();
    ~j_test();
    void maopao(int *p, int count);
    int sum(int  i1, int  i2);
    int test(int *p, char* count);
};

dll 实现类  j_test.cpp

#include "j_test.h"


j_test::j_test()
{
}


j_test::~j_test()
{
}

 
void maopao(int *p, int count)
{
    int temp = 0;
    for (int i = 1; i<count; i++)
    {
        for (int j = count - 1; j >= i; j--)
        {
            if (p[j]>p[j - 1])
            {
                temp = p[j];
                p[j] = p[j - 1];
                p[j - 1] = temp;
            }
        }
    }
}

int test(int *p, char* count){
    count[0] = (char)p[0];
    count[1] = (char)p[1];
    return 0;
}
int sum(int  i1, int  i2){

    return i1 + i2;
}

调用方 头文件

goo.h
#pragma once
class goo
{
public:
    goo();
    ~goo();
    void main(void);
};
goo.cpp
#include "goo.h"


goo::goo()
{
}


goo::~goo()
{
}
 
#include<iostream>
#include<Windows.h>
#include<time.h>
typedef int(*Dllfun)(int *, int);
typedef int(*TestFun)(int *, char *);
typedef int(*SumFun)(int  , int  );
using namespace std;
int main()
{
    Dllfun maopao1;
    HINSTANCE hdll;
    hdll = LoadLibrary("F:/workspaces/visual studio 2013/Projects/project-1/Project1/Debug/Project1.dll");
    if (hdll == NULL)
    {
        FreeLibrary(hdll);
    }
    maopao1 = (Dllfun)GetProcAddress(hdll, "maopao");
    if (maopao1 == NULL)
    {
        FreeLibrary(hdll);
    }
    int a[10];
    srand(time(0));
    for (int i = 0; i<10; i++)
        a[i] = rand() % 50;
    maopao1(a, 10);
    for (int i = 0; i<10; i++)
        cout << a[i] << endl;
    int b;
    char cs[10];
    TestFun testFun = (TestFun)GetProcAddress(hdll, "test");
    b = testFun(a, cs);
    cout << "cs = .............."  << endl;
    for (int i = 0; i<10; i++)
        cout << cs[i] << endl;
    cout << "b=" << b << endl;

    cout << "sumFun = .............." << endl;
    SumFun  sumFun = (SumFun)GetProcAddress(hdll, "sum");
    b = sumFun(3, 4);
    cout << "b=" << b << endl;





    cin >>  b;
    
    FreeLibrary(hdll);

}

C++如何调用DLL呢,有两种,一种是静态,另外一种是动态,即通过调用windowsAPI 来加载和卸载DLL,具体思路:

1.先编写一个DLL, 可以没有单独的头文件,因为很多情况下的DLL都是没有和lib和头文件一起的。

2.然后另外新建一个项目,来调用DLL,方法是:

1.声明头文件<windows.h>,说明我想用windows32方法来加载和卸载DLL

2.然后用typedef定义一个指针函数类型.typedef  void(*fun) //这个指针类型,要和你调用的函数类型和参数保持一致,记住,是指针参数就是(int *,int)

3.定一个句柄实例,用来取DLL的实例地址。HINSTANCE hdll;

格式为hdll=LoadLibrary(“DLL地址”);这里字符串类型是LPSTR,当是unicode字符集的时候会不行,因此要在配置-属性-常规里面把默认字符集“unicode”改成支持多字符扩展即可。

4.取的地址要判断,返回的句柄是否为空,如果为无效句柄,那么要释放加载DLL所占用的内存。

FreeLibrary(hdll);

5.然后定义一个函数指针,用来获取你要用的函数地址,这个咋用呢?

先是定一个函数指针 fun FUN;然后通过GetProcAdress来获取函数的地址,这个函数参数是什么呢?

参数是DLL的句柄和你要调用的函数名:比如:FUN=(fun)GetProcAdress(hdll,"sum");

这里也要判断要函数指针是否为空,如果没取到要求的函数,那么要释放句柄

FreeLibrary(hdll);

6.然后通过函数指针来调用函数。

FUN(int *p,int count);这里不能用函数名来使用函数,因为这个DLL本身不是当前CPP的一部分,而是通过windows去调用.没有在这个工程里声明或者定义,而是暴露出一个头,要指针获取他的地址,通过指针来调用.

最后调用结束后,就释放句柄

FreeLibrary(hdll);

这里只是通过动态加载没有涉及到静态的。这个在后续会学习。

 

来自 :   http://www.cnblogs.com/lhbssc/archive/2012/02/08/2342853.html

原文地址:https://www.cnblogs.com/mjorcen/p/3935306.html