VB調用VC dll的返回方式

第一種類型:數值傳遞
注意:在VB中,默認變量傳遞方式爲ByRef爲地址,而傳遞值就是用ByVal,還要注意在C++中,
int類型的變量是32位的,在VB中要用long型變量來配合。
VC++部分:css

[cpp]  view plain copy
 
  1. extern "C" _declspec(dllexport) int __stdcall TestCalc(int source)  
  2. {  
  3.     //AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  4.     return(++source);  
  5. }  



.def文件
EXPORTS TestCalc數組

 

 

VB部分
聲明:函數

[vb]  view plain copy
 
  1. Private Declare Function TestCalc Lib "Dll.dll" (ByVal Source As Long) As Long  


調用:spa

[vb]  view plain copy
 
  1. Dim Tint As Long  
  2. Tint = TestCalc(45)  
  3. MsgBox Tint, vbExclamation   





第二種類型:傳遞字符串,主要用於字符串返回或者處理。.net

 

 

VC++部分:指針

[cpp]  view plain copy
 
  1. extern "C" _declspec(dllexport) int __stdcall MidStr(CHAR * src,CHAR * dest)  
  2. {  
  3.     //AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  4.     strcpy(dest,src+1);  
  5.     return 0;  
  6. }  



.def文件
EXPORTS MidStrblog

 


VB部分:
聲明:ip

[vb]  view plain copy
 
  1. Private Declare Function MidStr Lib "Dll.dll" (ByVal src As String, ByVal dest As String) As Long  


調用:ci

[vb]  view plain copy
 
  1. Dim i As Long, s As String * 255  
  2. tempstr = "Hello!World"  
  3. i = MidStr(tempstr, s)  或者 i = MidStr("Hello!World", s)  
  4. MsgBox s, vbExclamation   



第三種類型:傳遞數組和變量指針,主要用於從dll中讀出大量數據字符串

 


VC++部分:

[css]  view plain copy
 
  1. extern "C" _declspec(dllexport) int __stdcall TestByte(BYTE *p,int *length)  
  2. {  
  3.     //AFX_MANAGE_STATE(AfxGetStaticModuleState());  
  4.     *p=45;  
  5.     *(p+1)=46;  
  6.     *length=2;  
  7.     return 0;  
  8. }  



.def文件
EXPORTS TestByte

VB部分
聲明:

[vb]  view plain copy
 
  1. Private Declare Function TestByte Lib "Dll.dll" (ByRef src As Any, ByRef length As Long) As Long  


調用:

[vb]  view plain copy
 
  1. Dim a(0 To 10) As Byte  
  2. Dim i As Integer, length As Long  
  3. i = TestByte(a(0), length)  
  4. MsgBox a(0) & " " & a(1) & vbCrLf & length, vbExclamation   


第四種類型:傳遞字符串數組 
    
一、VB TO VC :
 
 VC部分:
     

[cpp]  view plain copy
 
  1. extern "C" _declspec(dllexport) int WINAPI StringArray(LPSAFEARRAY *VbArray)  
  2.       {  
  3.           DWORD i;  
  4.           BSTR bSTR;    // UNICODE 字符串  
  5.           LPSAFEARRAY pSa;  
  6.           SAFEARRAYBOUND iBound;  
  7.        
  8.           char *arry[10];  
  9.            
  10.           for(i = 0;i < 10;i++)  
  11.           {  
  12.               arry[i] = "A";  
  13.           }  
  14.        
  15.           iBound.lLbound = 0;    // 數組起始位  
  16.           iBound.cElements = 10;    // 數據長度  
  17.            
  18.           // SafeArray描述符  
  19.           if (*VbArray == NULL)  
  20.           {  
  21.               if ((pSa = SafeArrayCreate(VT_BSTR,1,&iBound)) == NULL)  // 建立SafeArray描述符  
  22.               {  
  23.                   return FALSE;  
  24.               }  
  25.               *VbArray = pSa;                // 返回SafeArray描述符  
  26.           }  
  27.           else   
  28.           {   
  29.               if ((*VbArray)->cDims != 1)        // 釋放爲一維數組        
  30.                   return FALSE;       
  31.           }   
  32.        
  33.           for (i = iBound.lLbound;i < iBound.cElements;i++)  
  34.           {  
  35.               bSTR = SysAllocString((BSTR)arry[i]);  
  36.                
  37.               //     if(FAILED(safeArrayGetElement(*VbArray,(long*)&i,&bSTR))) // 從VbArray數組讀取數據  
  38.               //{  
  39.               //    return FALSE;  
  40.               //}  
  41.               // 放數組元素到VbArray數組  
  42.                
  43.               if(FAILED(safeArrayPutElement(*VbArray,(long*)&i,bSTR)))  
  44.               {  
  45.                   return FALSE;  
  46.               }  
  47.               SysFreeString(bSTR); // 釋放空間  
  48.        
  49.           }  
  50.           return 1;  
  51.       }  


        
