c 呼叫dll函式,匯出類中的成員函式

2021-06-18 07:31:37 字數 2613 閱讀 4282

對於dll的操作,我們可以使用dumpbin檢視dll 和lib。

********注意寫好dll後 用depends檢視匯出函式時會發現匯出的函式名不是你寫的那樣出現所謂的亂碼入?等*************這就注定dll不能通過顯示連線的方式匯入。

如果想通過顯示連線的方式匯入可以為類新增乙個友元函式去實現顯示連線建立類得物件**************************************

這裡對於匯出類,我們使用隱式鏈結;直接看**;

/** 隱式鏈結,匯入lib是必須的*/

#pragma comment(lib,"tool.lib")

//要麼自己使用標頭檔案

//extern int add(int a,int b);

//_declspec(dllimport) int mul(int a,int b);

void candroiddialog::onbnclickedbuttondll()

str.format(_t("5+3=%d"),add(5,3));

messagebox(str);

//通過序號訪問

typedef int (*mulproc)(int a,int b);

mulproc mul=(mulproc)getprocaddress(hinst,reinterpret_cast(makeintresource(6)));

if(!mul)

str.format(_t("5*3=%d"),mul(5,3));

messagebox(str);

//訪問c++類中成員,dll到處類不能顯示鏈結,要使用隱式呼叫

//typedef ctool * (*getctool)();//定義函式指標,獲取類testdll物件

getctool getctool=(getctool)getprocaddress(hinst,"getctool");

if(!getctool)

ctool *tool=getctool();

str.format(_t("5-3=%d"),tool->sub(5,3));

messagebox(str);

str.format(_t("15/3=%d"),tool->div(15,3));

messagebox(str);

if(hinst)

}

另外附上dll工程檔案;

tool.h

// 下列 ifdef 塊是建立使從 dll 匯出更簡單的

// 巨集的標準方法。此 dll 中的所有檔案都是用命令列上定義的 tool_exports

// 符號編譯的。在使用此 dll 的

// 任何其他專案上不應定義此符號。這樣,原始檔中包含此檔案的任何其他專案都會將

// tool_api 函式視為是從 dll 匯入的,而此 dll 則將用此巨集定義的

// 符號視為是被匯出的。

#ifdef tool_exports

#define tool_api /**extern "c"*/ __declspec(dllexport)

#else

#define tool_api /**extern "c"*/ __declspec(dllimport)

#endif

//刪除tool.def檔案,專案=》屬性=》聯結器=》輸入=》模組定義檔案

// 此類是從 tool.dll 匯出的

class tool_api ctool ;

extern tool_api int ntool;

tool_api int /**_stdcall */ fntool(void);//這裡可以修改呼叫約定,包括_stdcall(winapi),pascal,__cdecl __fastcall

tool_api int add(int a,int b);

tool_api int mul(int a,int b);

extern "c" tool_api ctool* getctool(); //獲取類testdll的物件

tool.cpp

// tool.cpp : 定義 dll 應用程式的匯出函式。

//#include "stdafx.h"

#include "tool.h"

// 這是匯出變數的乙個示例

tool_api int ntool=0;

// 這是匯出函式的乙個示例。

tool_api int fntool(void)

// 這是已匯出類的建構函式。

// 有關類定義的資訊,請參閱 tool.h

ctool::ctool()

int ctool::sub(int a,int b)

int ctool::div(int a,int b)

tool_api int add(int a,int b)

tool_api int mul(int a,int b)

//常規的dll是不支援類的匯出的.只支援c風格的函式的匯出.要弄mfc dll工程才行

extern "c" tool_api ctool *getctool()

DLL匯出類和匯出函式

1 動態庫dll中的類或者函式有時候要被其他的庫呼叫,因此需要被其他庫呼叫的類或者函式需要進行匯出。2 首先編寫需要匯出的dll,新建乙個工程設定應用程式型別為dll 3 類的 如下 標頭檔案 pragma once define ex port declspec dllexport using n...

C 的dll匯出類

首先建乙個dll 專案 然後做乙個公用的標頭檔案 export.h ifndef export h define export h struct icalculator endif 這個標頭檔案封轉乙個基類 然後再dll模組中做乙個子類,繼承於這個基類 calculator.h ifndef cal...

c 類的成員函式的呼叫

我知道c 有乙個虛函式列表,物件有指向虛函式列表的指標,對於有虛函式的類物件呼叫虛函式時通過虛函式列表裡面的指標實現的。但是對於普通的非虛函式是怎麼實現呼叫的呢 是怎麼找到非虛函式的入口位址?好像類物件裡面只有類的一些資料成員。求解惑 求詳細的記憶體層次的變化 2013 09 10 20 39 提問...