第一種類型:數值傳遞
注意:在VB中,默認變量傳遞方式爲ByRef爲地址,而傳遞值就是用ByVal,還要注意在C++中,
int類型的變量是32位的,在VB中要用long型變量來配合。
VC++部分:css
- extern "C" _declspec(dllexport) int __stdcall TestCalc(int source)
- {
-
- return(++source);
- }
.def文件
EXPORTS TestCalc數組
VB部分
聲明:函數
- Private Declare Function TestCalc Lib "Dll.dll" (ByVal Source As Long) As Long
調用:spa
- Dim Tint As Long
- Tint = TestCalc(45)
- MsgBox Tint, vbExclamation
第二種類型:傳遞字符串,主要用於字符串返回或者處理。.net
VC++部分:指針
- extern "C" _declspec(dllexport) int __stdcall MidStr(CHAR * src,CHAR * dest)
- {
-
- strcpy(dest,src+1);
- return 0;
- }
.def文件
EXPORTS MidStrblog
VB部分:
聲明:ip
- Private Declare Function MidStr Lib "Dll.dll" (ByVal src As String, ByVal dest As String) As Long
調用:ci
- Dim i As Long, s As String * 255
- tempstr = "Hello!World"
- i = MidStr(tempstr, s) 或者 i = MidStr("Hello!World", s)
- MsgBox s, vbExclamation
第三種類型:傳遞數組和變量指針,主要用於從dll中讀出大量數據字符串
VC++部分:
- extern "C" _declspec(dllexport) int __stdcall TestByte(BYTE *p,int *length)
- {
- //AFX_MANAGE_STATE(AfxGetStaticModuleState());
- *p=45;
- *(p+1)=46;
- *length=2;
- return 0;
- }
.def文件
EXPORTS TestByte
VB部分
聲明:
- Private Declare Function TestByte Lib "Dll.dll" (ByRef src As Any, ByRef length As Long) As Long
調用:
- Dim a(0 To 10) As Byte
- Dim i As Integer, length As Long
- i = TestByte(a(0), length)
- MsgBox a(0) & " " & a(1) & vbCrLf & length, vbExclamation
第四種類型:傳遞字符串數組
一、VB TO VC :
VC部分:
- extern "C" _declspec(dllexport) int WINAPI StringArray(LPSAFEARRAY *VbArray)
- {
- DWORD i;
- BSTR bSTR;
- LPSAFEARRAY pSa;
- SAFEARRAYBOUND iBound;
-
- char *arry[10];
-
- for(i = 0;i < 10;i++)
- {
- arry[i] = "A";
- }
-
- iBound.lLbound = 0;
- iBound.cElements = 10;
-
-
- if (*VbArray == NULL)
- {
- if ((pSa = SafeArrayCreate(VT_BSTR,1,&iBound)) == NULL)
- {
- return FALSE;
- }
- *VbArray = pSa;
- }
- else
- {
- if ((*VbArray)->cDims != 1)
- return FALSE;
- }
-
- for (i = iBound.lLbound;i < iBound.cElements;i++)
- {
- bSTR = SysAllocString((BSTR)arry[i]);
-
-
-
-
-
-
-
- if(FAILED(safeArrayPutElement(*VbArray,(long*)&i,bSTR)))
- {
- return FALSE;
- }
- SysFreeString(bSTR);
-
- }
- return 1;
- }
VB 部分:
聲明:
- Public Declare Function StringArray Lib "xxx.DLL" (byval s() As String) As Integer
- Sub StringArrayTest()
- Dim s() As String
-
- tmp = StringArray(s)
- Debug.Print s(0)
- 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函數原形:
- extern "C" BOOL __stdcall ShowVBStrArray(VARIANT VBpStrArray)
- {
- SAFEARRAY FAR *pStrArrTemp = NULL;
- long LBound;
- long UBound;
- BSTR HUGEP *pbstr;
- CString strtemp;
- if(V_VT(&VBpStrArray) != (VT_ARRAY | VT_BSTR))
- return FALSE;
- pStrArrTemp = V_ARRAY(&VBpStrArray);
- if (SafeArrayGetDim(pStrArrTemp)!=1)
- return FALSE;
- SafeArrayGetLBound(pStrArrTemp,1,&LBound);
- SafeArrayGetUBound(pStrArrTemp,1,&UBound);
- SafeArrayAccessData(pStrArrTemp, (void HUGEP* FAR*)&pbstr);
- for (int i=0;i<(UBound-LBound)+1;i++)
- strtemp+=LPWSTR(pbstr);
- MessageBox( 0,strtemp,"結果",MB_OK);
- SafeArrayUnaccessData(pStrArrTemp);
- return TRUE;
- }
在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部分:
聲明:
- extern "C" _declspec(dllexport) BOOL WINAPI cPowerAlarm(PowerAlarm* tagPower,PowerResult* tagResult)
結構體定義:
- typedef struct tagPowerAlarm
- {
- char* strSIM;
- char* cStartTime;
- char* cEndTime;
- }PowerAlarm;
- typedef struct tagPowerResult
- {
- char cResultCH[20];
- char cResultQuality[20];
- char cResultHV[20];
- char cResultLV[20];
-
- }PowerResult;
VB部分:
聲明:
- Public Declare Function cPowerAlarm Lib "DataDiagnose.DLL" (ByRef myPower As h_PowerAlarm, ByRef myPowerResult As h_PowerResult) As Integer
結構體定義:
- Public Type h_PowerAlarm
- strSIM As String
- strStartTime As String
- strEndTime As String
- End Type
- Public Type h_PowerResult
- strResultCH As String * 20
- strResultQuality As String * 20
- strResultHV As String * 20
- strResultLV As String * 20
- End Type