DELPHI呼叫VC寫的DLL經驗

2021-05-12 13:41:19 字數 2359 閱讀 7469

由於專案的需要,必須使用delphi呼叫vc編寫的dll,而我是編寫dll的。

這裡總結幾個出現過的問題,以後再遇到就會用了。

一、需要在每乙個輸出的函式開始新增上

afx_manage_state(afxgetstaticmodulestate())這是用來實現dll資源的切換!

讓我們來看看,這句**到底做了什麼?

#define afx_manage_state(p) afx_maintain_state2 _ctlstate(p);

afx_maintain_state2::afx_maintain_state2(afx_module_state* pnewstate)

_afxwin_inline afx_maintain_state2::~afx_maintain_state2()

原來,就是定義乙個區域性的物件,利用其構造和析構函式在函式的入口和函式的出口進行state狀態的切換,我猜afxgetstaticmodulestate()一定是獲取當前**所在dll的state。

果然,請看

static _afx_dll_module_state afxmodulestate;

afx_module_state* afxapi afxgetstaticmodulestate()

class _afx_dll_module_state : public afx_module_state

...coccmanager* m_poccmanager;

...這裡不得不說,mfc把很多的資料都堆放在這裡,搞得很複雜,結構性非常的差。

}afxmodulestate是dll的靜態成員,自然可以被同樣的dll裡的**所訪問,但是何時初始化的?

extern "c"

bool winapi dllmain(hinstance hinstance, dword dwreason, lpvoid /*lpreserved*/)

bool afxapi afxwininit(hinstance hinstance, hinstance hprevinstance,

lptstr lpcmdline, int ncmdshow)

_afxwin_inline hinstance afxapi afxgetresourcehandle()

#define afxcurrentresourcehandle    afxgetmodulestate()->m_hcurrentresourcehandle

關 於資源裝載,問題似乎已經解決了,但是還有一點點小麻煩就是,我實現的dll不是以普通的輸出函式進行輸出的,而是輸出類,我可不想在每乙個類的成員函式 裡新增afx_manage_state(afxgetstaticmodulestate())。怎麼辦呢?既然已經知道了資源切換的原理,我們新增兩 個輸出函式,分別對應afx_maintain_state2的構造和析構函式,在類的使用前後呼叫,就可以了。或者,分別放在類的構造和析構函式裡。又 或者,就宣告為成員變數。無論怎樣,需要保證的一點就是資源的切換要正確巢狀,不可交叉--這種情況在不同的dll之間交叉呼叫的時候會發生。

好 了,現在dll裡的資源可以正確呼叫了,但是在當dialog上包含有ie控制項的時候,我們還是失敗了,為什麼呢?我知道對於activex控制項, dialog需要做一些特殊的處理,afxenablecontrolcontainer(),我也知道,要使用com,需要coinitialize (),但是我一直沒有想過需要兩個一起用才能把ie弄出來,但是最後就是這樣的。奇怪的是,如果不是在工作執行緒裡,根本不需要coinitialize (),就能裝載ie控制項的,這個暫時就先不管了。

process_local(coccmanager, _afxoccmanager)

void afx_cdecl afxenablecontrolcontainer(coccmanager* poccmanager)

#define afxoccmanager   afxgetmodulestate()->m_poccmanager

這 樣看來,這個_afxoccmanager應該是屬於整個程序的,整個程序只有乙個,就在那個定義它的dll裡。但是,你需要把該物件(或者建立乙個自定 義的)傳給modulestate(請注意前面的afx_module_state裡就包含了該屬性),也就是要 afxenablecontrolcontainer()一下,這樣特定的modulestate就有了occmanager的資訊!但是,請注意,一定 要在目標dll裡,正確切換了資源之後,才能進行,如下:

afx_manage_state(afxgetstaticmodulestate());

coinitialize(null);

afxenablecontrolcontainer();

二、如果delphi呼叫vc寫的dll裡函式有引數時,要記得在函式名前面返回值後面新增winapi

Delphi 呼叫VC生成的DLL

1.在vc中新建乙個 dll 工程。寫如 如 extern c int npn int m,int n 編譯生成 dll 檔案。2。在delphi 中申明 function getpplength integer cdecl external dll.dll function npn m integ...

Delphi 呼叫VC生成的DLL

1.在vc中新建乙個 dll 工程。寫如 如 extern c int npn int m,int n 編譯生成 dll 檔案。2。在delphi 中申明 function getpplength integer cdecl external dll.dll function npn m integ...

Delphi呼叫VC編譯的DLL

delphi呼叫vc編譯的dll delphi windows sdk api 嘗試用delphi呼叫vc編譯的dll,dll介面函式中定義一般型別的引數時,可以正常呼叫,但是當在介面函式中定義 函式時,在delphi中將函式指標傳入介面函式時總是報錯,用delphi建立同樣功能的dll,就沒有問題...