Friday, August 25, 2006

Using DLL between BCB and VC++

◎蕭沖原創

僅有cdecl這種convention配合C的函式編碼(使用extern"C")才可以互用。cpp的overloading function,class等都是無法互通的!

BCB對於cdecl convention的function是會在function前面加上 _ 底線。
BCB對於stdcall 的部份則是沒加 _ 。
VC的部份則是相反。

__________________________________________________________________
呼 叫 慣 例 原 始 函 式 Borland C++Builder Microsoft Visual C++
__cdecl MyFunc_cdcel _MyFunc_cdcel MyFunc_cdcel
__stdcall MyFunc_std MyFunc_std _MyFunc_std@8
__fastcall MyFunc_fast @MyFunc_fast @MyFunc_fast@8
-----------------------------------------------------------------------


所以在製作for BCB lib from VC DLL時需要用下面的方法(針對cdcel的部份):
implib -a -c -f xxx.lib xxx.dll ::a switch force add _ alias to MS cdecl

或者可由impdef程式產生的DEF檔觀察,並進一步修正DEF檔,然後用個自的lib工具來製作lib檔。
若從VC.DLL來產生def檔,則會看到都沒有底線。此時為了要給BCB使用,則需要將dll中屬於cdecl的部份自己加上 _ 底線的alias(=號左邊),再用implib產生LIB檔,以"騙"BCB正常呼叫,但實則對應到沒有底線的DLL檔。相反的,若從BCB.DLL來產生def則會發現cdecl的部份都有 _ 底線,為了要給VC用,則要自行去底線,再由VC自帶的lib /DEF:xxx.def來產生。

PS 為了仰制name mangling,一定要使用extern "C",這樣才可以交互使用。

注意! BCB的DEF檔的alias與VC的DEF檔的alias方向似乎不同:
BCB : link phrase matching = name of the function that is exported
VC : name of the function that is exported = link phrase matching

No comments: