在VC中我做了一个API函数给VB调用:VC:代码片断:typedef struct S_LOG               
{
char* LogDateTime;
int UserID;                   
int Code;                     
int Val;                      
int Spare;                    
} LOG;
int WINAPI GetLog(int MachineID,int* LogNumber,S_LOG* pLog)
{
if(m_pComm==NULL)
{
return COMM_FAILURE;
}

char chSend[10]; //令牌报文
int i;
//cData[0]=LOBYTE(i); //取低位
//cData[1]= HIBYTE(i); //取高位 memset(chSend,0,sizeof(chSend));

*LogNumber=10;
for(i=0;i<10;i++)
{
pLog->Code=1;
pLog->LogDateTime="2002-10-10 12:08:23";
pLog->Spare=0;
pLog->Val=0;
pLog->UserID=3000+i;
pLog++;
}

return COMM_SUCCESS;
}
VB代码
Public Declare Function GetLog Lib "E:\MYWORK\指纹通讯开发包\API实现包\FingerComm\Debug\FingerComm.dll" (ByVal MachineID As Integer, ByRef LogNumber As Integer, ByRef LogInfo As Any) As IntegerType S_LOG
    LogDateTime As String                   
    UserID As Integer           
    Code As Integer                     
    Val As Integer                      
    Spare As Integer
End TypePrivate Sub Command2_Click()
    Dim Log(10) As S_LOG
    Dim i As Integer
    Dim iRes As Integer
    iRes = OpenComm(2, 9600)
    If iRes = 1 Then
        iRes = GetLog(1, i, Log(0))
        MsgBox Log(0).LogDateTime
        CloseComm
    End If
    
End SubVB代码我想得到Log(10)每个单元的值
VC中单步调试FOR循环中已经没有错在return COMM_SUCCESS;后报错,VB程序关闭,提示unhandled execption.....access voilation

解决方案 »

  1.   

    In Visual Basic:Type ARG
        i as Integer
        str as String
    End TypeIn C:typedef struct 
    {
        short i;
        BSTR str;
    } ARG;short WINAPI StructArray(LPSAFEARRAY *ppsaArg, 
        LPSAFEARRAY *ppsaStr)
    {
        ARG *parg;
        SAFEARRAY *psa;
        BSTR *pbstr;
        unsigned long i, cElements;
        #define BUFF_SIZE 1024
        TCHAR szBuff[BUFF_SIZE];    if (*ppsaStr == NULL)
            return -1;    cElements = (*ppsaStr)->rgsabound[0].cElements;    if (*ppsaArg == NULL) // create a new array
        {        if (FAILED(SafeArrayAllocDescriptor(1, &psa)))
                return -3;        // set up the SAFEARRAY structure
            // and allocate data space        psa->fFeatures = 0;
            psa->cbElements = sizeof(ARG);
            psa->rgsabound[0].cElements = cElements;
            psa->rgsabound[0].lLbound = (*ppsaStr)->rgsabound[0].lLbound;        if (FAILED(SafeArrayAllocData(psa))) 
            {
                SafeArrayDestroyDescriptor(psa);
                return -4;
            }        // get a pointer to the new data        if (FAILED(SafeArrayAccessData(psa, 
                    (void HUGEP* FAR*)&parg))) 
            {
                SafeArrayDestroy(psa);
                return -5;        }
        } 
        else // fail since we can't redimension
        {
                return -6;        // get a pointer to the old data        if (FAILED(SafeArrayAccessData(*ppsaArg, 
                    (void HUGEP* FAR*)&parg)))
                return -7;
        }    // get a pointer to the string array data    if (FAILED(SafeArrayAccessData(*ppsaStr, 
                (void HUGEP* FAR*)&pbstr)))
            return -8;    // allocate strings in the structure array and 
        // fill them with strings from the string array.
        // free any old BSTRs in the structure    for (i = 0; i < cElements; i++) 
        {
            SysFreeString(parg[i].bstr);//SysStringByteLen(pbstr[i])
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPSTR)pbstr[i], -1, 
                szBuff, sizeof(szBuff));
            parg[i].bstr = SysAllocString(szBuff);
            parg[i].i = SysStringLen(parg[i].bstr);
        }    // release pointers and move the structure
        // array pointer to the new array if we created one    SafeArrayUnaccessData(*ppsaStr);
        
        if (*ppsaArg == NULL) 
        {
            SafeArrayUnaccessData(psa);
            *ppsaArg = psa;
        }
        else 
            SafeArrayUnaccessData(*ppsaArg);
            
        return 0;
    }
    Declared and called from Visual Basic:Declare Function StructArray Lib "debug\ADVDLL.DLL" _
        (x() As ARG, s() As String) As IntegerSub StructArrayTest()
        Dim x() As ARG
        Dim s(1 To 4) As String
        s(1) = "yellow"
        s(2) = "orange"
        s(3) = "blue"
        s(4) = "green"
        n = StructArray(x, s)
        If n = 0 Then
            Worksheets(1).Activate
            Range("a1:c25").Clear
            For i = LBound(x) To UBound(x)
                Cells(i + 1, 1) = i
                Cells(i + 1, 2) = x(i).str
                Cells(i + 1, 3) = x(i).i
            Next
        Else
            MsgBox "StructArray failed, returned" & n
        End If
    End Sub
      

  2.   

    同意楼上的,基本上VB的String和VC的BSTR是一致的,但和char*不一致。