VB 字符串数组 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 "ShowVBStrArr" (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
posted @ 2009-05-04 11:13  荷包蛋  阅读(2977)  评论(0编辑  收藏  举报