In VC DLL
- function 前不需要加入 __declspec(dllexport),要匯出的function名稱加入 .def 即可,避免name mangling
- function 之 calling convertion 為 __stdcall
ex. int __stdcall GetCPUSpeed() - 使用.def 作出的 Dll,可被 VC 及 VB 程式呼叫
VB 傳參數進 VC DLL
例一 : Call By Reference
In VB,若function宣告為
Private Declare Function GetStrFromVC1 "z:DllDebugDll2.dll" ( ByRef PA as integer, ByRef str1 as String) As String
呼叫
Visual Basic:
-
Dim strRet as String
-
Dim PA as Integer
-
Dim strOutStr as String
-
PA = 100
-
strRet = "abcdef12345"
-
strOutStr = GetStrFromVC1 (PA, strRet ) 'ByRef ,strRetStr會被改變
-
Debug.Print strOutStr
In VC Dll 實作
C++:
-
BSTR __stdcall GetStrFroVC1(int *PA, BSTR* pbstr)
-
{
-
char szTemp[1024];
-
sprintf(szTemp, "*** %s *** ", (char*) (*pbstr));
-
*PA = SysAllocStringLen(pbstr,(BSTR)szTemp, strlen(szTemp));
-
return SysAllocString((BSTR) "Test 123456");
-
}
例二 : Call By Value
In VB,若function宣告為
Private Declare Function GetStrFromVC2 "z:DllDebugDll2.dll" ( ByVal A as integer, ByVal strin as String) As String
VB 呼叫 方式不變
In VC Dll 實作
C++:
-
BSTR __stdcall GetStrFromVC2(int A, char* strin)
-
{
-
//do something
-
}
-
結論 : ByVal 與 ByRef對VC DLL內 function宣告之影響
- (1) ByVal -> char *
- (2) ByRef -> BSTR *
VB How to step into C++ dll ?
- 開啟VC Dll 專案
- 設定專案屬性 -> Debug Tab -> Execute for Debug Session -> 選擇 vb6.exe。
Program argument 設為 VB 之專案(.vbp) - VC Dll 專案內設好中斷點,按F5,則會啟動 VB6 IDE 並載入 VB 專案,此時可在VB設中斷點,當執行到呼叫VC function時,可 step into,否則就直接進入VC breakpoint所在行。