VB 部分:
聲明:

 

 

[vb]  view plain copy
 
  1. Public Declare Function StringArray Lib "xxx.DLL" (byval s() As String) As Integer  

 

[vb]  view plain copy
 
  1. Sub StringArrayTest()  
  2.       Dim s()     As String  
  3.             
  4.         tmp = StringArray(s)  
  5.         Debug.Print s(0)  
  6. End Sub  



二、VB TO VC

VB的字符串數組是由BSTR組成的SafeArray類型,因此VB裏DLL函數如此聲明:
Private Declare FunctionMyFun Lib "MyDll" (ByVal strarr As Variant) As Long

創建MFC DLL工程,名爲 ShowVBStrArr 編譯生成 ShowVBStrArr.DLL
DLL函數原形:

[cpp]  view plain copy
 
  1. extern "C" BOOL __stdcall ShowVBStrArray(VARIANT VBpStrArray)  
  2. {  
  3. SAFEARRAY FAR *pStrArrTemp = NULL;  
  4. long LBound;  
  5. long UBound;  
  6. BSTR HUGEP *pbstr;  
  7. CString strtemp;  
  8. if(V_VT(&VBpStrArray) != (VT_ARRAY | VT_BSTR))//判斷是否爲字符數組  
  9. return FALSE;  
  10. pStrArrTemp = V_ARRAY(&VBpStrArray);  
  11. if (SafeArrayGetDim(pStrArrTemp)!=1)//判斷是否爲一維數組  
  12. return FALSE;  
  13. SafeArrayGetLBound(pStrArrTemp,1,&LBound);  
  14. SafeArrayGetUBound(pStrArrTemp,1,&UBound);  
  15. SafeArrayAccessData(pStrArrTemp, (void HUGEP* FAR*)&pbstr);  
  16. for (int i=0;i<(UBound-LBound)+1;i++)  
  17. strtemp+=LPWSTR(pbstr);  
  18. MessageBox( 0,strtemp,"結果",MB_OK);  
  19. SafeArrayUnaccessData(pStrArrTemp);  
  20. return TRUE;  
  21. }  



在DLL工程的def文件裏編輯以下:
EXPORTS
ShowVBStrArray


VB源碼:
Option Explicit
Private Declare Function ShowVBStrArray Lib "xxx.dll" (ByVal pstr As Variant) As Long

Private Sub Command1_Click()
Dim prompt(1) As String
prompt(0) = "Hello"
prompt(1) = "World"

ShowVBStrArray prompt
End Sub 

 

 

第五種 傳結構體

因爲須要根據需求向DLL中傳入多種值或者須要從DLL中返回多種數據,均可以傳結構體,不過得注意VB和VC的類型對應。具體操做以下: VC部分:

聲明:

 

[cpp]  view plain copy
 
  1. extern "C" _declspec(dllexport) BOOL WINAPI cPowerAlarm(PowerAlarm* tagPower,PowerResult* tagResult)  

 

 結構體定義:

 

[cpp]  view plain copy
 
  1. // 電源報警模塊 參數結構體  
  2. typedef struct tagPowerAlarm  
  3. {  
  4.     char* strSIM;       // SIM 卡號     
  5.     char* cStartTime;   // 開始時間  
  6.     char* cEndTime;     // 終止時間  
  7. }PowerAlarm;  
  8. // 電源報警模塊 返回結果結構體  
  9. typedef struct tagPowerResult  
  10. {  
  11.     char cResultCH[20]; // 充電狀況判斷  
  12.     char cResultQuality[20]; // 電池品質判斷  
  13.     char cResultHV[20]; // 過充判斷  
  14.     char cResultLV[20]; // 欠壓判斷  
  15.       
  16. }PowerResult;  

 

 VB部分:

聲明:

 

[vb]  view plain copy
 
  1. Public Declare Function cPowerAlarm Lib "DataDiagnose.DLL" (ByRef myPower As h_PowerAlarm, ByRef myPowerResult As h_PowerResult) As Integer  

 

結構體定義:

 

[vb]  view plain copy
 
  1. ' 電源報警模塊 參數結構體  
  2. Public Type h_PowerAlarm  
  3.     strSIM     As String        ' SIM 卡號  
  4.     strStartTime As String      ' 開始時間  
  5.     strEndTime   As String      ' 終止時間  
  6. End Type  
  7. ' 電源報警模塊 返回結果結構體  
  8. Public Type h_PowerResult  
  9.     strResultCH As String * 20      ' 充電狀況判斷  
  10.     strResultQuality As String * 20 ' 電池品質判斷  
  11.     strResultHV As String * 20      ' 過充判斷  
  12.     strResultLV As String * 20      ' 欠壓判斷  
  13. End Type  
相關文章
相關標籤/搜索