VS2013/VS2015生成动态链接库DLL、调用DLL

  • 新建Win32工程,选择类型为DLL,新建空项目。。。

  • 头文件写法(关键)

// jiarenyf.h
extern "C" {
    __declspec(dllexport) void Method(int jiarenyf);
}
  • 源文件写法:
// jiarenyf.cpp
#include "jiarenyf.h"

void Method(int jiarenyf)
{
    // 随便写...
}
  • 编译即可生成DLL

  • 调用DLL
#include "windows.h "

#include <iostream>
#include <string>
using namespace std;

int main()
{
    HINSTANCE hInstance = LoadLibrary(TEXT("*.dll"));
    if (!hInstance)
    {
        cout << "DLL加载失败..." << endl;
        FreeLibrary(hInstance);
        system("PAUSE");
        return -1;
    }

    typedef void* (*Method) (int jiarenyf);
    Method method = (Method)GetProcAddress(hInstance, "Method");
    if (!method)
    {
        cout << "函数Method加载失败..." << endl;
        system("PAUSE");
        FreeLibrary(hInstance);
        return -1;
    }
    else
    {
        //调用函数
        method(123);
    }

    system("PAUSE");
    //FreeLibrary(hInstance);

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章