第一种类型:数值传递
注意:在VB中,默认变量传递方式为ByRef为地址,而传递值就是用ByVal,还要注意在C++中,
int类型的变量是32位的,在VB中要用long型变量来配合。
VC++部分:
- 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
调用:
- Dim Tint As Long
- Tint = TestCalc(45)
- MsgBox Tint, vbExclamation
第二种类型:传递字符串,主要用于字符串返回或者处理。
VC++部分:
- extern "C" _declspec(dllexport) int __stdcall MidStr(CHAR * src,CHAR * dest)
- {
-
- strcpy(dest,src+1);
- return 0;
- }
.def文件
EXPORTS MidStr
VB部分:
声明:
- Private Declare Function MidStr Lib "Dll.dll" (ByVal src As String, ByVal dest As String) As Long
调用:
- 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
第四种类型:传递字符串数组
1、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
2、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
VB调用VC dll的返回方式,布布扣,bubuko.com
VB调用VC dll的返回方式
原文:http://www.cnblogs.com/rosesmall/p/3928644.html