EvoTalk

29 六月, 2006

VB Pass String to VC Dll

Posted by: asd In: C++| Code Snippet| 程式設計 ()

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

例一 : 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:
  1. Dim strRet as String
  2. Dim PA as Integer
  3. Dim strOutStr as String
  4. PA  = 100
  5. strRet = "abcdef12345"
  6. strOutStr = GetStrFromVC1 (PA, strRet )  'ByRef ,strRetStr會被改變
  7. Debug.Print strOutStr

In VC Dll 實作

C++:
  1. BSTR __stdcall GetStrFroVC1(int *PA, BSTR* pbstr)
  2. {
  3. char szTemp[1024];
  4. sprintf(szTemp, "*** %s *** ", (char*) (*pbstr));
  5. *PA = SysAllocStringLen(pbstr,(BSTR)szTemp, strlen(szTemp));
  6. return SysAllocString((BSTR) "Test 123456");
  7. }

例二 : 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++:
  1. BSTR __stdcall GetStrFromVC2(int A, char* strin)
  2. {
  3. //do something
  4. }

結論 : ByVal 與 ByRef對VC DLL內 function宣告之影響
(1) ByVal -> char *

(2) ByRef -> BSTR *
VB How to step into C++ dll ?
  1. 開啟VC Dll 專案
  2. 設定專案屬性 -> Debug Tab -> Execute for Debug Session -> 選擇 vb6.exe。
    Program argument 設為 VB 之專案(.vbp)
  3. VC Dll 專案內設好中斷點,按F5,則會啟動 VB6 IDE 並載入 VB 專案,此時可在VB設中斷點,當執行到呼叫VC function時,可 step into,否則就直接進入VC breakpoint所在行。 Debug VC Dll
Tags: , ,

Releated Posts



No Responses to "VB Pass String to VC Dll"

Comment Form