VB Pass String to VC Dll

VB Pass String to VC Dll

In VC DLL

  1. function 前不需要加入 __declspec(dllexport),要匯出的 function 名稱加入 .def 即可,避免 name mangling
  2. function 之 calling convertion 為 __stdcall
    ex. int __stdcall GetCPUSpeed()
  3. 使用.def 作出的 Dll,可被 VC 及 VB 程式呼叫


VB傳參數進 VC Dll


VB 呼叫 方式不變
In VC Dll 實作

BSTR __stdcall GetStrFromVC2(int A, char* strin)
{
   //do something
}
  • Call By Reference
    In VB,若 function 宣告為

            Private  Declare Function GetStrFromVC1 "DllDebugDll2.dll" ( ByRef PA as integer, ByRef str1 as String) As String

    呼叫
    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 實作
    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 "DllDebugDll2.dll" ( ByVal A as integer, ByVal strin as String) As String


結論 : 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所在行。

image

Dotblogs 的標籤: , ,