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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章